An open API service indexing awesome lists of open source software.

https://github.com/tbertuzzi/httpextension

:package: Extensions for HttpClient
https://github.com/tbertuzzi/httpextension

Last synced: 6 months ago
JSON representation

:package: Extensions for HttpClient

Awesome Lists containing this project

README

          

# HttpExtension

Extensions for HttpClient

###### This is the component, works on .NET Core and.NET Framework

**NuGet**

|Name|Info|Contributors|
| ------------------- | ------------------- | ------------------- |
|HttpExtension|[![NuGet](https://buildstats.info/nuget/HttpExtension)](https://www.nuget.org/packages/HttpExtension/)|[![GitHub contributors](https://img.shields.io/github/contributors/TBertuzzi/HttpExtension.svg)](https://github.com/TBertuzzi/HttpExtension/graphs/contributors)|

**Platform Support**

HttpExtension is a netstandard 2.1 library.

Extensions to make using HttpClient easy.

* GetAsync : Gets the return of a Get Rest and converts to the object or collection of pre-defined objects.
You can use only the path of the rest method, or pass a parameter dictionary. In case the url has parameters.

```csharp
public static async Task> GetAsync(this HttpClient httpClient, string address);
public static async Task> GetAsync(this HttpClient httpClient, string address, Dictionary values);
```

* PostAsync,PutAsync and DeleteAsync : Use post, put and delete service methods rest asynchronously and return objects if necessary.

```csharp
public static async Task PostAsync(this HttpClient httpClient, string address, object dto);
public static async Task> PostAsync(this HttpClient httpClient, string address, object dto);

public static async Task PutAsync(this HttpClient httpClient,string address, object dto);
public static async Task> PutAsync(this HttpClient httpClient, string address, object dto);

public static async Task DeleteAsync(this HttpClient httpClient,string address, object dto);
public static async Task> DeleteAsync(this HttpClient httpClient, string address, object dto);
```

* SendAsync : Use SendAsync for your custom HTTP request message and return predefined objects or collection.

```csharp
public static async Task> SendAsync(this HttpClient httpClient, HttpRequestMessage request);
```

* HttpExtensionResponse : Object that facilitates the return of requests Rest. It returns the Http code of the request, already converted object and the contents in case of errors.

```csharp
public class HttpExtensionResponse
{
public HttpStatusCode StatusCode { get; private set; }

public T Value { get; set; }

public string Content { get; set; }

public Exception Error { get; set; }
}
```

Example of use :

```csharp
public async Task> GetTodos()
{
try
{
//GetAsync Return with Object
var response = await _httpClient.GetAsync>("todos");

if (response.StatusCode == HttpStatusCode.OK)
{
return response.Value;
}
else
{
throw new Exception(
$"HttpStatusCode: {response.StatusCode.ToString()} Message: {response.Content}");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
```

**Retry Pattern support using Polly**

You can use the retry pattern with HttpExtension using [Polly](https://github.com/App-vNext/Polly).

Example of use :

```csharp
public async Task> GetTodos()
{
var policy = CreatePolicy();
try
{
//GetAsync using retry pattern
var response = await _httpClient.GetAsync>("todos", policy);

if (response.StatusCode == HttpStatusCode.OK)
return response.Value;
else
{
throw new Exception(
$"HttpStatusCode: {response.StatusCode.ToString()} Message: {response.Content}");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

private AsyncRetryPolicy CreatePolicy()
{
return Policy
.Handle()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(2)
);
}
```