Skip to content

Commit

Permalink
Upgrade to .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanstus committed Mar 2, 2024
1 parent 5e14048 commit f4c39a8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
7 changes: 7 additions & 0 deletions OpenExchangeRates/Extensions/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")}");
}
Expand Down
26 changes: 11 additions & 15 deletions OpenExchangeRates/OpenExchangeRates.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup>
<PackageId>OpenExchangeRates.NET</PackageId>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<Authors>Ivan Stus</Authors>
<Title>OpenExchangeRates.NET</Title>
<Description>Client for Open Exchange Rates API</Description>
<Copyright>Copyright © 2022 Ivan Stus</Copyright>
<Copyright>Copyright © 2024 Ivan Stus</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/ivanstus/OpenExchangeRates.NET</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -26,23 +25,20 @@
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework) == 'net6.0'">
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="6.0.1" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net7.0'">
<PackageReference Include="System.Net.Http.Json" Version="7.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
</ItemGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<ItemGroup Condition="$(TargetFramework) == 'net8.0'">
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
47 changes: 36 additions & 11 deletions OpenExchangeRates/OpenExchangeRatesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand All @@ -48,25 +54,32 @@ public void Dispose()
public async Task<ConvertResponse?> 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<ConvertResponse>(_jsonOptions, cancellationToken);
return await response.Content.ReadFromJsonAsync<ConvertResponse>(JsonOptions, cancellationToken);
}

public async Task<IReadOnlyDictionary<string, string>?> GetCurrenciesAsync(bool prettyPrint = false, bool alternative = false,
Expand All @@ -78,13 +91,19 @@ public void Dispose()
if (!response.IsSuccessStatusCode)
throw new OpenExchangeRatesException(response.ReasonPhrase);

return await response.Content.ReadFromJsonAsync<IReadOnlyDictionary<string, string>?>(_jsonOptions, cancellationToken);
return await response.Content.ReadFromJsonAsync<IReadOnlyDictionary<string, string>?>(JsonOptions, cancellationToken);
}

public async Task<RatesResponse?> GetHistoricalRatesAsync(DateOnly date, string baseCurrency = DefaultCurrency,
IEnumerable<string>? 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);
Expand All @@ -93,18 +112,24 @@ public void Dispose()
public async Task<RatesResponse?> GetHistoricalRatesAsync(DateTime date, string baseCurrency = DefaultCurrency,
IEnumerable<string>? 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);

if (!response.IsSuccessStatusCode)
throw new OpenExchangeRatesException(response.ReasonPhrase);

return await response.Content.ReadFromJsonAsync<RatesResponse>(_jsonOptions, cancellationToken);
return await response.Content.ReadFromJsonAsync<RatesResponse>(JsonOptions, cancellationToken);
}

public async Task<RatesResponse?> GetLatestRatesAsync(string? baseCurrency = null, IEnumerable<string>? currencies = null,
Expand All @@ -115,7 +140,7 @@ public void Dispose()
if (!response.IsSuccessStatusCode)
throw new OpenExchangeRatesException(response.ReasonPhrase);

return await response.Content.ReadFromJsonAsync<RatesResponse>(_jsonOptions, cancellationToken);
return await response.Content.ReadFromJsonAsync<RatesResponse>(JsonOptions, cancellationToken);
}

public async Task<UsageData?> GetUsageDataAsync(bool prettyPrint = false, CancellationToken cancellationToken = default)
Expand All @@ -125,7 +150,7 @@ public void Dispose()
if (!response.IsSuccessStatusCode)
throw new OpenExchangeRatesException(response.ReasonPhrase);

var usageResponse = await response.Content.ReadFromJsonAsync<UsageResponse>(_jsonOptions, cancellationToken);
var usageResponse = await response.Content.ReadFromJsonAsync<UsageResponse>(JsonOptions, cancellationToken);

return usageResponse?.Data;
}
Expand Down

0 comments on commit f4c39a8

Please sign in to comment.