Skip to content

Instantly share code, notes, and snippets.

@epi
Created January 12, 2018 13:30
Show Gist options
  • Save epi/21bcb217a4700878b6044e24dd3ba140 to your computer and use it in GitHub Desktop.
Save epi/21bcb217a4700878b6044e24dd3ba140 to your computer and use it in GitHub Desktop.
NBP Web API
/+dub.sdl:
dependency "vibe-d" version="~>0.8.0"
+/
import vibe.d;
import std.stdio;
struct RateByDate
{
string no;
string effectiveDate;
@optional double ask;
@optional double bid;
@optional double mid;
}
struct Rates
{
string table;
string currency;
string code;
RateByDate[] rates;
}
struct RateByCurrency
{
string currency;
string code;
@optional double ask;
@optional double bid;
@optional double mid;
}
struct Table
{
string table;
string no;
string effectiveDate;
RateByCurrency[] rates;
}
@path("/api/exchangerates/")
interface NBPWebAPI
{
@path("rates/:table/:code/")
Rates getCurrent(string _table, string _code);
@path("rates/:table/:code/last/:topCount/")
Rates getLast(string _table, string _code, uint _topCount);
@path("rates/:table/:code/today/")
Rates getToday(string _table, string _code);
@path("rates/:table/:code/:date/")
Rates getOn(string _table, string _code, string _date);
@path("rates/:table/:code/:startDate/:endDate/")
Rates getBetween(string _table, string _code, string _startDate, string _endDate);
@path("tables/:table/")
Table[] getCurrent(string _table);
@path("tables/:table/last/:topCount")
Table[] getLast(string _table, uint _topCount);
@path("tables/:table/today")
Table[] getToday(string _table);
@path("tables/:table/:date")
Table[] getOn(string _table, string _date);
@path("tables/:table/:startDate/:endDate")
Table[] getBetween(string _table, string _startDate, string _endDate);
}
void main()
{
auto client = new RestInterfaceClient!NBPWebAPI("http://api.nbp.pl/");
writeln(client.getCurrent("A", "EUR"));
writeln(client.getCurrent("B", "EUR"));
writeln(client.getCurrent("C", "EUR"));
writeln(client.getLast("A", "USD", 20));
writeln(client.getToday("C", "CHF"));
writeln(client.getOn("A", "EUR", "2017-07-24"));
writeln(client.getBetween("C", "EUR", "2017-12-01", "2017-12-31"));
writeln(client.getCurrent("A"));
writeln(client.getLast("C", 20));
writeln(client.getToday("C"));
writeln(client.getOn("A", "2017-07-24"));
writeln(client.getBetween("C", "EUR", "2017-12-01", "2017-12-31"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment