HttpWrapperClients 1.1.9

dotnet add package HttpWrapperClients --version 1.1.9                
NuGet\Install-Package HttpWrapperClients -Version 1.1.9                
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="HttpWrapperClients" Version="1.1.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpWrapperClients --version 1.1.9                
#r "nuget: HttpWrapperClients, 1.1.9"                
#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 HttpWrapperClients as a Cake Addin
#addin nuget:?package=HttpWrapperClients&version=1.1.9

// Install HttpWrapperClients as a Cake Tool
#tool nuget:?package=HttpWrapperClients&version=1.1.9                

HttpWrapperClients README

Overview

HttpWrapperClients is a NuGet package that simplifies making HTTP requests by providing a wrapper around HttpClient. This package is designed to work with Dependency Injection (DI) in .NET, making it easy to integrate into your applications.

Installation

To install the package, use the following command in the NuGet Package Manager Console:

Install-Package HttpWrapperClients -Version 1.1.9

Alternatively, you can add the package reference directly to your .csproj file:

<PackageReference Include="HttpWrapperClients" Version="1.1.9" />

Setup

Registering the Service

After installing the package, you need to register the HttpWrapperClients service with the DI container. This can be done in the Program.cs file (for .NET 6 and later) or Startup.cs file (for earlier versions):

builder.Services.AddHelperClientWrapper();

Using the Service

To use the HttpWrapperClients service in your class, you need to inherit it and pass IHttpClientFactory to the base constructor:

Example Usage

Here's an example of how to use the HttpWrapperClients service in a class:

public class MyService : HttpClientWrapper
{
    public MyService(IHttpClientFactory httpClientFactory) : base(httpClientFactory)
    {
    }

    /// <summary>
    /// Makes an HTTP request using the HttpClientWrapper.
    /// </summary>
    /// <typeparam name="TRequest">The type of the request payload.</typeparam>
    /// <typeparam name="TResponse">The type of the response payload.</typeparam>
    /// <typeparam name="TError">The type of the error payload.</typeparam>
    /// <param name="request">The request payload.</param>
    /// <param name="url">The URL to send the request to.</param>
    /// <param name="method">The HTTP method (GET, POST, etc.).</param>
    /// <param name="contentType">The content type of the request.</param>
    /// <param name="apiType">The API type (REST, SOAP, PlainText).</param>
    /// <param name="headers">Optional HTTP headers to include in the request.</param>
    /// <returns>A tuple containing the success status, response object, and error object.</returns>
    public async Task<(bool Success, TResponse Response, TError Error)> MakeRequest<TRequest, TResponse, TError>(
        TRequest request, string url, HttpMethod method, string contentType, HttpClientWrapper_ApiType apiType, Dictionary<string, string> headers = null)
    {
        var response = await SendRequest<TRequest, TResponse, TError>(
            request != null ? request : default,
            url,
            method,
            apiType,
            contentType,
            headers: headers // dictionary
        );

        if (response.Item3 == HttpStatusCode.InternalServerError)
            return (false, default, default);

        return response.Item1 != null
            ? (true, response.Item1, default)
            : (false, default, response.Item2);
    }
}

Sending a Request

To send a request using the SendRequest method, follow the pattern below:

var request = new MyRequestModel
{
    // Populate request model properties
};

var headers = new Dictionary<string, string>
{
    { "Authorization", "Bearer token" },
    { "Custom-Header", "HeaderValue" }
};

var url = "https://api.example.com/resource";
var method = HttpMethod.Post;
var contentType = "application/json";
var apiType = HttpClientWrapper_ApiType.Rest; // can be HttpClientWrapper_ApiType.PlainText, HttpClientWrapper_ApiType.Soap, or HttpClientWrapper_ApiType.Rest

var (success, response, error) = await myService.MakeRequest<MyRequestModel, MyResponseModel, MyErrorModel>(
    request,
    url,
    method,
    contentType,
    apiType,
    headers
);

if (success)
{
    var data = response;
    // Handle successful response
}
else
{
    // Handle error response
}

Response Handling

The SendRequest method returns a tuple with three elements:

  1. bool: Indicates whether the request was successful.
  2. TResponse: The response object if the request was successful.
  3. TError: The error object if the request failed.
  4. HttpStatusCode: The status code of the response.

Check the first element of the tuple to determine if the request was successful. If not, handle the error appropriately using the third element of the tuple.

Example of Response Handling

var (success, response, error) = await myService.MakeRequest<MyRequestModel, MyResponseModel, MyErrorModel>(
    request,
    url,
    method,
    contentType,
    apiType,
    headers
);

if (success)
{
    // Handle the successful response
    var data = response;
}
else
{
    // Handle the error response
    var errorMessage = error;
}

License

HttpWrapperClients is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your changes.

Contact

For questions or support, please open an issue on the GitHub repository.


This README provides a comprehensive guide to using the HttpWrapperClients package, including installation, setup, usage, and response handling. The examples demonstrate how to integrate the service into a .NET application and make HTTP requests using the SendRequest method.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.9 216 7/25/2024
1.1.8 187 7/24/2024
1.1.7 86 7/24/2024
1.1.6 152 7/19/2024
1.1.5 407 10/2/2023
1.1.4 130 10/2/2023
1.1.3 132 10/2/2023
1.1.2 140 10/2/2023
1.1.1 146 10/2/2023
1.1.0 134 10/2/2023
1.0.9 142 10/1/2023
1.0.8 135 9/20/2023
1.0.7 455 7/10/2023