Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.63 KB

README.md

File metadata and controls

49 lines (35 loc) · 1.63 KB

SFA.DAS.Http

badge

NuGet Badge

Library containing Http related classes such as base classes for connecting to Api's.

Http

Example Usage

Create a custom Api Client class using the RestHttpClient class and HttpClientBuilder.

public class Example
    {
        public async Task CustomApiClientExample()
        {
            HttpClient httpClient = new HttpClientBuilder()
                .WithDefaultHeaders()
                .WithBearerAuthorisationHeader(new JwtBearerTokenGenerator(new MyClientConfiguration()))
                .Build();

            var restApiClient = new RestHttpClient(httpClient);

            var result = await restApiClient.Get("http://some-host/some-resource/");
        }

        private class MyClientConfiguration : IJwtClientConfiguration
        { 
            public string ClientToken { get => "SomeTokenValue"; }
        }        
    }

HttpClientBuilder

This class builds a HttpClient with specified configuration:

WithDefaultHeaders()

Adds default headers such as Accept header.

WithHandler(DelegatingHandler)

Adds a handler that inherits from System.Net.Http.DelegatingHandler

WithBearerTokenHeader(IGenerateBearerToken)

Adds a Bearer header with the value provided by the implementation of IGenerateBearerToken interface that is passed in. Currently two generators are provided:

  • JwtBearerTokenGenerator
  • AzureADBearerTokenGenerator