This post announces the new
It allows you to fetch exchange rates from the https://exchangeratesapi.io/ website.
Free currency exchange rate API's are surprisingly hard to find. https://exchangeratesapi.io/ is the only one that I found, so I decided to write a little Haskell client to call the API. The API is free to use, and the author asks to cache the results as much as possible, so the Haskell client automatically caches all results as much as possible.
The library is laid out as follows:
- ExchangeRates.Types contains the types for currencies, rates, etc...
- ExchangeRates.API contains the raw API types, made with Servant.
- ExchangeRates.Cache contains the caching functionality
- ExchangeRates.Client contains the code to actually call the API.
- ExchangeRates re-exports all the relevant functions and types to use the library.
To call the https://exchangeratesapi.io/ APi in the simplest way possible, we need to specificy a cache file, and our query. The following is a minimal example that gets the exchange rates for 2018-01-19 and prints them to standard output:
main :: IO main = do rates <- autoRunExchangeRatesClient $ withFileCache "/tmp/exchangerates.cache" $ getAtDate (fromGregorian 2018 01 19) (Just EUR) Nothing case rates of Left err -> die $ show err Right v -> print v
This little piece of code will call the live API and save the cache with results to
The next time this piece of code is run, it will read
/tmp/exchangerates.cache and notice that it does not need to call the live API anymore, and just print the results.