A lightweight, highly compatible .NET library for simplifying the consumption of REST APIs
$ dotnet add package KGSoft.TinyHttpClientA lightweight, highly compatible .NET library for simplifying the consumption of REST APIs (via both static client or Fluent API)
This library was created out if necessity, as it contains code we seemed to be duplicating throughout every Xamarin project we wrote. We have tried to encapsulate deserialization and the reading of HttpResponseMessages into this handy library. It has since grown into a handy library for server-side API consumption as well, hence the newly added v2.x.x Fluent API feature.
This library has two modes of use, each suited to different implementations. Mode 1 (Static Client) is better suited to client-side (ie. mobile/desktop apps) where you have a single static context for headers / token refreshing etc. Mode 2 (Fluent API) is better suited to server-side API consumption via the new Fluent API implementation.
https://www.nuget.org/packages/KGSoft.TinyHttpClient/
https://www.mr-kg.com/kgsoft-tinyhttpclient-a-smarter-way-to-consume-your-apis/
await Helper.GetAsync<SomeObject>("some http endpoint");
var response = await new HttpRequestBuilder()
.Get("some http endpoint")
.MakeRequestAsync<SomeObject>();
await Helper.PostAsync("some http endpoint", "{ some json object }");
var response = await new HttpRequestBuilder()
.Post("some http endpoint")
.AddBody(new data() { some object })
.MakeRequestAsync();
await Helper.PostAsync<SomeObject>("some http endpoint", "{ some json object }");
var response = await new HttpRequestBuilder()
.Post("some http endpoint")
.AddBody(new data() { some object })
.MakeRequestAsync<SomeObject>();
/// <summary>
/// Our method to get and set our ADAL AccessToken
/// </summary>
/// <returns></returns>
private async Task GetAuthBearerToken()
{
var authority = "";
var resource = "";
var clientId = "";
var redirectUri = "";
var extraQueryParams = "";
var authContext = new AuthenticationContext(authority);
PlatformParameters platform = new PlatformParameters(PromptBehavior.Auto);
// Call to the ADAL to get a token
var result = await authContext.AcquireTokenAsync(
resource,
clientId,
new Uri(redirectUri),
platform,
UserIdentifier.AnyUser,
extraQueryParams);
if (result != null) // We get a token, and we set it as the AuthHeader for our HttpClient in the global config
HttpConfig.DefaultAuthHeader = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
}
[TestMethod]
public async Task Test_PreAuth()
{
// Set the PreRequestAuth Function here and forget about it
HttpConfig.PreRequestAuthAsyncFunc = GetAuthBearerToken;
var response = await Helper.GetAsync("some protected API");
}