Skip to content
master
Switch branches/tags
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

KGSoft.TinyHttpClient

A lightweight, highly compatible .NET Standard library for simplifying the consumption of REST APIs

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.

Features

  • Encapsulation of types expected from an HttpRequest (Request and Request of T)
  • Logging capability
  • Global set and forget config (With the ability to manipulate headers on a request-by-request basis if needed)
  • Re-use of HttpClient (HttpClient pool functionality coming soon)
  • Pre-request action and async func (useful for ADAL requests where you need to get/refresh an ADAL token on every request)
  • 401 Callback action and async func (useful for token expiry situations and triggering auth/refresh flow)

Coming soon

  • Suggestions welcome!

Nuget

https://www.nuget.org/packages/KGSoft.TinyHttpClient/

Further Reading

https://www.mr-kg.com/kgsoft-tinyhttpclient-a-smarter-way-to-consume-your-apis/

Usage

Simple GET request, expecting a response of T

await Helper.GetAsync<SomeObject>("some http endpoint");

Simple POST request, not expecting an object to be returned

await Helper.PostAsync("some http endpoint", "{ some json object }");

Simple POST request, expecting a response of T

await Helper.PostAsync<SomeObject>("some http endpoint", "{ some json object }");

Pre-request Token Acquisition/Refresh Using Microsoft.IdentityModel.Clients.ActiveDirectory

/// <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");
}

For more examples, please consult the unit test project included in the repo

About

A lightweight, highly compatible .NET Standard library for simplifying the consumption of REST APIs

Topics

Resources

Releases

No releases published

Packages

No packages published

Languages