Created
April 16, 2017 00:01
-
-
Save mlusiak/5053aabbc1c6e76db082dc8daa952c81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "System.Net.Http" | |
#r "Newtonsoft.Json" | |
open System.Net | |
open System.Net.Http | |
open Newtonsoft.Json | |
type Airport = { | |
iata: string; | |
country: string; | |
city: string; | |
lat: float; | |
lon: float; | |
} | |
let Run(req: HttpRequestMessage, log: TraceWriter) = | |
async { | |
let airports = | |
[ | |
{iata="CPH"; country="Denmark"; city="Copenhagen"; lat=55.618056; lon=12.656111} | |
{iata="GDN"; country="Poland"; city="Gdansk"; lat=54.3775; lon=18.466111} | |
{iata="LHR"; country="United Kingdom"; city="London"; lat=51.4775; lon= -0.461389} | |
{iata="ARN"; country="Sweden"; city="Stockholm"; lat=59.651944; lon=17.918611} | |
] | |
log.Info(sprintf | |
"F# HTTP trigger function processed a request.") | |
// Set name to query string | |
let iata = | |
req.GetQueryNameValuePairs() | |
|> Seq.tryFind (fun q -> q.Key = "iataCode") | |
match iata with | |
| Some a -> | |
let query = airports |> List.filter(fun x -> x.iata = a.Value ) | |
match query with | |
| [] -> | |
return req.CreateResponse(HttpStatusCode.NotFound, "No data on the airport"); | |
| _ -> | |
let airport = query |> List.head | |
return req.CreateResponse(HttpStatusCode.OK, airport, "application/json"); | |
| None -> | |
return req.CreateResponse(HttpStatusCode.BadRequest, "Specify iata code!", "application/json"); | |
} |> Async.RunSynchronously |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment