Skip to content

Commit 73c8a78

Browse files
committed
feat: normalize currency string handling in ExchangeApiClient
1 parent f4ecd4e commit 73c8a78

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

Source/Ecommerce.Infrastructure/Services/Providers/Forex/ExchangeApiClient.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ public async Task<Result<decimal>> GetExchangeRateAsync(
1515
Currency toCurrency
1616
)
1717
{
18+
string to = toCurrency.ToString().ToLower();
19+
string from = fromCurrency.ToString().ToLower();
20+
1821
string baseUrl =
19-
$"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{fromCurrency}.json";
22+
$"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{from}.json";
2023

2124
using HttpClient client = new();
2225
client.BaseAddress = new Uri(baseUrl);
@@ -33,11 +36,9 @@ Currency toCurrency
3336
using JsonDocument document = JsonDocument.Parse(content);
3437
JsonElement root = document.RootElement;
3538

36-
if (root.TryGetProperty(fromCurrency.ToString(), out JsonElement fromCurrencyElement))
39+
if (root.TryGetProperty(from.ToString(), out JsonElement fromElement))
3740
{
38-
if (
39-
fromCurrencyElement.TryGetProperty(toCurrency.ToString(), out JsonElement rateElement)
40-
)
41+
if (fromElement.TryGetProperty(to.ToString(), out JsonElement rateElement))
4142
{
4243
if (rateElement.TryGetDecimal(out decimal rate))
4344
{
@@ -51,18 +52,14 @@ Currency toCurrency
5152
}
5253
else
5354
{
54-
logger.LogError(
55-
"Exchange rate for {ToCurrency} not found for {FromCurrency}.",
56-
toCurrency,
57-
fromCurrency
58-
);
59-
return InternalError.GetResult($"Exchange rate for {toCurrency} not found.");
55+
logger.LogError("Exchange rate for {to} not found for {from}.", to, from);
56+
return InternalError.GetResult($"Exchange rate for {to} not found.");
6057
}
6158
}
6259
else
6360
{
64-
logger.LogError("Exchange rates for {FromCurrency} not found.", fromCurrency);
65-
return InternalError.GetResult($"Exchange rates for {fromCurrency} not found.");
61+
logger.LogError("Exchange rates for {from} not found.", from);
62+
return InternalError.GetResult($"Exchange rates for {from} not found.");
6663
}
6764
}
6865
else

0 commit comments

Comments
 (0)