dinero is a Go client library for accessing the Open Exchange Rates API (https://docs.openexchangerates.org/docs/).
Upon request of forex rates these will be cached (in-memory), keyed by base currency. With a two hour expiry window, subsequent requests will use cached data or fetch fresh data accordingly.
go get -u github.com/mattevans/dinero
Get All
// Init dinero client.
client := NewClient(os.Getenv("OPEN_EXCHANGE_APP_ID"))
// Set a base currency to work with.
client.Rates.SetBaseCurrency("AUD")
// Get latest forex rates using AUD as a base currency.
response, err := client.Rates.All()
if err != nil {
return err
}
{
"rates":{
"AED":2.702388,
"AFN":48.893275,
"ALL":95.142814,
"AMD":356.88691,
...
},
"updated_at":"2016-12-16T11:25:47.38290048+13:00",
"base":"AUD"
}
Get Single
// Init dinero client.
client := NewClient(os.Getenv("OPEN_EXCHANGE_APP_ID"))
// Set a base currency to work with.
client.Rates.SetBaseCurrency("AUD")
// Get latest forex rate for NZD using AUD as a base currency.
response, err := client.Rates.Single("NZD")
if err != nil {
return err
}
1.045545
Expire in-memory cache
The following will expire the in-memory cache for the set base currency.
client.Cache.Expire()
If you've found a bug or would like to contribute, please create an issue here on GitHub, or better yet fork the project and submit a pull request!