From 8e682dfb3d600924234a84647fcd5032ed434a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Sun, 20 Aug 2023 04:18:09 +0100 Subject: [PATCH] v1.2.2 - Improve `QueryParameters` to enforce a value case conversion - Fix `WormsClient`: `GetExternalId` and `GetRecordByExternalId` must send the type in lower-case --- CHANGELOG.md | 5 ++ Directory.Build.props | 2 +- SpeciesDatabaseApi/ApiToken.cs | 29 +++++++++ SpeciesDatabaseApi/ApiTokenPlacement.cs | 30 ---------- .../MarineSpecies/WormsClient.cs | 4 +- SpeciesDatabaseApi/QueryParameter.cs | 37 ++++++++++-- SpeciesDatabaseApi/QueryParameters.cs | 60 +++++++++++++------ 7 files changed, 112 insertions(+), 55 deletions(-) delete mode 100644 SpeciesDatabaseApi/ApiTokenPlacement.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4482d07..c90367b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## 20/08/2023 - v1.2.2 + +- Improve `QueryParameters` to enforce a value case conversion +- Fix `WormsClient`: `GetExternalId` and `GetRecordByExternalId` must send the type in lower-case + ## 17/08/2023 - v1.2.1 - Improve the `BaseClient` and remove the overloads for url parameters, now `QueryParameters` must be used instead to populate the parameters collection diff --git a/Directory.Build.props b/Directory.Build.props index c09d58e..db075df 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ Tiago Conceição (sn4k3) PTRTECH Copyright 2023-$([System.DateTime]::Now.ToString(`yyyy`)) © PTRTECH - 1.2.1 + 1.2.2 Queries and fetch data from species, taxon, regions and conservation database(s) to retrieve information using the provider API. $(MSBuildThisFileDirectory)images/icon.ico diff --git a/SpeciesDatabaseApi/ApiToken.cs b/SpeciesDatabaseApi/ApiToken.cs index 745323a..bca83ce 100644 --- a/SpeciesDatabaseApi/ApiToken.cs +++ b/SpeciesDatabaseApi/ApiToken.cs @@ -5,6 +5,35 @@ namespace SpeciesDatabaseApi; +/// +/// Defines where a API token will be placed within the request +/// +public enum ApiTokenPlacement +{ + /// + /// Is up to developer to insert the token manually + /// + Manual, + + /// + /// The token goes into Header
+ /// Inserted by the base client + ///
+ Header, + + /// + /// The token goes into Header.Authorization
+ /// Inserted by the base client + ///
+ HeaderAuthorization, + + /// + /// The tokes goes as a GET parameter
+ /// Inserted by the base client + ///
+ Get, +} + /// /// Represents an API token to send with the request /// diff --git a/SpeciesDatabaseApi/ApiTokenPlacement.cs b/SpeciesDatabaseApi/ApiTokenPlacement.cs deleted file mode 100644 index ab05a7c..0000000 --- a/SpeciesDatabaseApi/ApiTokenPlacement.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace SpeciesDatabaseApi; - -/// -/// Defines where a API token will be placed within the request -/// -public enum ApiTokenPlacement -{ - /// - /// Is up to developer to insert the token manually - /// - Manual, - - /// - /// The token goes into Header
- /// Inserted by the base client - ///
- Header, - - /// - /// The token goes into Header.Authorization
- /// Inserted by the base client - ///
- HeaderAuthorization, - - /// - /// The tokes goes as a GET parameter
- /// Inserted by the base client - ///
- Get, -} \ No newline at end of file diff --git a/SpeciesDatabaseApi/MarineSpecies/WormsClient.cs b/SpeciesDatabaseApi/MarineSpecies/WormsClient.cs index 85b553b..74b76cb 100644 --- a/SpeciesDatabaseApi/MarineSpecies/WormsClient.cs +++ b/SpeciesDatabaseApi/MarineSpecies/WormsClient.cs @@ -116,7 +116,7 @@ public WormsClient(HttpClient? httpClient = null) : base(DefaultApiAddress, http /// public Task GetExternalId(int aphiaId, ExternalIdentifierTypeEnum type, CancellationToken token = default) { - var parameters = new QueryParameters("type", type); + var parameters = new QueryParameters("type", type, valueCase:QueryParameterCase.Lower); return GetJsonAsync($"AphiaExternalIDByAphiaID/{aphiaId}", parameters, token); } @@ -129,7 +129,7 @@ public WormsClient(HttpClient? httpClient = null) : base(DefaultApiAddress, http /// public Task GetRecordByExternalId(string externalId, ExternalIdentifierTypeEnum type, CancellationToken token = default) { - var parameters = new QueryParameters("type", type); + var parameters = new QueryParameters("type", type, valueCase: QueryParameterCase.Lower); return GetJsonAsync($"AphiaRecordByExternalID/{externalId}", parameters, token); } #endregion diff --git a/SpeciesDatabaseApi/QueryParameter.cs b/SpeciesDatabaseApi/QueryParameter.cs index 76204eb..9aad9a0 100644 --- a/SpeciesDatabaseApi/QueryParameter.cs +++ b/SpeciesDatabaseApi/QueryParameter.cs @@ -18,19 +18,32 @@ public class QueryParameter : IEquatable /// public object? Value { get; init; } - public QueryParameter(string key, object? value) + /// + /// Enforce a case conversion + /// + public QueryParameterCase ValueCase { get; init; } + + public QueryParameter(string key, object? value, QueryParameterCase valueCase = QueryParameterCase.None) { Key = key; Value = value; + ValueCase = valueCase; } - public QueryParameter(KeyValuePair keyValuePair) : this(keyValuePair.Key, keyValuePair.Value) + public QueryParameter(KeyValuePair keyValuePair, QueryParameterCase valueCase = QueryParameterCase.None) : this(keyValuePair.Key, keyValuePair.Value, valueCase) { } public override string ToString() { - return $"{Key}={Value}"; + var value = ValueCase switch + { + QueryParameterCase.None => Value?.ToString(), + QueryParameterCase.Lower => Value?.ToString()?.ToLower(), + QueryParameterCase.Upper => Value?.ToString()?.ToUpper(), + _ => throw new ArgumentOutOfRangeException() + }; + return $"{Key}={value}"; } public string? ToUrlString() @@ -61,7 +74,15 @@ public override string ToString() foreach (var arrayItem in items) { if (sb.Length > 0) sb.Append('&'); - sb.Append($"{Uri.EscapeDataString(Key)}={Uri.EscapeDataString(arrayItem)}"); + + value = ValueCase switch + { + QueryParameterCase.None => arrayItem, + QueryParameterCase.Lower => arrayItem.ToLower(), + QueryParameterCase.Upper => arrayItem.ToUpper(), + _ => throw new ArgumentOutOfRangeException() + }; + sb.Append($"{Uri.EscapeDataString(Key)}={Uri.EscapeDataString(value)}"); } return sb.ToString(); @@ -95,6 +116,14 @@ public override string ToString() break; } + value = ValueCase switch + { + QueryParameterCase.None => value, + QueryParameterCase.Lower => value.ToLower(), + QueryParameterCase.Upper => value.ToUpper(), + _ => throw new ArgumentOutOfRangeException() + }; + return $"{Uri.EscapeDataString(Key)}={Uri.EscapeDataString(value)}"; } diff --git a/SpeciesDatabaseApi/QueryParameters.cs b/SpeciesDatabaseApi/QueryParameters.cs index d7d66ff..cb20610 100644 --- a/SpeciesDatabaseApi/QueryParameters.cs +++ b/SpeciesDatabaseApi/QueryParameters.cs @@ -15,6 +15,24 @@ public enum QueryParametersSpecialType ClassPropertiesReflection } +public enum QueryParameterCase +{ + /// + /// As set, do not force case + /// + None, + + /// + /// Force lower case + /// + Lower, + + /// + /// Force upper case + /// + Upper, +} + /// /// Handles query parameters and build the string, avoiding any null value /// @@ -38,9 +56,10 @@ public QueryParameters(int capacity) : base(capacity) /// /// /// True to only add distinct keys - public QueryParameters(string key, object? value, bool distinct = false) : this(1) + /// Enforce a value case conversion + public QueryParameters(string key, object? value, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) : this(1) { - Add(key, value, distinct); + Add(key, value, distinct, valueCase); } /// @@ -48,9 +67,10 @@ public QueryParameters(string key, object? value, bool distinct = false) : this( /// /// /// True to only add distinct keys - public QueryParameters(KeyValuePair keyValuePair, bool distinct = false) : this(1) + /// Enforce a value case conversion + public QueryParameters(KeyValuePair keyValuePair, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) : this(1) { - Add(keyValuePair, distinct); + Add(keyValuePair, distinct, valueCase); } /// @@ -58,9 +78,10 @@ public QueryParameters(KeyValuePair keyValuePair, bool distinct /// /// /// True to only add distinct keys - public QueryParameters(IEnumerable> list, bool distinct = false) : this(list.Count()) + /// Enforce a value case conversion + public QueryParameters(IEnumerable> list, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) : this(list.Count()) { - Add(list, distinct); + Add(list, distinct, valueCase); } @@ -69,9 +90,10 @@ public QueryParameters(IEnumerable> list, bool dis /// /// /// True to only add distinct keys - public QueryParameters(IReadOnlyDictionary dictionary, bool distinct = false) : this(dictionary.Count) + /// Enforce a value case conversion + public QueryParameters(IReadOnlyDictionary dictionary, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) : this(dictionary.Count) { - Add(dictionary, distinct); + Add(dictionary, distinct, valueCase); } @@ -93,11 +115,12 @@ public QueryParameters(QueryParametersSpecialType specialType, object? classObj, /// /// /// True to only add distinct keys - public void Add(string key, object? value, bool distinct = false) + /// Enforce a value case conversion + public void Add(string key, object? value, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) { if (value is null) return; if (distinct && this.Any(queryParameter => queryParameter.Key == key)) return; - Add(new QueryParameter(key, value)); + Add(new QueryParameter(key, value, valueCase)); } /// @@ -105,11 +128,10 @@ public void Add(string key, object? value, bool distinct = false) /// /// /// True to only add distinct keys - public void Add(KeyValuePair keyValuePair, bool distinct = false) + /// Enforce a value case conversion + public void Add(KeyValuePair keyValuePair, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) { - if (keyValuePair.Value is null) return; - if (distinct && this.Any(queryParameter => queryParameter.Key == keyValuePair.Key)) return; - Add(new QueryParameter(keyValuePair)); + Add(keyValuePair.Key, keyValuePair.Value, distinct, valueCase); } /// @@ -117,11 +139,12 @@ public void Add(KeyValuePair keyValuePair, bool distinct = fals /// /// /// True to only add distinct keys - public void Add(IEnumerable> list, bool distinct = false) + /// Enforce a value case conversion + public void Add(IEnumerable> list, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) { foreach (var keyValuePair in list) { - Add(keyValuePair, distinct); + Add(keyValuePair, distinct, valueCase); } } @@ -130,11 +153,12 @@ public void Add(IEnumerable> list, bool distinct = /// /// /// True to only add distinct keys - public void Add(IReadOnlyDictionary dictionary, bool distinct = false) + /// Enforce a value case conversion + public void Add(IReadOnlyDictionary dictionary, bool distinct = false, QueryParameterCase valueCase = QueryParameterCase.None) { foreach (var keyValuePair in dictionary) { - Add(keyValuePair, distinct); + Add(keyValuePair, distinct, valueCase); } }