As mentioned in my last week Elixir blog post, I produced some quick fake API based on Azure Functions. I thought it’s gonna take a couple of minutes, but it turned out to be a whole adventure in itself.
The creating of a function is a breeze.
- Go to Portal, click big green “+” sign and search for “Function App”
- Pick Function App published by Microsoft
- Fill all the necessary fields like App name (must be globally unique) or location. For hosting plan I used “Consumption plan” which means, I pay only for the time that function is running. I also like to pin my stuff to the dashboard, so it’s easier to find.
- It will take several minutes to deploy.
- Now you can create your functions for the app. F# is hidden in small print just above “Create this function” button. So click “create your own custom function”.
- Then with Language drop-down, pick “F#” and for Scenario – “API & Webhooks”. There should be on the F# function triggered by HTTP request. That’s the one you want for API.
- You’ll get premade piece of code with a simple function that is triggered by HTTP POST with name object and responses “Hello “.
Then I started writing the logic I wanted. I made an array of hard coded airport data. I made the function to accept only GET requests (you can change it in
function.json file). In code, I parse query strings and get the airport IATA code. If I have this airport in my array, I response 200 with JSON containing the data. Otherwise, I return 404. If there’s no parameter in the query string, function answers with 500.
It’s relatively simple and straightforward F# code. I just struggled a lot with debugging. The small editor on Azure doesn’t give you static analysis, nor type information and no squigglies. You need to run the function and check for compilation errors or runtime errors. There was also some weird scoping behaviour, that forced me to declare the Airports array within the function. Anyways, after 2hrs I had an API that did what I wanted. You can see the code below. It’s not bulletproof, but it does the job. And I got to play with Azure Functions a bit.