2using System.Collections.Generic;
4using System.Threading.Tasks;
14 private readonly
string apiKey;
33 private async Task<T> RequestRespond<T>(
string Url,
string ResponseElement,
34 params KeyValuePair<string, string>[] Parameters)
36 StringBuilder sb =
new StringBuilder();
38 sb.Append(
"https://openexchangerates.org/api/");
40 sb.Append(
"?app_id=");
41 sb.Append(this.apiKey);
43 foreach (KeyValuePair<string, string> P
in Parameters)
46 sb.Append(HttpUtility.UrlEncode(P.Key));
48 sb.Append(HttpUtility.UrlEncode(P.Value));
51 Uri Uri =
new Uri(sb.ToString());
55 new KeyValuePair<string, string>(
"Accept",
"application/json"),
56 }) is Dictionary<string, object> Result))
58 throw new Exception(
"Unexpected response returned from openexchangerates.org.");
61 if (
string.IsNullOrEmpty(ResponseElement))
63 if (Result is T Response)
66 else if (Result.TryGetValue(ResponseElement, out
object Obj))
68 if (Obj is T Response2)
70 else if (System.Convert.ChangeType(Obj, typeof(T)) is T Response3)
74 throw new Exception(
"Unexpected response returned from openexchangerates.org.");
95 public Task<ExchangeRate[]>
GetLatest(
string BaseCurrency)
97 return this.
GetLatest(BaseCurrency,
null,
false);
107 return this.
GetLatest(
null, Symbols,
false);
115 public Task<ExchangeRate[]>
GetLatest(
bool ShowAlternative)
117 return this.
GetLatest(
null,
null, ShowAlternative);
126 public Task<ExchangeRate[]>
GetLatest(
string BaseCurrency,
string[] Symbols)
128 return this.
GetLatest(BaseCurrency, Symbols,
false);
137 public Task<ExchangeRate[]>
GetLatest(
string BaseCurrency,
bool ShowAlternative)
139 return this.
GetLatest(BaseCurrency,
null, ShowAlternative);
149 public async Task<ExchangeRate[]>
GetLatest(
string BaseCurrency,
string[] Symbols,
150 bool ShowAlternative)
152 List<KeyValuePair<string, string>> Request =
new List<KeyValuePair<string, string>>()
154 new KeyValuePair<string, string>(
"show_alternative",
CommonTypes.
Encode(ShowAlternative))
157 if (!
string.IsNullOrEmpty(BaseCurrency))
158 Request.Add(
new KeyValuePair<string, string>(
"base", BaseCurrency));
160 if (!(Symbols is
null) && Symbols.Length > 0)
162 StringBuilder sb =
new StringBuilder();
165 foreach (
string Symbol
in Symbols)
175 Request.Add(
new KeyValuePair<string, string>(
"symbols", sb.ToString()));
178 Dictionary<string, object> Response = await this.RequestRespond<Dictionary<string, object>>(
"latest.json",
null,
181 if (Response.TryGetValue(
"base", out
object Obj) && Obj is
string Base &&
182 Response.TryGetValue(
"rates", out Obj) && Obj is Dictionary<string, object> Rates)
184 int i = 0, c = Rates.Count;
186 Dictionary<CaseInsensitiveString, decimal> Reference =
new Dictionary<CaseInsensitiveString, decimal>();
187 DateTime TP = DateTime.UtcNow;
189 foreach (KeyValuePair<string, object> P
in Rates)
193 Result[i++] =
new ExchangeRate(Base, P.Key, d, TP,
"openexchangerates.org");
194 Reference[P.Key] = d;
200 throw new Exception(
"Unexpected response returned from openexchangerates.org.");
205 #region Get Currencies
226 public async Task<CurrencySymbol[]>
GetCurrencies(
bool ShowAlternative,
bool ShowInactive)
228 Dictionary<string, object> Symbols = await this.RequestRespond<Dictionary<string, object>>(
"currencies.json",
null,
229 new KeyValuePair<string, string>(
"show_alternative",
CommonTypes.
Encode(ShowAlternative)),
230 new KeyValuePair<string, string>(
"show_inactive",
CommonTypes.
Encode(ShowInactive)));
231 int i = 0, c = Symbols.Count;
234 foreach (KeyValuePair<string, object> P
in Symbols)
250 StringBuilder sb =
new StringBuilder();
252 sb.Append(
"convert/");
253 sb.Append(Amount.ToString());
255 sb.Append(From.
Value.ToUpper());
257 sb.Append(To.
Value.ToUpper());
259 double Converted = await this.RequestRespond<double>(sb.ToString(),
"result");
261 return (decimal)Converted;
string ApiKey
Current API Key
Task< ExchangeRate[]> GetLatest(string[] Symbols)
Gets latest exchange rates.
OpenExchangeRateClient(string ApiKey)
Currency Converter service from openexchangerates.org.
Task< ExchangeRate[]> GetLatest(bool ShowAlternative)
Gets latest exchange rates.
Task< ExchangeRate[]> GetLatest(string BaseCurrency, bool ShowAlternative)
Gets latest exchange rates.
Task< ExchangeRate[]> GetLatest(string BaseCurrency, string[] Symbols)
Gets latest exchange rates.
async Task< ExchangeRate[]> GetLatest(string BaseCurrency, string[] Symbols, bool ShowAlternative)
Gets latest exchange rates.
Task< ExchangeRate[]> GetLatest(string BaseCurrency)
Gets latest exchange rates.
async Task< CurrencySymbol[]> GetCurrencies(bool ShowAlternative, bool ShowInactive)
Gets supported currency symbols.
async Task< decimal > Convert(CaseInsensitiveString From, CaseInsensitiveString To, int Amount)
Converts an amount from one currency to another.
Task< CurrencySymbol[]> GetCurrencies()
Gets supported currency symbols.
Task< ExchangeRate[]> GetLatest()
Gets latest exchange rates.
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Static class managing encoding and decoding of internet content.
static Task< object > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
Class managing a script expression.
static decimal ToDecimal(object Object)
Converts an object to a double value.