diff --git a/OpenExchangeRates/Extensions/StringBuilderExtensions.cs b/OpenExchangeRates/Extensions/StringBuilderExtensions.cs index 3ff2317..d1459ad 100644 --- a/OpenExchangeRates/Extensions/StringBuilderExtensions.cs +++ b/OpenExchangeRates/Extensions/StringBuilderExtensions.cs @@ -6,7 +6,14 @@ internal static class StringBuilderExtensions public static StringBuilder AppendNameValueBoolean(this StringBuilder sb, string name, bool? value) { ArgumentNullException.ThrowIfNull(sb); + +#if NET8_0_OR_GREATER + ArgumentException.ThrowIfNullOrWhiteSpace(name); +#elif NET7_0 + ArgumentException.ThrowIfNullOrEmpty(name); +#else ArgumentNullException.ThrowIfNull(name); +#endif return value == null ? sb : sb.Append($"&{name}={(value.Value ? "true" : "false")}"); } diff --git a/OpenExchangeRates/OpenExchangeRates.csproj b/OpenExchangeRates/OpenExchangeRates.csproj index e1a19fa..56b760a 100644 --- a/OpenExchangeRates/OpenExchangeRates.csproj +++ b/OpenExchangeRates/OpenExchangeRates.csproj @@ -1,20 +1,19 @@ - net6.0;net7.0 + net6.0;net7.0;net8.0 enable enable - latest true OpenExchangeRates.NET - 1.2.1 + 1.3.0 Ivan Stus OpenExchangeRates.NET Client for Open Exchange Rates API - Copyright © 2022 Ivan Stus + Copyright © 2024 Ivan Stus LICENSE https://github.com/ivanstus/OpenExchangeRates.NET README.md @@ -26,23 +25,20 @@ - + - + - - - True - \ - + + + - - True - \ - + + + \ No newline at end of file diff --git a/OpenExchangeRates/OpenExchangeRatesClient.cs b/OpenExchangeRates/OpenExchangeRatesClient.cs index c1e34b6..2f44c17 100644 --- a/OpenExchangeRates/OpenExchangeRatesClient.cs +++ b/OpenExchangeRates/OpenExchangeRatesClient.cs @@ -14,7 +14,7 @@ public sealed class OpenExchangeRatesClient : IDisposable private const string ApiBaseUrl = "https://openexchangerates.org/api/"; private const string DefaultCurrency = "USD"; - private static readonly JsonSerializerOptions _jsonOptions = new() + private static readonly JsonSerializerOptions JsonOptions = new() { Converters = { new JsonValueConverterApiStatus(), new JsonValueConverterDateTimeOffsetUnixSeconds() }, DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, @@ -35,7 +35,13 @@ public sealed class OpenExchangeRatesClient : IDisposable public OpenExchangeRatesClient(string appId) { +#if NET8_0_OR_GREATER + ArgumentException.ThrowIfNullOrWhiteSpace(appId); +#elif NET7_0 + ArgumentException.ThrowIfNullOrEmpty(appId); +#else ArgumentNullException.ThrowIfNull(appId); +#endif _appId = appId; } @@ -48,25 +54,32 @@ public void Dispose() public async Task ConvertAsync(string from, string to, decimal amount, bool prettyPrint = false, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentException.ThrowIfNullOrWhiteSpace(from); + ArgumentException.ThrowIfNullOrWhiteSpace(to); +#elif NET7_0 + ArgumentException.ThrowIfNullOrEmpty(from); + ArgumentException.ThrowIfNullOrEmpty(to); +#else ArgumentNullException.ThrowIfNull(from); ArgumentNullException.ThrowIfNull(to); - ArgumentNullException.ThrowIfNull(amount); +#endif if (string.Equals(from.Trim(), string.Empty, StringComparison.OrdinalIgnoreCase)) - throw new ArgumentException(null, nameof(from)); + throw new ArgumentException(message: null, nameof(from)); if (string.Equals(to.Trim(), string.Empty, StringComparison.OrdinalIgnoreCase)) - throw new ArgumentException(null, nameof(to)); + throw new ArgumentException(message: null, nameof(to)); if (amount <= decimal.Zero) - throw new ArgumentException(null, nameof(amount)); + throw new ArgumentException(message: null, nameof(amount)); var response = await _httpClient.GetAsync($"convert/{amount}/{from}/{to}?" + BuildQuery(prettyPrint: prettyPrint), cancellationToken); if (!response.IsSuccessStatusCode) throw new OpenExchangeRatesException(response.ReasonPhrase); - return await response.Content.ReadFromJsonAsync(_jsonOptions, cancellationToken); + return await response.Content.ReadFromJsonAsync(JsonOptions, cancellationToken); } public async Task?> GetCurrenciesAsync(bool prettyPrint = false, bool alternative = false, @@ -78,13 +91,19 @@ public void Dispose() if (!response.IsSuccessStatusCode) throw new OpenExchangeRatesException(response.ReasonPhrase); - return await response.Content.ReadFromJsonAsync?>(_jsonOptions, cancellationToken); + return await response.Content.ReadFromJsonAsync?>(JsonOptions, cancellationToken); } public async Task GetHistoricalRatesAsync(DateOnly date, string baseCurrency = DefaultCurrency, IEnumerable? currencies = null, bool prettyPrint = false, bool alternative = false, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentException.ThrowIfNullOrWhiteSpace(baseCurrency); +#elif NET7_0 + ArgumentException.ThrowIfNullOrEmpty(baseCurrency); +#else ArgumentNullException.ThrowIfNull(baseCurrency); +#endif return await GetHistoricalRatesAsync(date.ToDateTime(TimeOnly.MinValue), baseCurrency, currencies, prettyPrint, alternative, cancellationToken); @@ -93,10 +112,16 @@ public void Dispose() public async Task GetHistoricalRatesAsync(DateTime date, string baseCurrency = DefaultCurrency, IEnumerable? currencies = null, bool prettyPrint = false, bool alternative = false, CancellationToken cancellationToken = default) { +#if NET8_0_OR_GREATER + ArgumentException.ThrowIfNullOrWhiteSpace(baseCurrency); +#elif NET7_0 + ArgumentException.ThrowIfNullOrEmpty(baseCurrency); +#else ArgumentNullException.ThrowIfNull(baseCurrency); +#endif if (string.Equals(baseCurrency.Trim(), string.Empty, StringComparison.OrdinalIgnoreCase)) - throw new ArgumentException(null, nameof(baseCurrency)); + throw new ArgumentException(message: null, nameof(baseCurrency)); var response = await _httpClient.GetAsync( $"historical/{date:yyyy-MM-dd}.json?" + BuildQuery(baseCurrency, currencies, prettyPrint, alternative), cancellationToken); @@ -104,7 +129,7 @@ public void Dispose() if (!response.IsSuccessStatusCode) throw new OpenExchangeRatesException(response.ReasonPhrase); - return await response.Content.ReadFromJsonAsync(_jsonOptions, cancellationToken); + return await response.Content.ReadFromJsonAsync(JsonOptions, cancellationToken); } public async Task GetLatestRatesAsync(string? baseCurrency = null, IEnumerable? currencies = null, @@ -115,7 +140,7 @@ public void Dispose() if (!response.IsSuccessStatusCode) throw new OpenExchangeRatesException(response.ReasonPhrase); - return await response.Content.ReadFromJsonAsync(_jsonOptions, cancellationToken); + return await response.Content.ReadFromJsonAsync(JsonOptions, cancellationToken); } public async Task GetUsageDataAsync(bool prettyPrint = false, CancellationToken cancellationToken = default) @@ -125,7 +150,7 @@ public void Dispose() if (!response.IsSuccessStatusCode) throw new OpenExchangeRatesException(response.ReasonPhrase); - var usageResponse = await response.Content.ReadFromJsonAsync(_jsonOptions, cancellationToken); + var usageResponse = await response.Content.ReadFromJsonAsync(JsonOptions, cancellationToken); return usageResponse?.Data; }