-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from TBCBank/bugfix/minor-fixes
Changed class from public to intrenal, changed nullable parameters, …
- Loading branch information
Showing
14 changed files
with
217 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 95 additions & 45 deletions
140
src/TBC.OpenAPI.SDK.ExchangeRates/ExchangeRatesClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,165 @@ | ||
using TBC.OpenAPI.SDK.Core; | ||
using System.Runtime.CompilerServices; | ||
using TBC.OpenAPI.SDK.Core; | ||
using TBC.OpenAPI.SDK.Core.Exceptions; | ||
using TBC.OpenAPI.SDK.Core.Models; | ||
using TBC.OpenAPI.SDK.ExchangeRates.Helpers; | ||
using TBC.OpenAPI.SDK.ExchangeRates.Models; | ||
|
||
[assembly: InternalsVisibleTo("TBC.OpenAPI.SDK.ExchangeRates.Tests")] | ||
namespace TBC.OpenAPI.SDK.ExchangeRates | ||
{ | ||
public class ExchangeRatesClient : IExchangeRatesClient | ||
internal class ExchangeRatesClient : IExchangeRatesClient | ||
{ | ||
private readonly HttpHelper<ExchangeRatesClient> _http; | ||
private readonly IHttpHelper<ExchangeRatesClient> _http; | ||
|
||
public ExchangeRatesClient(HttpHelper<ExchangeRatesClient> http) | ||
public ExchangeRatesClient(IHttpHelper<ExchangeRatesClient> http) | ||
{ | ||
_http = http; | ||
} | ||
|
||
|
||
#region CommercialRates | ||
|
||
/// <summary> | ||
/// კომერციული კურსის დასაბრუნებელი მეთოდი | ||
/// Gets commercial exchange rates for Georgian Lari | ||
/// </summary> | ||
/// <param name="currencies">(required) ვალუტები, რომლებიც უნდა დაბრუნდეს</param> | ||
/// <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> | ||
/// <param name="cancellationToken">(optional)</param> | ||
/// <returns>აბრუნებს გადაცემული ვალუტების კურსებს</returns> | ||
|
||
public async Task<GetCommercialRatesResponse?> GetCommercialRates(string[] currencies, CancellationToken cancellationToken = default) | ||
/// <returns>Returns list of TBC Bank's commercial exchange rates</returns> | ||
public async Task<GetCommercialRatesResponse> GetCommercialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default) | ||
{ | ||
var queryParams = new QueryParamCollection(); | ||
queryParams.Add("currency", string.Join(",",currencies)); | ||
|
||
if (currencies != null) | ||
{ | ||
ParametersValidationHelper.CurrencyListValidation(currencies); | ||
|
||
queryParams.Add("currency", string.Join(",", currencies)); | ||
} | ||
|
||
var result = await _http.GetJsonAsync<GetCommercialRatesResponse>("/commercial", queryParams , cancellationToken).ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception); | ||
|
||
return result.Data!; | ||
return result.Data; | ||
} | ||
|
||
/// <summary> | ||
/// კომერციული კურსის დასაკონვერტირებელი მეთოდი | ||
/// Converts amount between currencies based on TBC bank's commercial exchange rates | ||
/// </summary> | ||
/// <param name="amount">(required) დასაკონვენტირებელი თანხის რაოდენობა</param> | ||
/// <param name="from">(required) ვალუტა, საიდანაც უნდა დაკონვერტირდეს</param> | ||
/// <param name="to">(required) ვალუტა, რაშიც უნდა დაკონვერტირდეს</param> | ||
/// <param name="amount">(required) Value to be converted</param> | ||
/// <param name="from">(required) Base currency from which given amount should be converted</param> | ||
/// <param name="to">(required) Target currency to which amount should be converted</param> | ||
/// <param name="cancellationToken">(optional)</param> | ||
/// <returns>აბრუნებს დაკონვერტირებული ვალუტის კურსს</returns> | ||
|
||
public async Task<ConvertCommercialRatesResponse?> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default) | ||
/// <returns>Returns convertion value of amount between currencies specified in from and to parameters based on TBC bank's commercial exchange rates</returns> | ||
public async Task<ConvertCommercialRatesResponse> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default) | ||
{ | ||
var queryParams = new QueryParamCollection(); | ||
queryParams.Add("amount", amount); | ||
queryParams.Add("from", from); | ||
queryParams.Add("to", to); | ||
ParametersValidationHelper.ConvertionParameterValidation(amount, from, to); | ||
|
||
var queryParams = new QueryParamCollection | ||
{ | ||
{ "amount", amount }, | ||
{ "from", from }, | ||
{ "to", to } | ||
}; | ||
|
||
var result = await _http.GetJsonAsync<ConvertCommercialRatesResponse>("/commercial/convert", queryParams, cancellationToken).ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception); | ||
|
||
return result.Data!; | ||
return result.Data; | ||
} | ||
|
||
#endregion | ||
|
||
#region Official Rates | ||
|
||
/// <summary> | ||
/// Gets official exchange rates for Georgian Lari | ||
/// </summary> | ||
/// <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> | ||
/// <param name="cancellationToken">(optional)</param> | ||
/// <returns>Returns list of official exchange rates</returns> | ||
public async Task<List<OfficialRate>> GetOfficialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default) | ||
{ | ||
var queryParams = new QueryParamCollection(); | ||
|
||
if (currencies != null) | ||
{ | ||
ParametersValidationHelper.CurrencyListValidation(currencies); | ||
|
||
queryParams.Add("currency", string.Join(",", currencies)); | ||
} | ||
|
||
var result = await _http.GetJsonAsync<List<OfficialRate>>("/nbg", queryParams, cancellationToken).ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception); | ||
|
||
return result.Data; | ||
} | ||
|
||
#region Official Rates | ||
/// <summary> | ||
/// ოფიციალური კურსების დასაბრუნებელი მეთოდი | ||
/// Gets official exchange rates for Georgian Lari by specific date | ||
/// </summary> | ||
/// <param name="currencies">(optional) ვალუტების მასივი, რომლებიც უნდა დაბრუნდეს(ამ პარამეტრის არგადაცემის შემთხვევაში აბრუნებს ყველა ვალუტას)</param> | ||
/// <param name="date">Parameter for getting official rates for specific date. Date should be passed in YYYY-MM-dd format</param> | ||
/// <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> | ||
/// <param name="cancellationToken">(optional)</param> | ||
/// <returns>აბრუნებს ყველა ვალუტის ან გადაცემული ვალუტების კურსებს</returns> | ||
|
||
public async Task<List<OfficialRate>?> GetOfficialRates(string[]? currencies = null, CancellationToken cancellationToken = default) | ||
/// <returns>Returns list of official exchange rates on specific date</returns> | ||
public async Task<List<OfficialRate>> GetOfficialRatesByDate(IEnumerable<string> currencies = null, string? date = null, CancellationToken cancellationToken = default) | ||
{ | ||
var queryParams = new QueryParamCollection(); | ||
if (currencies?.Any() ?? false) | ||
|
||
if (currencies != null) | ||
{ | ||
queryParams.Add("currency", string.Join(",",currencies)); | ||
ParametersValidationHelper.CurrencyListValidation(currencies); | ||
|
||
queryParams.Add("currency", string.Join(",", currencies)); | ||
} | ||
|
||
var result = await _http.GetJsonAsync<List<OfficialRate>?>("/nbg", queryParams, cancellationToken).ConfigureAwait(false); | ||
if (!string.IsNullOrEmpty(date)) | ||
{ | ||
ParametersValidationHelper.DateFormatValidation(date); | ||
|
||
queryParams.Add("date", date); | ||
} | ||
|
||
var result = await _http.GetJsonAsync<List<OfficialRate>>("/nbg", queryParams, cancellationToken).ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception); | ||
|
||
return result.Data!; | ||
return result.Data; | ||
} | ||
|
||
/// <summary> | ||
/// ოფიციალური კურსის დასაკონვერტირებელი მეთოდი | ||
/// Converts amount between currencies based on official exchange rates | ||
/// </summary> | ||
/// <param name="amount">(required) დასაკონვენტირებელი თანხის რაოდენობა</param> | ||
/// <param name="from">(required) ვალუტა, საიდანაც უნდა დაკონვერტირდეს</param> | ||
/// <param name="to">(required) ვალუტა, რაშიც უნდა დაკონვერტირდეს</param> | ||
/// <param name="amount">(required) Value to be converted</param> | ||
/// <param name="from">(required) Base currency from which given amount should be converted</param> | ||
/// <param name="to">(required) Target currency to which amount should be converted</param> | ||
/// <param name="cancellationToken">(optional)</param> | ||
/// <returns>დაკონვერტირებული ვალუტის კურსი</returns> | ||
public async Task<ConvertOfficialRatesResponse?> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default) | ||
/// <returns>Returns convertion value of amount between currencies specified in from and to parameters based on official exchange rates</returns> | ||
public async Task<ConvertOfficialRatesResponse> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default) | ||
{ | ||
var queryParams = new QueryParamCollection(); | ||
queryParams.Add("amount", amount); | ||
queryParams.Add("from", from); | ||
queryParams.Add("to", to); | ||
ParametersValidationHelper.ConvertionParameterValidation(amount, from, to); | ||
|
||
var queryParams = new QueryParamCollection | ||
{ | ||
{ "amount", amount }, | ||
{ "from", from }, | ||
{ "to", to } | ||
}; | ||
|
||
var result = await _http.GetJsonAsync<ConvertOfficialRatesResponse>("/nbg/convert", queryParams, cancellationToken).ConfigureAwait(false); | ||
|
||
if (!result.IsSuccess) | ||
throw new OpenApiException(result.Problem?.Title ?? "Unexpected error occurred", result.Exception); | ||
|
||
return result.Data!; | ||
return result.Data; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/TBC.OpenAPI.SDK.ExchangeRates/Helpers/ParametersValidationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
using TBC.OpenAPI.SDK.Core.Exceptions; | ||
|
||
namespace TBC.OpenAPI.SDK.ExchangeRates.Helpers | ||
{ | ||
internal static class ParametersValidationHelper | ||
{ | ||
private const string CURRENCY_PATTERN = "^[A-Z]{3}$"; | ||
private const string CURRENCY_LIST_PATTERN = "^[A-Z]{3}(?:,[A-Z]{3})*$"; | ||
|
||
internal static void ConvertionParameterValidation(decimal amount, string from, string to) | ||
{ | ||
Regex currencyRegEx = new Regex(CURRENCY_PATTERN); | ||
|
||
if (string.IsNullOrEmpty(from)) | ||
throw new OpenApiException("Base currency parameter 'from' must not be empty."); | ||
|
||
if (!currencyRegEx.IsMatch(from)) | ||
throw new OpenApiException("Base currency format is invalid. Please use 3-letter currency codes."); | ||
|
||
if (string.IsNullOrEmpty(to)) | ||
throw new OpenApiException("Target currency parameter 'to' must not be empty."); | ||
|
||
if (!currencyRegEx.IsMatch(to)) | ||
throw new OpenApiException("Target currency format is invalid. Please use 3-letter currency codes."); | ||
} | ||
|
||
internal static void CurrencyFormatValidation(IEnumerable<string> currencies) | ||
{ | ||
Regex currencyRegEx = new Regex(CURRENCY_PATTERN); | ||
|
||
if (currencies.Any(x => !currencyRegEx.IsMatch(x))) | ||
throw new OpenApiException("Currency format is invalid. Please use 3-letter currency codes."); | ||
} | ||
|
||
internal static void CurrencyListValidation(IEnumerable<string> currencies) | ||
{ | ||
if (currencies.Any(x => string.IsNullOrEmpty(x))) | ||
throw new OpenApiException("List of currencies contains empty element."); | ||
|
||
CurrencyFormatValidation(currencies); | ||
} | ||
|
||
internal static void DateFormatValidation(string date) | ||
{ | ||
if (!DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out _)) | ||
throw new OpenApiException("Date format is invalid. Please use YYYY-MM-dd format."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...K.ExchangeRates/Models/CommercialRates.cs → ...DK.ExchangeRates/Models/CommercialRate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.