Puffix.Rest 3.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Puffix.Rest --version 3.2.0
                    
NuGet\Install-Package Puffix.Rest -Version 3.2.0
                    
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="Puffix.Rest" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Puffix.Rest" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Puffix.Rest" />
                    
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 Puffix.Rest --version 3.2.0
                    
#r "nuget: Puffix.Rest, 3.2.0"
                    
#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 Puffix.Rest@3.2.0
                    
#: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=Puffix.Rest&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Puffix.Rest&version=3.2.0
                    
Install as a Cake Tool

Puffix REST

Library to handle REST API in your project.

The library handles the management of the HTTP client, authentication and result serialization.

Three samples are provided in the test projet (Tests.Puffix.Rest):

  • Open Weather Map API
  • Azure Maps API
  • OVH API

Token

The first element to implement is the token. Two types of token are available: the header token (IHeaderToken interface), when the token is passed in the request headers, or the query token (IQueryParameterToken interface), when the token is passed as a query parameter. These interfaces are inherited to define a contract and an implementatipn as shown in the following paragraphs.

Header token

Create the token interface:

public interface ISampleApiToken : IHeaderToken { }

Token implementation:

public class SampleApiToken(IConfiguration configuration) : IOvhApiToken
{
    private readonly string token = configuration["tokenParamaterName"] ?? string.Empty;

    public IDictionary<string, string> GetHeaders()
    {
        return new Dictionary<string, string>()
        {
            { "headerName", token },
        };
    }
}

Query parameter token

Create the token interface:

public interface ISampleApiToken : IQueryParameterToken { }

Token implementation:

public class SampleApiToken(IConfiguration configuration) : ISampleApiToken
{
    private readonly string token = configuration["tokenParamaterName"] ?? string.Empty;

    public string GetQueryParameter()
    {
        return $"queryParameterName={token}";
    }
}

Query description

The query description embeds and manages all the information for the query, like the base URI, query path, query parameters, query headers, or body. It controls the construction of the full URI with the path, the parameters, and the token, if needed. It also controls the body serialization or the headers sent to the targeted service. A contract is defined and inherits from the base contract, the IQueryInformation<TokenT> interface. Then a conctrete class is created and inherits from the defined contract, and the QueryInformation<TokenT> base class.

Create the query infotmation interface:

public interface ISampleApiQueryInformation : IQueryInformation<ISampleApiToken> { }

Query information implementation:

public class SampleApiQueryInformation(HttpMethod httpMethod, ISampleApiToken? token, IDictionary<string, string> headers, string uri, string queryParameters, string queryContent) :
    QueryInformation<ISampleApiToken>(httpMethod, token, headers, uri, queryParameters, queryContent),
    ISampleApiQueryInformation
{
    public static ISampleApiQueryInformation CreateNewUnauthenticatedQuery(HttpMethod httpMethod, string apiUri, string queryPath, string queryParameters, string queryContent)
    {
        string uri = BuildUriWithPath(apiUri, queryPath);
        return new SampleApiQueryInformation(httpMethod, default, new Dictionary<string, string>(), uri, queryParameters, queryContent);
    }

    public static ISampleApiQueryInformation  CreateNewAuthenticatedQuery(ISampleApiToken token, HttpMethod httpMethod, string apiUri, string queryPath, string queryParameters, string queryContent)
    {
        string uri = BuildUriWithPath(apiUri, queryPath);
        return new SampleApiQueryInformation(httpMethod, token, new Dictionary<string, string>(), uri, queryParameters, queryContent);
    }
}

The query information implementation can override the GetUriWithParameters, GetQueryContent or GetAuthenticationHeader methods to have more control over the generation of these elements.

The CreateNewAuthentication method is used to build unauthenticated queries, for example, as a first call to get a token to a service.

HTTP repository

The HTTP repository embeds the logic to implement the HTTP client to send the query and handle the response. To call the base HTTP repository, a contract is defined and inherits from the base contract, the IRestHttpRepository<QueryInformationContainerT, TokenT> interface. Then a conctrete class is created and inherits from the defined contract, and the RestHttpRepository<QueryInformationContainerT, TokenT> base class.

Create the query infotmation interface:

public interface ISampleApiHttpRepository : IRestHttpRepository<ISampleApiQueryInformation, ISampleApiToken> { }

HTTP repository implementation:

public class SampleApiHttpRepository(IHttpClientFactory httpClientFactory) :
    RestHttpRepository<ISampleApiQueryInformation, ISampleApiToken>(httpClientFactory),
    ISampleApiHttpRepository
{
    public override ISampleApiQueryInformation BuildUnauthenticatedQuery(HttpMethod httpMethod, string apiUri, string queryPath, string queryParameters, string queryContent)
    {
        return SampleApiQueryInformation.CreateNewUnauthenticatedQuery(httpMethod, apiUri, queryPath, queryParameters, queryContent);
    }

    public override ISampleApiQueryInformation BuildAuthenticatedQuery(IAzMapsApiToken token, HttpMethod httpMethod, string apiUri, string queryPath, string queryParameters, string queryContent)
    {
        return SampleApiQueryInformation.CreateNewAuthenticatedQuery(token, httpMethod, apiUri, queryPath, queryParameters, queryContent);
    }
}

The HTTP repository is also used as a gateway to initialise the information objects in the request. Thus, in basic calls, only references to the repository and the basic token are required.

Base call

ISampleApiToken token = container.Resolve<ISampleApiToken>();
ISampleApiHttpRepository httpRepository = container.Resolve<ISampleApiHttpRepository>();

ISampleApiQueryInformation queryInformation = httpRepository.BuildAuthenticatedQuery(token, HttpMethod.Get, apiBaseUri, queryPath, queryParameters, queryContent);

string response = await httpRepository.HttpAsync(queryInformation);

Complete samples are available in the Tests.Puffix.Rest project.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  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
3.4.2 194 8/20/2025
3.4.1 136 8/18/2025
3.4.0 123 8/18/2025
3.3.1 246 5/15/2025
3.3.0 234 11/13/2024
3.2.3 137 10/21/2024
3.2.2 270 5/20/2024
3.2.1 197 3/5/2024
3.2.0 158 3/2/2024

First release