Getting started
Getting started with Geko.HttpClientService
is rather easy, you only need three things:
- Install the nuget package Geko.HttpClientService
- Optionally, provide the options to request an access token in the
appsettings.json
- Register the service in
Startup.cs
It's a nuget package!
Install the Geko.HttpClientService nuget package, using any of your favorite ways.
Optionally, set the IdentityServer4 Access Token Request Options
Add the IdentityServer4 Access Token Request Options to your appsettings.json
(the configuration section should always be or end with ClientCredentialsOptions
):
"ClientCredentialsOptions": {
"Address": "https://demo.identityserver.io/connect/token",
"ClientId": "m2m",
"ClientSecret": "secret",
"Scope": "api"
}
// The values above are part of the demo offered in https://demo.identityserver.io/
Register the service
Register the service In StartUp.cs
in ConfigureServices(IServiceCollection services)
:
services.AddHttpClientService()
//Optional, set it if you have ClientCredentialsOptions or PasswordOptions
.Configure<ClientCredentialsOptions>(Configuration.GetSection(nameof(ClientCredentialsOptions)));
You are done
Request the IHttpClientServiceFactory
wherever you want to make the authenticated requests:
using Geko.HttpClientService.Extensions;
[ApiController]
[Route("customers")]
public class CustomerController : ControllerBase
{
//Request the IHttpClientServiceFactory instance in your controller or service
private readonly IHttpClientServiceFactory _requestServiceFactory;
public CustomerController(IHttpClientServiceFactory requestServiceFactory){
_requestServiceFactory = requestServiceFactory;
}
[HttpGet]
public async Task<IActionResult> Get(){
//Make the request
var responseObject = await _requestServiceFactory
//Create a instance of the service
.CreateHttpClientService()
//GET and deserialize the response body to IEnumerable<Customers>
.GetAsync<IEnumerable<Customers>>("https://api/customers");
//Do something with the results
if (!responseObject.HasError)
{
var customers = responseObject.BodyAsType;
return Ok(customers);
}
else
{
var httpStatusCode = responseObject.StatusCode;
var errorMessage = responseObject.Error;
return StatusCode((int)httpStatusCode, errorMessage);
}
}
}
Configuring the service from startup following the Options Pattern is the simpler way, but there are more ways HTTP verbs supported are: GET, POST, PUT, DELETE, PATCH and HEAD.