Skip to content

Commit

Permalink
Merge pull request #5 from TBCBank/bugfix/minor-fixes
Browse files Browse the repository at this point in the history
Changed class from  public to intrenal, changed nullable parameters, …
  • Loading branch information
levangagnidze authored Jun 17, 2022
2 parents 164a3b5 + 8c9ee06 commit 112ace5
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 88 deletions.
1 change: 1 addition & 0 deletions examples/CoreApiAppExmaple/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public TestController(IExchangeRatesClient exchangeRatesClient)
public async Task<ActionResult<OfficialRate>> GetOfficialRates(CancellationToken cancellationToken = default)
{
var result = await _exchangeRatesClient.GetOfficialRates(new string[] { "EUR", "USD"},cancellationToken);
//var result = await _exchangeRatesClient.GetOfficialRatesByDate(new string[] { "EUR", "USD" }, "05-2022-01", cancellationToken);
//var result = await _exchangeRatesClient.ConvertOfficialRates(120.4M,"GEL","USD", cancellationToken);
//var result = await _exchangeRatesClient.GetCommercialRates(new string[] { "EUR", "USD" }, cancellationToken);
//var result = await _exchangeRatesClient.ConvertCommercialRate(120.5M, "GEL", "USD" , cancellationToken);
Expand Down
140 changes: 95 additions & 45 deletions src/TBC.OpenAPI.SDK.ExchangeRates/ExchangeRatesClient.cs
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static OpenApiClientFactoryBuilder AddExchangeRatesClient(this OpenApiCli

public static OpenApiClientFactoryBuilder AddExchangeRatesClient(this OpenApiClientFactoryBuilder builder,
ExchangeRatesClientOptions options,
Action<HttpClient>? configureClient = null,
Func<HttpClientHandler>? configureHttpMessageHandler = null)
Action<HttpClient> configureClient = null,
Func<HttpClientHandler> configureHttpMessageHandler = null)
{
return builder.AddClient<IExchangeRatesClient, ExchangeRatesClient, ExchangeRatesClientOptions>(options, configureClient, configureHttpMessageHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public static IServiceCollection AddExchangeRatesClient(this IServiceCollection
=> AddExchangeRatesClient(services, options, null, null);

public static IServiceCollection AddExchangeRatesClient(this IServiceCollection services, ExchangeRatesClientOptions options,
Action<HttpClient>? configureClient = null,
Func<HttpClientHandler>? configureHttpMessageHandler = null)
Action<HttpClient> configureClient = null,
Func<HttpClientHandler> configureHttpMessageHandler = null)
{
services.AddOpenApiClient<IExchangeRatesClient, ExchangeRatesClient, ExchangeRatesClientOptions>(options, configureClient, configureHttpMessageHandler);
return services;
Expand Down
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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ namespace TBC.OpenAPI.SDK.ExchangeRates
{
public interface IExchangeRatesClient : IOpenApiClient
{
Task<GetCommercialRatesResponse?> GetCommercialRates(string[] currencies, CancellationToken cancellationToken = default);
Task<ConvertCommercialRatesResponse?> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default);
Task<List<OfficialRate>?> GetOfficialRates(string[]? currencies = null, CancellationToken cancellationToken = default);
Task<ConvertOfficialRatesResponse?> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default);
Task<GetCommercialRatesResponse> GetCommercialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default);
Task<ConvertCommercialRatesResponse> ConvertCommercialRate(decimal amount, string from, string to, CancellationToken cancellationToken = default);
Task<List<OfficialRate>> GetOfficialRates(IEnumerable<string> currencies = null, CancellationToken cancellationToken = default);
Task<List<OfficialRate>> GetOfficialRatesByDate(IEnumerable<string> currencies = null, string date = null, CancellationToken cancellationToken = default);
Task<ConvertOfficialRatesResponse> ConvertOfficialRates(decimal amount, string from, string to, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
namespace TBC.OpenAPI.SDK.ExchangeRates.Models
{
/// <summary>
/// კომერციული კურსის მოდელი
/// TBC Bank's commercial exchange rate response model
/// </summary>
public class CommercialRates
public class CommercialRate
{
/// <summary>
/// ვალუტა
/// 3-digit currency code
/// </summary>
public string? Currency { get; set; }
public string Currency { get; set; }

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

/// <summary>
/// გაყიდვის კურსი
/// Rate value at which bank is selling target currency
/// </summary>
public decimal Sell { get; set; }
}
Expand Down
Loading

0 comments on commit 112ace5

Please sign in to comment.