-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatesExchangeApiService.cs
279 lines (256 loc) · 12.7 KB
/
RatesExchangeApiService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RatesExchangeApi.Models;
namespace RatesExchangeApi
{
/// <summary>
/// Wrapper class for interacting with the Rates Exchange Api.
/// Returns ECB rates and provides API usage information.
/// </summary>
public class RatesExchangeApiService
{
/// <summary>
/// The API key to use in all requests.
/// </summary>
private readonly string _apiKey;
#region Consts
private const string ApiBaseUrl = "https://api.ratesexchange.eu";
private const string CheckIfApiIsOnLineUrl = "{0}/client/checkapi";
private const string GetLatestRatesUrl = "{0}/client/latest?apikey={1}&base_currency={2}¤cies={3}";
private const string GetLatestDetailsRatesUrl = "{0}/client/latestdetails?apikey={1}&base_currency={2}¤cies={3}";
private const string GetHistoryRatesUrl = "{0}/client/history?apiKey={1}&base_currency={2}&date={3}¤cies={4}";
private const string GetHistoryDetailsRatesUrl = "{0}/client/historydetails?apiKey={1}&base_currency={2}&date={3}¤cies={4}";
private const string GetHistoryRatesForCurrencyUrl = "{0}/client/historydates?apiKey={1}¤cy={2}&from_date={3}";
private const string ConvertCurrencyUrl = "{0}/client/convert?apiKey={1}&from={2}&amount={3}&date={4}¤cies={5}";
private const string ConvertCurrencyDetailsUrl = "{0}/client/convertdetails?apiKey={1}&from={2}&amount={3}&date={4}¤cies={5}";
private const string GetCurrenciesUrl = "{0}/client/currencies?apiKey={1}";
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="RatesExchangeApiService"/> class.
/// </summary>
/// <param name="key">
/// The API key to use.
/// </param>
public RatesExchangeApiService(string key)
{
_apiKey = key;
}
/// <summary>
/// Asynchronously checks if API is online or not
/// </summary>
/// <returns>
/// A <see cref="Task"/> for a <see cref="BoolResponse"/> with the requested data.
/// </returns>
public async Task<BoolResponse> CheckIfApiIsOnline()
{
var requestUrl = string.Format(CultureInfo.InvariantCulture, CheckIfApiIsOnLineUrl, ApiBaseUrl);
return await HttpHandler.GetResponseFromUrlAsync<BoolResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves the latest rates
/// </summary>
/// <param name="baseCurrency">
/// Base currency (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesResponse> GetLatestRates(string baseCurrency, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetLatestRatesUrl, ApiBaseUrl, _apiKey, baseCurrency, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves the latest rates
/// </summary>
/// <param name="baseCurrency">
/// Base currency (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesDetailsResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesDetailsResponse> GetLatestDetailsRates(string baseCurrency, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetLatestDetailsRatesUrl, ApiBaseUrl, _apiKey, baseCurrency, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesDetailsResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves rates for a specific date
/// </summary>
/// <param name="baseCurrency">
/// Base currency (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <param name="date">
/// Rates date (format: YYYY-MM-DD)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesResponse> GetHistoryRates(string baseCurrency, string date, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetHistoryRatesUrl, ApiBaseUrl, _apiKey, baseCurrency, date, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves rates for a specific date
/// </summary>
/// <param name="baseCurrency">
/// Base currency (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <param name="date">
/// Rates date (format: YYYY-MM-DD)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesDetailsResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesDetailsResponse> GetHistoryDetailsRates(string baseCurrency, string date, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetHistoryDetailsRatesUrl, ApiBaseUrl, _apiKey, baseCurrency, date, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesDetailsResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves rates for a specific currency <see cref="baseCurrency"/>
/// </summary>
/// <param name="baseCurrency">
/// Base currency (ISO format)
/// </param>
/// <param name="date">
/// Rates date (format: YYYY-MM-DD)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesHistoryResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesHistoryResponse> GetHistoryRatesForCurrency(string baseCurrency, string date)
{
ThrowExceptionIfApiKeyInvalid();
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetHistoryRatesForCurrencyUrl, ApiBaseUrl, _apiKey, baseCurrency, date);
return await HttpHandler.GetResponseFromUrlAsync<RatesHistoryResponse>(requestUrl);
}
/// <summary>
/// Asynchronously converts from one currency <see cref="fromCurrency"/> to many <see cref="currencies"/> for a specific date.
/// </summary>
/// <param name="fromCurrency">
/// Currency to convert (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <param name="date">
/// Rates date (format: YYYY-MM-DD)
/// </param>
/// <param name="amount">
/// Amount to convert (ex: 100,50)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesResponse> ConvertCurrency(string fromCurrency, string amount, string date, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, ConvertCurrencyUrl, ApiBaseUrl, _apiKey, fromCurrency, amount, date, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesResponse>(requestUrl);
}
/// <summary>
/// Asynchronously converts from one currency <see cref="fromCurrency"/> to many <see cref="currencies"/> for a specific date.
/// </summary>
/// <param name="fromCurrency">
/// Currency to convert (ISO format)
/// </param>
/// <param name="currencies">
/// List of currencies (ISO format) separated by a comma (optional)
/// </param>
/// <param name="date">
/// Rates date (format: YYYY-MM-DD)
/// </param>
/// <param name="amount">
/// Amount to convert (ex: 100,50)
/// </param>
/// <returns>
/// A <see cref="Task"/> for a <see cref="RatesDetailsResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<RatesDetailsResponse> ConvertCurrencyDetails(string fromCurrency, string amount, string date, List<string> currencies = null)
{
ThrowExceptionIfApiKeyInvalid();
var currenciesList = CheckCurrenciesValues(currencies);
var requestUrl = string.Format(CultureInfo.InvariantCulture, ConvertCurrencyDetailsUrl, ApiBaseUrl, _apiKey, fromCurrency, amount, date, currenciesList);
return await HttpHandler.GetResponseFromUrlAsync<RatesDetailsResponse>(requestUrl);
}
/// <summary>
/// Asynchronously retrieves all available currencies
/// </summary>
/// <returns>
/// A <see cref="Task"/> for a <see cref="CurrenciesResponse"/> with the requested data, or null if the data was corrupted.
/// </returns>
/// <exception cref="ValidationException">
/// Thrown when the service returned anything other than a 200 (Status OK) code.
/// </exception>
public async Task<List<CurrenciesResponse>> GetCurrencies()
{
ThrowExceptionIfApiKeyInvalid();
var requestUrl = string.Format(CultureInfo.InvariantCulture, GetCurrenciesUrl, ApiBaseUrl, _apiKey);
return await HttpHandler.GetResponseFromUrlAsync<List<CurrenciesResponse>>(requestUrl);
}
#region Private methods
private void ThrowExceptionIfApiKeyInvalid()
{
if (string.IsNullOrEmpty(_apiKey))
{
throw new InvalidOperationException("No API key was given.");
}
}
private static string CheckCurrenciesValues(IReadOnlyCollection<string> currencies)
{
if (currencies == null) return null;
var currenciesList = new StringBuilder();
currenciesList.Append(string.Join(",", currencies.Select(x => x.ToString().ToLowerInvariant())));
return currenciesList.ToString();
}
#endregion
}
}