The SDK includes a custom implementation of an HTTP client abstraction that provides a fluent, developer-friendly API for constructing and executing HTTP requests.
Below is an example of a class that retrieves data from an OData-compliant HTTP endpoint. The response is automatically deserialized into a strongly-typed Vendor
object.
public class CustomerModule
{
private readonly ILogger<CustomerModule> _logger;
private readonly HttpClientService _httpClientService;
private readonly IAuthenticationService _authenticationService;
public CustomerModule(ILogger<CustomerModule> logger,
HttpClientService httpClientService,
IAuthenticationService authenticationService)
{
_logger = logger;
_httpClientService = httpClientService;
_authenticationService = authenticationService;
}
public async Task<Vendor> GetVendor()
{
var token = await _authenticationService.GetAccessToken();
var getVendor =
await _httpClientService.CreateHttpRequest(new Uri("https://a.api.com"), false)
.WithPath("/data/Vendors")
.WithQueryParams(new Dictionary<string, string>()
{
{ "cross-company", "true" },
{ "$filter", $"VendorAccountNumber eq '11' and dataAreaId eq '22'" },
})
.WithBearerAuthentication(token)
.WithMethod(HttpMethod.Get)
.Start();
if (!getVendor.IsSuccessStatusCode)
{
var vendor = await getVendor.GetResponseJsonBody<Vendor>();
return Ok(vendor);
}
return BadRequest(getVendor.GetResponseJsonBody<ErrorMessage>());
}
}