TheNevix.Utils.RequestHandler 1.1.3

dotnet add package TheNevix.Utils.RequestHandler --version 1.1.3                
NuGet\Install-Package TheNevix.Utils.RequestHandler -Version 1.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TheNevix.Utils.RequestHandler" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TheNevix.Utils.RequestHandler --version 1.1.3                
#r "nuget: TheNevix.Utils.RequestHandler, 1.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TheNevix.Utils.RequestHandler as a Cake Addin
#addin nuget:?package=TheNevix.Utils.RequestHandler&version=1.1.3

// Install TheNevix.Utils.RequestHandler as a Cake Tool
#tool nuget:?package=TheNevix.Utils.RequestHandler&version=1.1.3                

TheNevix.Utils.RequestHandler

NuGet NuGet Downloads

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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