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
- Host: GitHub
- URL: https://github.com/tbertuzzi/httpextension
- Owner: TBertuzzi
- Created: 2019-02-21T13:53:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-18T12:58:43.000Z (over 1 year ago)
- Last Synced: 2025-04-11T22:45:56.367Z (6 months ago)
- Language: C#
- Homepage:
- Size: 51.8 KB
- Stars: 13
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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|[](https://www.nuget.org/packages/HttpExtension/)|[](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)
);
}
```