Http call using F# and HttpClient
It took me a few tries to finally call an HTTP endpoint using F#.
I've ended up with this code:
let result =
async {
let client = new HttpClient()
let url = "https://api.frankfurter.app/latest?amount=10.99&from=USD&to=PLN"
let! result =
client.GetFromJsonAsync<ApiResponse>(url)
|> Async.AwaitTask
printfn $"10.99 in USD is {result.Rates.PLN} in PLN"
}
result |> Async.RunSynchronously
For my taste, it is very similar to the C# code.
The most significant difference lies in the async block.
It doesn't run until you do it explicitly!
In the C#, when you create a task, you have no control over when it starts. I've described one of such scenarios here.
Only after calling Async.RunSynchronously the actual workflow runs. It reminds a bit of await instruction in C#, but the inner working is different.
The complete code is available on gist.
I like this functional approach with cold workflows. It seems more intuitive to me. One downside I can see is that it may require more code. But I don't have enough experience with F# to be sure.