TheNevix.Utils.RequestHandler
1.1.1
See the version list below for details.
dotnet add package TheNevix.Utils.RequestHandler --version 1.1.1
NuGet\Install-Package TheNevix.Utils.RequestHandler -Version 1.1.1
<PackageReference Include="TheNevix.Utils.RequestHandler" Version="1.1.1" />
paket add TheNevix.Utils.RequestHandler --version 1.1.1
#r "nuget: TheNevix.Utils.RequestHandler, 1.1.1"
// Install TheNevix.Utils.RequestHandler as a Cake Addin #addin nuget:?package=TheNevix.Utils.RequestHandler&version=1.1.1 // Install TheNevix.Utils.RequestHandler as a Cake Tool #tool nuget:?package=TheNevix.Utils.RequestHandler&version=1.1.1
TheNevix.Utils.RequestHandler
A simple and flexible HTTP request handler utility for .NET applications. This library simplifies making API calls by providing an easy-to-use interface for sending HTTP requests.
Features
- Simplified API for making HTTP requests
- Built-in error handling and response validation
- Supports .NET 6.0, 7.0, and 8.0
Installation
You can install the package via NuGet Package Manager, .NET CLI, or by adding it to your project file.
NuGet Package Manager
Install-Package TheNevix.Utils.RequestHandler
Configuration
Enable the RequestHandler in your program.cs file.
builder.Services.AddRequestHandlerServices();
Usage
HTTP request with ExecuteAsync()
This example shows how to send an HTTP request and manage the response manually.
public class SomeService
{
private readonly IRequestHandler _requestHandler;
public SomeService(IRequestHandler requestHandler)
{
_requestHandler = requestHandler;
}
public async Task DoSomethingAsync(string url)
{
var response = await requestHandler
.CreateRequest("https://www.freetogame.com/api/games?platform=pc")
.WithMethod(HttpMethod.Get)
.ExecuteAsync();
response.EnsureSuccessStatusCode();
var jsonResponseBody = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<List<SimpleExecuteAsync>>(jsonResponseBody);
}
}
class SimpleExecuteAsync
{
public string Title { get; set; }
public string Genre { get; set; }
}
In this example, you receive an HttpResponseMessage object. You can then check the status code, deserialize the response body, and handle the data accordingly.
HTTP request with ExecuteAsync<TResponse>()
This example demonstrates how to send an HTTP request and let the library convert the JSON response into a provided response model.
public class SomeService
{
private readonly IRequestHandler _requestHandler;
public SomeService(IRequestHandler requestHandler)
{
_requestHandler = requestHandler;
}
public async Task DoSomethingAsync(string url)
{
var response = await requestHandler
.CreateRequest("https://www.freetogame.com/api/games?platform=pc")
.WithMethod(HttpMethod.Get)
.ExecuteAsync<List<ExecuteAsyncWithResponseModel>>();
}
}
class ExecuteAsyncWithResponseModel
{
public string Title { get; set; }
public string Genre { get; set; }
}
Here, the response is automatically deserialized into the specified TResponse model. Remember to handle any potential exceptions, especially deserialization errors.
HTTP request with ExecuteWithHandlingAsync<TResponse, TData>()
This example shows how to send an HTTP request where the library not only converts the JSON response into a provided response model but also checks for errors. If an error occurs, the IsSuccess property will be false.
public class SomeService
{
private readonly IRequestHandler _requestHandler;
public SomeService(IRequestHandler requestHandler)
{
_requestHandler = requestHandler;
}
public async Task DoSomethingAsync(string url)
{
var response = await requestHandler
.CreateRequest("https://www.freetogame.com/api/games?platform=pc")
.WithMethod(HttpMethod.Get)
.ExecuteWithHandlingAsync<ResponseModel, List<GamesData>>();
if (!response.IsSuccess)
{
//Handle an error
}
}
}
class ResponseModel : IResponseModel<List<GamesData>>
{
public List<GamesData> Data { get; set; }
public bool IsSuccess { get; set ; }
public string? Message { get ; set ; }
public int StatusCode { get; set; }
}
class GamesData
{
public string Title { get; set; }
public string Genre { get; set; }
}
In this case, the library handles the response and error checking for you. You can easily determine if the request was successful by checking the IsSuccess property.
License
This project is licensed under the MIT License.
Special thank you to freetogame
This documentation used the free freetogame api to showcase this package.
You can check them out at: https://www.freetogame.com/
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.3)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.3 | 0 | 12/2/2024 |
1.1.2 | 0 | 12/2/2024 |
1.1.1 | 0 | 12/2/2024 |
1.1.0 | 132 | 8/14/2024 |
1.1.0-beta-240813 | 113 | 8/13/2024 |
1.1.0-beta-240812-4 | 100 | 8/12/2024 |
1.1.0-beta-240812-3 | 102 | 8/12/2024 |
1.1.0-alpha-240812-2 | 104 | 8/12/2024 |
1.1.0-alpha-240812 | 107 | 8/12/2024 |
1.0.0 | 134 | 8/10/2024 |