A helper library to use with HTTP API Calls
you need to install the package, add to DI and then use it in services.
- install the package
dotnet add package Pdsr.HttpClient
and for extensionsdotnet add package Pdsr.HttpClient.Extensions
. - Inject an
System.Net.HttpClient
to the DI container. - Implement the
IPdsrClientBase
or inherit the abstract classPdsrClientBase
and override any methods required.
public class SomeService
{
private readonly IPdsrClient _client;
public SomeService(IPdsrClient client) => _client = client;
public async Task SomeAsyncMethod(string someRouteId, string someQueryStringValue, CancellationToken cancellationToken = default)
{
var results = await _client.Url("https://example.com/api").AddUrl(someRouteId)
.AddQueryString ("key" , someQueryStringValue)
.Accept("application/vnd.api.custom+custom")
.OnBadRequest( (res)=>
{
// do something
})
.OnException( (ex) =>
{
// do something about the exception
})
.OnNoFound( (res) =>
{
// do something when resource not found.
})
// and any other status code and so on
// or add handler to the Client to run on certain situations
// or add handler to the HttpRequestMessage on certain situations
.Post(new { Something = "some value" })
.SnakeCase()
.SendAsync<SomeModelSupposeToDeserializeTo>(cancellationToken);
return results;
}
}
you need to override the abstract method GetAuthorizationHeader
if your API needs authentication and implement the authorization logic there.
protected abstract Task SetAuthorizationHeader(HttpRequestMessage request, CancellationToken cancellationToken = default);
You can also log all requests and responses in the inherited class, to have one code log all requests.
protected abstract Task WriteLog(HttpResponseMessage response, long ellapsed, CancellationToken cancellationToken = default);
Use the required Serializer/Deserializer Name casing.
Right now, it only supports CamelCase
by default and SnakeCase
can be used as well.
In case of deserialization, if your API does not return any model or you want to get anything else other than Model, such as Stream, String, or the HttpResponseMessage itself, you can use the respected method such as GetStream
or GetString
and override the SendAsync
to return the Message itself.
Please refer to contribute.
Under Construction.