@@ -14,7 +14,7 @@ public sealed class OpenExchangeRatesClient : IDisposable
1414 private const string ApiBaseUrl = "https://openexchangerates.org/api/" ;
1515 private const string DefaultCurrency = "USD" ;
1616
17- private static readonly JsonSerializerOptions _jsonOptions = new ( )
17+ private static readonly JsonSerializerOptions JsonOptions = new ( )
1818 {
1919 Converters = { new JsonValueConverterApiStatus ( ) , new JsonValueConverterDateTimeOffsetUnixSeconds ( ) } ,
2020 DictionaryKeyPolicy = JsonNamingPolicy . CamelCase ,
@@ -35,7 +35,13 @@ public sealed class OpenExchangeRatesClient : IDisposable
3535
3636 public OpenExchangeRatesClient ( string appId )
3737 {
38+ #if NET8_0_OR_GREATER
39+ ArgumentException . ThrowIfNullOrWhiteSpace ( appId ) ;
40+ #elif NET7_0
41+ ArgumentException . ThrowIfNullOrEmpty ( appId ) ;
42+ #else
3843 ArgumentNullException . ThrowIfNull ( appId ) ;
44+ #endif
3945
4046 _appId = appId ;
4147 }
@@ -48,25 +54,32 @@ public void Dispose()
4854 public async Task < ConvertResponse ? > ConvertAsync ( string from , string to , decimal amount , bool prettyPrint = false ,
4955 CancellationToken cancellationToken = default )
5056 {
57+ #if NET8_0_OR_GREATER
58+ ArgumentException . ThrowIfNullOrWhiteSpace ( from ) ;
59+ ArgumentException . ThrowIfNullOrWhiteSpace ( to ) ;
60+ #elif NET7_0
61+ ArgumentException . ThrowIfNullOrEmpty ( from ) ;
62+ ArgumentException . ThrowIfNullOrEmpty ( to ) ;
63+ #else
5164 ArgumentNullException . ThrowIfNull ( from ) ;
5265 ArgumentNullException . ThrowIfNull ( to ) ;
53- ArgumentNullException . ThrowIfNull ( amount ) ;
66+ #endif
5467
5568 if ( string . Equals ( from . Trim ( ) , string . Empty , StringComparison . OrdinalIgnoreCase ) )
56- throw new ArgumentException ( null , nameof ( from ) ) ;
69+ throw new ArgumentException ( message : null , nameof ( from ) ) ;
5770
5871 if ( string . Equals ( to . Trim ( ) , string . Empty , StringComparison . OrdinalIgnoreCase ) )
59- throw new ArgumentException ( null , nameof ( to ) ) ;
72+ throw new ArgumentException ( message : null , nameof ( to ) ) ;
6073
6174 if ( amount <= decimal . Zero )
62- throw new ArgumentException ( null , nameof ( amount ) ) ;
75+ throw new ArgumentException ( message : null , nameof ( amount ) ) ;
6376
6477 var response = await _httpClient . GetAsync ( $ "convert/{ amount } /{ from } /{ to } ?" + BuildQuery ( prettyPrint : prettyPrint ) , cancellationToken ) ;
6578
6679 if ( ! response . IsSuccessStatusCode )
6780 throw new OpenExchangeRatesException ( response . ReasonPhrase ) ;
6881
69- return await response . Content . ReadFromJsonAsync < ConvertResponse > ( _jsonOptions , cancellationToken ) ;
82+ return await response . Content . ReadFromJsonAsync < ConvertResponse > ( JsonOptions , cancellationToken ) ;
7083 }
7184
7285 public async Task < IReadOnlyDictionary < string , string > ? > GetCurrenciesAsync ( bool prettyPrint = false , bool alternative = false ,
@@ -78,13 +91,19 @@ public void Dispose()
7891 if ( ! response . IsSuccessStatusCode )
7992 throw new OpenExchangeRatesException ( response . ReasonPhrase ) ;
8093
81- return await response . Content . ReadFromJsonAsync < IReadOnlyDictionary < string , string > ? > ( _jsonOptions , cancellationToken ) ;
94+ return await response . Content . ReadFromJsonAsync < IReadOnlyDictionary < string , string > ? > ( JsonOptions , cancellationToken ) ;
8295 }
8396
8497 public async Task < RatesResponse ? > GetHistoricalRatesAsync ( DateOnly date , string baseCurrency = DefaultCurrency ,
8598 IEnumerable < string > ? currencies = null , bool prettyPrint = false , bool alternative = false , CancellationToken cancellationToken = default )
8699 {
100+ #if NET8_0_OR_GREATER
101+ ArgumentException . ThrowIfNullOrWhiteSpace ( baseCurrency ) ;
102+ #elif NET7_0
103+ ArgumentException . ThrowIfNullOrEmpty ( baseCurrency ) ;
104+ #else
87105 ArgumentNullException . ThrowIfNull ( baseCurrency ) ;
106+ #endif
88107
89108 return await GetHistoricalRatesAsync ( date . ToDateTime ( TimeOnly . MinValue ) , baseCurrency , currencies , prettyPrint , alternative ,
90109 cancellationToken ) ;
@@ -93,18 +112,24 @@ public void Dispose()
93112 public async Task < RatesResponse ? > GetHistoricalRatesAsync ( DateTime date , string baseCurrency = DefaultCurrency ,
94113 IEnumerable < string > ? currencies = null , bool prettyPrint = false , bool alternative = false , CancellationToken cancellationToken = default )
95114 {
115+ #if NET8_0_OR_GREATER
116+ ArgumentException . ThrowIfNullOrWhiteSpace ( baseCurrency ) ;
117+ #elif NET7_0
118+ ArgumentException . ThrowIfNullOrEmpty ( baseCurrency ) ;
119+ #else
96120 ArgumentNullException . ThrowIfNull ( baseCurrency ) ;
121+ #endif
97122
98123 if ( string . Equals ( baseCurrency . Trim ( ) , string . Empty , StringComparison . OrdinalIgnoreCase ) )
99- throw new ArgumentException ( null , nameof ( baseCurrency ) ) ;
124+ throw new ArgumentException ( message : null , nameof ( baseCurrency ) ) ;
100125
101126 var response = await _httpClient . GetAsync (
102127 $ "historical/{ date : yyyy-MM-dd} .json?" + BuildQuery ( baseCurrency , currencies , prettyPrint , alternative ) , cancellationToken ) ;
103128
104129 if ( ! response . IsSuccessStatusCode )
105130 throw new OpenExchangeRatesException ( response . ReasonPhrase ) ;
106131
107- return await response . Content . ReadFromJsonAsync < RatesResponse > ( _jsonOptions , cancellationToken ) ;
132+ return await response . Content . ReadFromJsonAsync < RatesResponse > ( JsonOptions , cancellationToken ) ;
108133 }
109134
110135 public async Task < RatesResponse ? > GetLatestRatesAsync ( string ? baseCurrency = null , IEnumerable < string > ? currencies = null ,
@@ -115,7 +140,7 @@ public void Dispose()
115140 if ( ! response . IsSuccessStatusCode )
116141 throw new OpenExchangeRatesException ( response . ReasonPhrase ) ;
117142
118- return await response . Content . ReadFromJsonAsync < RatesResponse > ( _jsonOptions , cancellationToken ) ;
143+ return await response . Content . ReadFromJsonAsync < RatesResponse > ( JsonOptions , cancellationToken ) ;
119144 }
120145
121146 public async Task < UsageData ? > GetUsageDataAsync ( bool prettyPrint = false , CancellationToken cancellationToken = default )
@@ -125,7 +150,7 @@ public void Dispose()
125150 if ( ! response . IsSuccessStatusCode )
126151 throw new OpenExchangeRatesException ( response . ReasonPhrase ) ;
127152
128- var usageResponse = await response . Content . ReadFromJsonAsync < UsageResponse > ( _jsonOptions , cancellationToken ) ;
153+ var usageResponse = await response . Content . ReadFromJsonAsync < UsageResponse > ( JsonOptions , cancellationToken ) ;
129154
130155 return usageResponse ? . Data ;
131156 }
0 commit comments