From 93b94f67ca741b6f73818fe20eedec34133a8811 Mon Sep 17 00:00:00 2001 From: omyska Date: Fri, 16 Sep 2022 21:09:36 +0200 Subject: [PATCH] Fix suggestion for 404 in Language --- PokeApiNet/Models/Common.cs | 5 +++++ PokeApiNet/Models/ResourceBase.cs | 5 +++++ PokeApiNet/PokeApiClient.cs | 11 +++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/PokeApiNet/Models/Common.cs b/PokeApiNet/Models/Common.cs index 49e99df..f96fe0a 100644 --- a/PokeApiNet/Models/Common.cs +++ b/PokeApiNet/Models/Common.cs @@ -41,6 +41,11 @@ public class Language : NamedApiResource /// The name of this resource listed in different languages. /// public List Names { get; set; } + + /// + /// Is endpoint case sensitive + /// + internal new static bool IsApiEndpointCaseSensitive { get; } = true; } /// diff --git a/PokeApiNet/Models/ResourceBase.cs b/PokeApiNet/Models/ResourceBase.cs index 6a8e895..a6ffc46 100644 --- a/PokeApiNet/Models/ResourceBase.cs +++ b/PokeApiNet/Models/ResourceBase.cs @@ -15,6 +15,11 @@ public abstract class ResourceBase /// The endpoint string for this resource /// public static string ApiEndpoint { get; } + + /// + /// Is endpoint case sensitive + /// + public static bool IsApiEndpointCaseSensitive { get; } } /// diff --git a/PokeApiNet/PokeApiClient.cs b/PokeApiNet/PokeApiClient.cs index a4a7e9e..aa2a458 100644 --- a/PokeApiNet/PokeApiClient.cs +++ b/PokeApiNet/PokeApiClient.cs @@ -109,8 +109,9 @@ private static ProductHeaderValue GetDefaultUserAgent() private async Task GetResourcesWithParamsAsync(string apiParam, CancellationToken cancellationToken) where T : ResourceBase { - // lowercase the resource name as the API doesn't recognize upper case and lower case as the same - string sanitizedApiParam = apiParam.ToLowerInvariant(); + // check for case sensitive API endpoint + bool isApiEndpointCaseSensitive = IsApiEndpointCaseSensitive(); + string sanitizedApiParam = isApiEndpointCaseSensitive ? apiParam : apiParam.ToLowerInvariant(); string apiEndpoint = GetApiEndpointString(); return await GetAsync($"{apiEndpoint}/{sanitizedApiParam}/", cancellationToken); @@ -444,5 +445,11 @@ private static string GetApiEndpointString() PropertyInfo propertyInfo = typeof(T).GetProperty("ApiEndpoint", BindingFlags.Static | BindingFlags.NonPublic); return propertyInfo.GetValue(null).ToString(); } + + private static bool IsApiEndpointCaseSensitive() + { + PropertyInfo propertyInfo = typeof(T).GetProperty("IsApiEndpointCaseSensitive", BindingFlags.Static | BindingFlags.NonPublic); + return propertyInfo == null ? false : (bool)propertyInfo.GetValue(null); + } } }