Skip to content

Latest commit

 

History

History
110 lines (85 loc) · 5.17 KB

README.md

File metadata and controls

110 lines (85 loc) · 5.17 KB

Solid.Http.Core

Solid.Http.Core is where most of the included extension methods are located. It can be used on it's own if you don't need to serialize/deserialize content. The Http request/response is done using HttpClient which is provided by the IHttpClientFactory in Microsoft.Extensions.Http.

> dotnet add package Solid.Http.Core

Initialization

You can do basic intialization, and Solid.Http.Core will work just fine.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSolidHttpCore();
}

Global event handlers

You can also register event handlers during initialization.

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddSolidHttpCore(builder => 
        {
            builder
                .Configure(options =>
                {
                    options.OnClientCreated((services, client) =>
                    {
                         // your client created handler here.
                    });
                    options.OnRequestCreated((services, request) =>
                    {
                         // your request created handler here.
                    });
                    options.OnHttpRequest(async (services, httpRequest) =>
                    {
                        // your http request handler here.
                    });
                    options.OnHttpResponse(async (services, httpResponse) =>
                    {
                        // your http response handler here.
                    });
                })
            ;
        })
    ;
}

Important interfaces

Solid.Http.Core includes some interfaces that you will, well, interface with.

ISolidHttpClientFactory

ISolidHttpClientFactory is where you request an ISolidHttpClient so that you can perform http request fluently and asynchronously. The interface has two methods.

ISolidHttpClientFactory.Create

Create() does what it says. It creates an ISolidHttpClient and provides it to you. This client expects a full and absolute url when it is performing an HTTP request.

ISolidHttpClientFactory.CreateWithBaseAddress

CreateWithBaseAddress(Uri baseAddress) creates an ISolidHttpClient that has a base address. Requests made using this client should be relative.

About relative addresses (a bit of a tangent)

Some people don't realize the difference between /posts and posts as relative addresses.

/posts will go to the root of the baseAddress that it's used on. So if you have a base address of https://mysite.com/api/, a relative address of /posts would point to https://mysite.com/posts, while posts would point to https://mysite.com/api/posts.

ISolidHttpClient

ISolidHttpClient is what you use to create an ISolidHttpRequest, which is the interface that has the most extension methods for the fluent syntax.

ISolidHttpClient.BaseAddress

BaseAddress is a readonly property that is populated when you create the client using ISolidHttpClientFactory.CreateWithBaseAddress.

ISolidHttpClient.OnRequestCreated

OnRequestCreated is where the event handlers are registered for when an ISolidHttpRequest is created. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.

ISolidHttpClient.PerformRequestAsync

PerformRequestAsync is what creates an ISolidHttpRequest and starts the fluent syntax of the http request. This method is what the request method extension methods call internally, i.e. GetAsync, PostAsync etc.

ISolidHttpRequest

ISolidHttpRequest is an awaitable interface which performs an http request and returns an HttpResponseMessage.

ISolidHttpRequest.Services

This is the root IServiceProvider and is passed to the event handlers that are run on the ISolidHttpRequest. It can also be used in extension methods on ISolidHttpRequest.

ISolidHttpRequest.Context

The is a simple hashtable of items that can be used in extension methods if you need context between multiple methods.

ISolidHttpRequest.CancellationToken

The cancellation token to be used by HttpClient.

ISolidHttpRequest.BaseRequest

This is the HttpRequestMessage that will be passed to HttpClient and sent over the wire.

ISolidHttpRequest.BaseResponse

This will be populated once the ISolidHttpRequest has been awaited.

ISolidHttpRequest.OnHttpRequest

OnHttpRequest is where the event handlers are registered that run just before the http request is sent. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.

ISolidHttpRequest.OnHttpResponse

OnHttpResponse is where the event handlers are registered that are run directly after a response has be received. They will be run along with any event handlers that were registered when Solid.Http.Core was added to the IServiceCollection.

ISolidHttpRequest.As

As is a method for deserialization of the response content body.

ISolidHttpRequest.GetAwaiter

GetAwaiter is what enables the ISolidHttpRequest to be awaited. Once the ISolidHttpRequest is awaited, it will return a HttpResponseMessage.