| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Official D client for the UniRate API — free, real-time and historical currency exchange rates plus VAT rates.
Add the dependency with dub:
dub add unirate-apior in your dub.json:
"dependencies": {
"unirate-api": "~>0.1.0"
}import unirate;
import std.process : environment;
import std.stdio : writefln;
void main()
{
auto client = new Client(environment["UNIRATE_API_KEY"]);
double rate = client.getRate("USD", "EUR");
writefln("USD -> EUR: %.4f", rate);
double euros = client.convert(100, "USD", "EUR");
writefln("100 USD = %.2f EUR", euros);
}Get a free API key at https://unirateapi.com.
auto client = new Client(
"your-api-key",
30.seconds, // request timeout (default 30s)
"https://api.unirateapi.com", // base URL (override for tests)
);double rate = client.getRate("USD", "EUR");
// → double
double[string] rates = client.getAllRates("USD");
// → currency code -> rate
double result = client.convert(100, "USD", "EUR");
// → double
string[] codes = client.getSupportedCurrencies();
// → list of currency codesdouble rate = client.getHistoricalRate("2024-01-01", "USD", "EUR");
double[string] rates = client.getHistoricalRates("2024-01-01", "USD");
double amount = client.convertHistorical(100, "USD", "EUR", "2024-01-01");
auto series = client.getTimeSeries(
"2024-01-01", "2024-01-07",
1, // amount
"USD", // base
["EUR", "GBP"], // pass [] for all currencies
);
auto limits = client.getHistoricalLimits();auto all = client.getVatRates();
auto de = client.getVatRate("DE");
writeln(de.vatData.vatRate); // 19.0Every method accepts an optional trailing CallOptions argument for the API's format and callback query parameters:
client.getRate("USD", "EUR", CallOptions("xml"));The typed methods always decode JSON, so requesting a non-JSON format will fail to parse — build the URL yourself if you need a raw XML/CSV body.
All errors derive from UniRateException. Catch that to handle every failure, or a specific subclass to branch on the HTTP semantics:
import unirate;
try
{
auto rate = client.getRate("USD", "ZZZ");
}
catch (AuthenticationException e) { /* 401 — invalid API key */ }
catch (InvalidCurrencyException e) { /* 404 — unknown currency code */ }
catch (RateLimitException e) { /* 429 — back off and retry */ }
catch (InvalidDateException e) { /* 400 — bad request parameters */ }
catch (APIException e)
{
// Any other non-2xx, including 403 on Pro-gated endpoints and 503.
writefln("status %d: %s", e.statusCode, e.responseBody);
}
catch (UniRateException e) { /* transport / parse failure */ }The constructor accepts a Transport delegate — Response delegate(string url, string[string] headers) — so tests can run entirely offline without libcurl:
import unirate;
import core.time : seconds;
Response stub(string url, string[string] headers)
{
return Response(200, `{"rate": "0.9"}`);
}
auto client = new Client("test-key", 30.seconds, "https://mock.local", &stub);
assert(client.getRate("USD", "EUR") == 0.9);dub test # mock tests (no network)
UNIRATE_API_KEY=... dub test # additionally runs the free-tier live testsUniRate ships official client libraries and framework integrations across the ecosystem. The repos below are all maintained under the UniRate-API org.
Get a free API key at unirateapi.com.
MIT — see LICENSE.
| Back | FazBrowse Home | New Git URL |