Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix suggestion for 404 in Language #39

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}