Skip to content

Commit 8c9ee06

Browse files
committed
Changed class from public to intrenal, changed nullable parameters, added validations, changed array to general ienumerable, changed object names from plural, translated Georgian comments to English
1 parent 164a3b5 commit 8c9ee06

File tree

14 files changed

+217
-88
lines changed

14 files changed

+217
-88
lines changed

examples/CoreApiAppExmaple/Controllers/TestController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public TestController(IExchangeRatesClient exchangeRatesClient)
1919
public async Task<ActionResult<OfficialRate>> GetOfficialRates(CancellationToken cancellationToken = default)
2020
{
2121
var result = await _exchangeRatesClient.GetOfficialRates(new string[] { "EUR", "USD"},cancellationToken);
22+
//var result = await _exchangeRatesClient.GetOfficialRatesByDate(new string[] { "EUR", "USD" }, "05-2022-01", cancellationToken);
2223
//var result = await _exchangeRatesClient.ConvertOfficialRates(120.4M,"GEL","USD", cancellationToken);
2324
//var result = await _exchangeRatesClient.GetCommercialRates(new string[] { "EUR", "USD" }, cancellationToken);
2425
//var result = await _exchangeRatesClient.ConvertCommercialRate(120.5M, "GEL", "USD" , cancellationToken);
Lines changed: 95 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,165 @@
1-
using TBC.OpenAPI.SDK.Core;
1+
using System.Runtime.CompilerServices;
2+
using TBC.OpenAPI.SDK.Core;
23
using TBC.OpenAPI.SDK.Core.Exceptions;
34
using TBC.OpenAPI.SDK.Core.Models;
5+
using TBC.OpenAPI.SDK.ExchangeRates.Helpers;
46
using TBC.OpenAPI.SDK.ExchangeRates.Models;
57

8+
[assembly: InternalsVisibleTo("TBC.OpenAPI.SDK.ExchangeRates.Tests")]
69
namespace TBC.OpenAPI.SDK.ExchangeRates
710
{
8-
public class ExchangeRatesClient : IExchangeRatesClient
11+
internal class ExchangeRatesClient : IExchangeRatesClient
912
{
10-
private readonly HttpHelper<ExchangeRatesClient> _http;
13+
private readonly IHttpHelper<ExchangeRatesClient> _http;
1114

12-
public ExchangeRatesClient(HttpHelper<ExchangeRatesClient> http)
15+
public ExchangeRatesClient(IHttpHelper<ExchangeRatesClient> http)
1316
{
1417
_http = http;
1518
}
1619

17-
1820
#region CommercialRates
21+
1922
/// <summary>
20-
/// კომერციული კურსის დასაბრუნებელი მეთოდი
23+
/// Gets commercial exchange rates for Georgian Lari
2124
/// </summary>
22-
/// <param name="currencies">(required) ვალუტები, რომლებიც უნდა დაბრუნდეს</param>
25+
/// <param name="currencies">List of comma-separated 3-letter currency codes for limiting results to specific currencies. e.g. USD,EUR,JPY. If this parameter is not provided, rates will be returned for all currencies</param>
2326
/// <param name="cancellationToken">(optional)</param>
24-
/// <returns>აბრუნებს გადაცემული ვალუტების კურსებს</returns>
25-
26-
public async Task<GetCommercialRatesResponse?> GetCommercialRates(string[] currencies, CancellationToken cancellationToken = default)
27+
/// <returns>Returns list of TBC Bank's commercial exchange rates</returns>
28+
public async Task<GetCommercialRatesResponse> GetCommercialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default)
2729
{
2830
var queryParams = new QueryParamCollection();
29-
queryParams.Add("currency", string.Join(",",currencies));
31+
32+
if (currencies != null)
33+
{
34+
ParametersValidationHelper.CurrencyListValidation(currencies);
35+
36+
queryParams.Add("currency", string.Join(",", currencies));
37+
}
3038

3139
var result = await _http.GetJsonAsync<GetCommercialRatesResponse>("/commercial", queryParams , cancellationToken).ConfigureAwait(false);
3240

3341
if (!result.IsSuccess)
3442
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception);
3543

36-
return result.Data!;
44+
return result.Data;
3745
}
3846

3947
/// <summary>
40-
/// კომერციული კურსის დასაკონვერტირებელი მეთოდი
48+
/// Converts amount between currencies based on TBC bank's commercial exchange rates
4149
/// </summary>
42-
/// <param name="amount">(required) დასაკონვენტირებელი თანხის რაოდენობა</param>
43-
/// <param name="from">(required) ვალუტა, საიდანაც უნდა დაკონვერტირდეს</param>
44-
/// <param name="to">(required) ვალუტა, რაშიც უნდა დაკონვერტირდეს</param>
50+
/// <param name="amount">(required) Value to be converted</param>
51+
/// <param name="from">(required) Base currency from which given amount should be converted</param>
52+
/// <param name="to">(required) Target currency to which amount should be converted</param>
4553
/// <param name="cancellationToken">(optional)</param>
46-
/// <returns>აბრუნებს დაკონვერტირებული ვალუტის კურსს</returns>
47-
48-
public async Task<ConvertCommercialRatesResponse?> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default)
54+
/// <returns>Returns convertion value of amount between currencies specified in from and to parameters based on TBC bank's commercial exchange rates</returns>
55+
public async Task<ConvertCommercialRatesResponse> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default)
4956
{
50-
var queryParams = new QueryParamCollection();
51-
queryParams.Add("amount", amount);
52-
queryParams.Add("from", from);
53-
queryParams.Add("to", to);
57+
ParametersValidationHelper.ConvertionParameterValidation(amount, from, to);
58+
59+
var queryParams = new QueryParamCollection
60+
{
61+
{ "amount", amount },
62+
{ "from", from },
63+
{ "to", to }
64+
};
5465

5566
var result = await _http.GetJsonAsync<ConvertCommercialRatesResponse>("/commercial/convert", queryParams, cancellationToken).ConfigureAwait(false);
5667

5768
if (!result.IsSuccess)
5869
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception);
5970

60-
return result.Data!;
71+
return result.Data;
6172
}
73+
6274
#endregion
6375

76+
#region Official Rates
77+
78+
/// <summary>
79+
/// Gets official exchange rates for Georgian Lari
80+
/// </summary>
81+
/// <param name="currencies">List of comma-separated 3-letter currency codes for limiting results to specific currencies. e.g. USD,EUR,JPY. If this parameter is not provided, rates will be returned for all currencies</param>
82+
/// <param name="cancellationToken">(optional)</param>
83+
/// <returns>Returns list of official exchange rates</returns>
84+
public async Task<List<OfficialRate>> GetOfficialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default)
85+
{
86+
var queryParams = new QueryParamCollection();
87+
88+
if (currencies != null)
89+
{
90+
ParametersValidationHelper.CurrencyListValidation(currencies);
91+
92+
queryParams.Add("currency", string.Join(",", currencies));
93+
}
6494

95+
var result = await _http.GetJsonAsync<List<OfficialRate>>("/nbg", queryParams, cancellationToken).ConfigureAwait(false);
6596

97+
if (!result.IsSuccess)
98+
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception);
99+
100+
return result.Data;
101+
}
66102

67-
#region Official Rates
68103
/// <summary>
69-
/// ოფიციალური კურსების დასაბრუნებელი მეთოდი
104+
/// Gets official exchange rates for Georgian Lari by specific date
70105
/// </summary>
71-
/// <param name="currencies">(optional) ვალუტების მასივი, რომლებიც უნდა დაბრუნდეს(ამ პარამეტრის არგადაცემის შემთხვევაში აბრუნებს ყველა ვალუტას)</param>
106+
/// <param name="date">Parameter for getting official rates for specific date. Date should be passed in YYYY-MM-dd format</param>
107+
/// <param name="currencies">List of comma-separated 3-letter currency codes for limiting results to specific currencies. e.g. USD,EUR,JPY. If this parameter is not provided, rates will be returned for all currencies</param>
72108
/// <param name="cancellationToken">(optional)</param>
73-
/// <returns>აბრუნებს ყველა ვალუტის ან გადაცემული ვალუტების კურსებს</returns>
74-
75-
public async Task<List<OfficialRate>?> GetOfficialRates(string[]? currencies = null, CancellationToken cancellationToken = default)
109+
/// <returns>Returns list of official exchange rates on specific date</returns>
110+
public async Task<List<OfficialRate>> GetOfficialRatesByDate(IEnumerable<string> currencies = null, string? date = null, CancellationToken cancellationToken = default)
76111
{
77112
var queryParams = new QueryParamCollection();
78-
if (currencies?.Any() ?? false)
113+
114+
if (currencies != null)
79115
{
80-
queryParams.Add("currency", string.Join(",",currencies));
116+
ParametersValidationHelper.CurrencyListValidation(currencies);
117+
118+
queryParams.Add("currency", string.Join(",", currencies));
81119
}
82120

83-
var result = await _http.GetJsonAsync<List<OfficialRate>?>("/nbg", queryParams, cancellationToken).ConfigureAwait(false);
121+
if (!string.IsNullOrEmpty(date))
122+
{
123+
ParametersValidationHelper.DateFormatValidation(date);
124+
125+
queryParams.Add("date", date);
126+
}
127+
128+
var result = await _http.GetJsonAsync<List<OfficialRate>>("/nbg", queryParams, cancellationToken).ConfigureAwait(false);
84129

85130
if (!result.IsSuccess)
86131
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception);
87132

88-
return result.Data!;
133+
return result.Data;
89134
}
90135

91136
/// <summary>
92-
/// ოფიციალური კურსის დასაკონვერტირებელი მეთოდი
137+
/// Converts amount between currencies based on official exchange rates
93138
/// </summary>
94-
/// <param name="amount">(required) დასაკონვენტირებელი თანხის რაოდენობა</param>
95-
/// <param name="from">(required) ვალუტა, საიდანაც უნდა დაკონვერტირდეს</param>
96-
/// <param name="to">(required) ვალუტა, რაშიც უნდა დაკონვერტირდეს</param>
139+
/// <param name="amount">(required) Value to be converted</param>
140+
/// <param name="from">(required) Base currency from which given amount should be converted</param>
141+
/// <param name="to">(required) Target currency to which amount should be converted</param>
97142
/// <param name="cancellationToken">(optional)</param>
98-
/// <returns>დაკონვერტირებული ვალუტის კურსი</returns>
99-
public async Task<ConvertOfficialRatesResponse?> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default)
143+
/// <returns>Returns convertion value of amount between currencies specified in from and to parameters based on official exchange rates</returns>
144+
public async Task<ConvertOfficialRatesResponse> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default)
100145
{
101-
var queryParams = new QueryParamCollection();
102-
queryParams.Add("amount", amount);
103-
queryParams.Add("from", from);
104-
queryParams.Add("to", to);
146+
ParametersValidationHelper.ConvertionParameterValidation(amount, from, to);
147+
148+
var queryParams = new QueryParamCollection
149+
{
150+
{ "amount", amount },
151+
{ "from", from },
152+
{ "to", to }
153+
};
105154

106155
var result = await _http.GetJsonAsync<ConvertOfficialRatesResponse>("/nbg/convert", queryParams, cancellationToken).ConfigureAwait(false);
107156

108157
if (!result.IsSuccess)
109158
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception);
110159

111-
return result.Data!;
160+
return result.Data;
112161
}
162+
113163
#endregion
114164
}
115165
}

src/TBC.OpenAPI.SDK.ExchangeRates/Extensions/FactoryExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public static OpenApiClientFactoryBuilder AddExchangeRatesClient(this OpenApiCli
99

1010
public static OpenApiClientFactoryBuilder AddExchangeRatesClient(this OpenApiClientFactoryBuilder builder,
1111
ExchangeRatesClientOptions options,
12-
Action<HttpClient>? configureClient = null,
13-
Func<HttpClientHandler>? configureHttpMessageHandler = null)
12+
Action<HttpClient> configureClient = null,
13+
Func<HttpClientHandler> configureHttpMessageHandler = null)
1414
{
1515
return builder.AddClient<IExchangeRatesClient, ExchangeRatesClient, ExchangeRatesClientOptions>(options, configureClient, configureHttpMessageHandler);
1616
}

src/TBC.OpenAPI.SDK.ExchangeRates/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public static IServiceCollection AddExchangeRatesClient(this IServiceCollection
99
=> AddExchangeRatesClient(services, options, null, null);
1010

1111
public static IServiceCollection AddExchangeRatesClient(this IServiceCollection services, ExchangeRatesClientOptions options,
12-
Action<HttpClient>? configureClient = null,
13-
Func<HttpClientHandler>? configureHttpMessageHandler = null)
12+
Action<HttpClient> configureClient = null,
13+
Func<HttpClientHandler> configureHttpMessageHandler = null)
1414
{
1515
services.AddOpenApiClient<IExchangeRatesClient, ExchangeRatesClient, ExchangeRatesClientOptions>(options, configureClient, configureHttpMessageHandler);
1616
return services;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Globalization;
2+
using System.Text.RegularExpressions;
3+
using TBC.OpenAPI.SDK.Core.Exceptions;
4+
5+
namespace TBC.OpenAPI.SDK.ExchangeRates.Helpers
6+
{
7+
internal static class ParametersValidationHelper
8+
{
9+
private const string CURRENCY_PATTERN = "^[A-Z]{3}$";
10+
private const string CURRENCY_LIST_PATTERN = "^[A-Z]{3}(?:,[A-Z]{3})*$";
11+
12+
internal static void ConvertionParameterValidation(decimal amount, string from, string to)
13+
{
14+
Regex currencyRegEx = new Regex(CURRENCY_PATTERN);
15+
16+
if (string.IsNullOrEmpty(from))
17+
throw new OpenApiException("Base currency parameter 'from' must not be empty.");
18+
19+
if (!currencyRegEx.IsMatch(from))
20+
throw new OpenApiException("Base currency format is invalid. Please use 3-letter currency codes.");
21+
22+
if (string.IsNullOrEmpty(to))
23+
throw new OpenApiException("Target currency parameter 'to' must not be empty.");
24+
25+
if (!currencyRegEx.IsMatch(to))
26+
throw new OpenApiException("Target currency format is invalid. Please use 3-letter currency codes.");
27+
}
28+
29+
internal static void CurrencyFormatValidation(IEnumerable<string> currencies)
30+
{
31+
Regex currencyRegEx = new Regex(CURRENCY_PATTERN);
32+
33+
if (currencies.Any(x => !currencyRegEx.IsMatch(x)))
34+
throw new OpenApiException("Currency format is invalid. Please use 3-letter currency codes.");
35+
}
36+
37+
internal static void CurrencyListValidation(IEnumerable<string> currencies)
38+
{
39+
if (currencies.Any(x => string.IsNullOrEmpty(x)))
40+
throw new OpenApiException("List of currencies contains empty element.");
41+
42+
CurrencyFormatValidation(currencies);
43+
}
44+
45+
internal static void DateFormatValidation(string date)
46+
{
47+
if (!DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
48+
throw new OpenApiException("Date format is invalid. Please use YYYY-MM-dd format.");
49+
}
50+
}
51+
}

src/TBC.OpenAPI.SDK.ExchangeRates/Interfaces/IExchangeRatesClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ namespace TBC.OpenAPI.SDK.ExchangeRates
55
{
66
public interface IExchangeRatesClient : IOpenApiClient
77
{
8-
Task<GetCommercialRatesResponse?> GetCommercialRates(string[] currencies, CancellationToken cancellationToken = default);
9-
Task<ConvertCommercialRatesResponse?> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default);
10-
Task<List<OfficialRate>?> GetOfficialRates(string[]? currencies = null, CancellationToken cancellationToken = default);
11-
Task<ConvertOfficialRatesResponse?> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default);
8+
Task<GetCommercialRatesResponse> GetCommercialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default);
9+
Task<ConvertCommercialRatesResponse> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default);
10+
Task<List<OfficialRate>> GetOfficialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default);
11+
Task<List<OfficialRate>> GetOfficialRatesByDate(IEnumerable<string> currencies = null, string date = null, CancellationToken cancellationToken = default);
12+
Task<ConvertOfficialRatesResponse> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default);
1213
}
1314
}

src/TBC.OpenAPI.SDK.ExchangeRates/Models/CommercialRates.cs renamed to src/TBC.OpenAPI.SDK.ExchangeRates/Models/CommercialRate.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
namespace TBC.OpenAPI.SDK.ExchangeRates.Models
22
{
33
/// <summary>
4-
/// კომერციული კურსის მოდელი
4+
/// TBC Bank's commercial exchange rate response model
55
/// </summary>
6-
public class CommercialRates
6+
public class CommercialRate
77
{
88
/// <summary>
9-
/// ვალუტა
9+
/// 3-digit currency code
1010
/// </summary>
11-
public string? Currency { get; set; }
11+
public string Currency { get; set; }
1212

1313
/// <summary>
14-
/// ყიდვის კურსი
14+
/// Rate value at which bank is buying target currency
1515
/// </summary>
1616
public decimal Buy { get; set; }
1717

1818
/// <summary>
19-
/// გაყიდვის კურსი
19+
/// Rate value at which bank is selling target currency
2020
/// </summary>
2121
public decimal Sell { get; set; }
2222
}

0 commit comments

Comments
 (0)