Skip to content

Commit

Permalink
Merge pull request #39 from ondrejmyska/master
Browse files Browse the repository at this point in the history
Fix suggestion for 404 in Language
  • Loading branch information
jtwotimes authored Sep 16, 2022
2 parents 4ae5822 + 93b94f6 commit 65c462e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions PokeApiNet/Models/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class Language : NamedApiResource
/// The name of this resource listed in different languages.
/// </summary>
public List<Names> Names { get; set; }

/// <summary>
/// Is endpoint case sensitive
/// </summary>
internal new static bool IsApiEndpointCaseSensitive { get; } = true;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions PokeApiNet/Models/ResourceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public abstract class ResourceBase
/// The endpoint string for this resource
/// </summary>
public static string ApiEndpoint { get; }

/// <summary>
/// Is endpoint case sensitive
/// </summary>
public static bool IsApiEndpointCaseSensitive { get; }
}

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions PokeApiNet/PokeApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ private static ProductHeaderValue GetDefaultUserAgent()
private async Task<T> GetResourcesWithParamsAsync<T>(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<T>();
string sanitizedApiParam = isApiEndpointCaseSensitive ? apiParam : apiParam.ToLowerInvariant();
string apiEndpoint = GetApiEndpointString<T>();

return await GetAsync<T>($"{apiEndpoint}/{sanitizedApiParam}/", cancellationToken);
Expand Down Expand Up @@ -444,5 +445,11 @@ private static string GetApiEndpointString<T>()
PropertyInfo propertyInfo = typeof(T).GetProperty("ApiEndpoint", BindingFlags.Static | BindingFlags.NonPublic);
return propertyInfo.GetValue(null).ToString();
}

private static bool IsApiEndpointCaseSensitive<T>()
{
PropertyInfo propertyInfo = typeof(T).GetProperty("IsApiEndpointCaseSensitive", BindingFlags.Static | BindingFlags.NonPublic);
return propertyInfo == null ? false : (bool)propertyInfo.GetValue(null);
}
}
}

0 comments on commit 65c462e

Please sign in to comment.