WebApi.RestClient 1.0.3

dotnet add package WebApi.RestClient --version 1.0.3
                    
NuGet\Install-Package WebApi.RestClient -Version 1.0.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="WebApi.RestClient" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebApi.RestClient" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="WebApi.RestClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebApi.RestClient --version 1.0.3
                    
#r "nuget: WebApi.RestClient, 1.0.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.
#:package WebApi.RestClient@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WebApi.RestClient&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=WebApi.RestClient&version=1.0.3
                    
Install as a Cake Tool

WebApi RestClient V1.0.1

What is WebApi RestClient

WebApi RestClient is a library designed to simplify and make fluid API calls to any type of web service.

The library is designed to integrate with Microsoft's HttpClientFactory, taking advantage of its features for managing HTTP clients, such as connection pooling, lifecycle management and centralized configuration of the HttpClients.

The main feature of this library is the Fluent architecture, which allows you to build and send API requests in an intuitive and readable way, with a compact and easily configurable syntax.

How to use it?

In the Program.cs file, you can register the various HttpClients using the AddHttpClient method. This allows you to set common properties of API calls, keeping the logic of creating the HttpClient compliant with the ASP.NET Core standards.

Here is an example of configuring a client:

services.AddHttpClient("api", client =>
{
  client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
  client.DefaultRequestHeaders.Add("Accept", "application/json");
});

If you are in blazor WebAssembly you can register your HttpClient like this :

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5154/") });

You must insert this after your HttpClient :

builder.Services.AddRestClient();

//If you have WebAssembly project
builder.Services.AddRestClient(builder); //builder is WebAssemblyHostBuilder

At this point you can use the HttpClientBuilderFactory class that allows you to create a client with the specified name that must be the same as the one inserted inside the AddHttpClient method.

var client = _httpClientBuilderFactory.CreateClient("api");

The CreateClient method will return an instance of HttpClientBuilder which allows us to build our API call in a Fluent way.

Usage Example

With the HttpClientBuilder instance, you can easily configure the API call, specifying the HTTP method, endpoint, query parameters, body, and so on, using a Fluent syntax.

Here is an example of an API call with a POST method, query parameters, and a JSON body:

var response = await client
    .WithMethod(HttpMethod.Post)
    .WithEndpoint("login")
    .WithQueryParameters(new Dictionary<string, string>
    {
    { "useCookies", "false" },
    { "useSessionCookies", "false" }
    })
    .WithBody(new { email = "test@mail.com", password = "Passw0rd" })
    .BuildRequest()
    .SendAsync<LoginInfoResponse>();

In this example, the POST call is sent to the login endpoint with the query parameters and the body containing the login credentials. The response is then deserialized into the LoginInfoResponse object.

Response Handling

When you send a request, the library returns an object of type RestResponseWithData<T>, which extends the base class RestResponse. This object includes several properties useful for managing the status of the response, such as the HTTP status code, a description of the status, any errors, and the data returned by the call.

public class RestResponse
{
/// <summary>
/// Indicates whether the status of the response is successful or not.
/// </summary>
public bool IsSuccessful { get; internal set; }

/// <summary>
/// Status code of the response.
/// </summary>
public HttpStatusCode StatusCode { get; internal set; }

/// <summary>
/// Description of the status of the response.
/// </summary>
public string? StatusDescription { get; internal set; }

/// <summary>
/// Any error message.
/// </summary>
public string? ErrorBody { get; internal set; }
}
public class RestResponseWithData<T> : RestResponse
{
/// <summary>
/// The data returned by the API response.
/// </summary>
public T? Data { get; internal set; }
}

This framework allows you to easily handle errors and data returned from API calls, centralizing the response handling logic and improving code readability.

Key Features

  • Fluent API: Build and send API calls in an intuitive and readable way.
  • Support for major HTTP methods: GET, POST, PUT, DELETE, etc.
  • Centralized HttpClient Management: Leverage HttpClientFactory capabilities.
  • Automatic Response Deserialization: Automatically deserialize the data returned by the API call based on the specified type.
  • Error Handling: Easily handle API call errors and exceptions.
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.0.3 292 4/15/2025
1.0.2 238 4/15/2025
1.0.1 212 2/2/2025
1.0.0 141 2/2/2025