Core Files
These are the core files that all Api Clients will be downloaded with. They are just the bare minimum assortment of interfaces, classes, and extension methods that are needed to make a clean C# Api Client. It is recommended to look through each of these (class/function/..) and delete any you don't need. Especially with the extension methods, I had to ensure every case was covered, so there is a good chance there are a lot of extension methods that you may not need. Feel free to keep them if you find them useful in development though.
This is just one extension method that is used to add query parameters to a string.
public static class QueryHelpers
{
public static string AddQueryString(
string uri,
IEnumerable<KeyValuePair<string, string?>> queryString)
{
ArgumentNullException.ThrowIfNull(uri);
ArgumentNullException.ThrowIfNull(queryString);
var anchorIndex = uri.IndexOf('#');
var uriToBeAppended = uri.AsSpan();
var anchorText = ReadOnlySpan<char>.Empty;
// If there is an anchor, then the query string must be inserted before its first occurrence.
if (anchorIndex != -1)
{
anchorText = uriToBeAppended.Slice(anchorIndex);
uriToBeAppended = uriToBeAppended.Slice(0, anchorIndex);
}
var queryIndex = uriToBeAppended.IndexOf('?');
var hasQuery = queryIndex != -1;
var sb = new StringBuilder();
sb.Append(uriToBeAppended);
foreach (var parameter in queryString)
{
if (parameter.Value == null)
{
continue;
}
sb.Append(hasQuery ? '&' : '?');
sb.Append(UrlEncoder.Default.Encode(parameter.Key));
sb.Append('=');
sb.Append(UrlEncoder.Default.Encode(parameter.Value));
hasQuery = true;
}
sb.Append(anchorText);
return sb.ToString();
}
}