Puffix.Rest 3.4.2

dotnet add package Puffix.Rest --version 3.4.2
                    
NuGet\Install-Package Puffix.Rest -Version 3.4.2
                    
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.4.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Puffix.Rest" Version="3.4.2" />
                    
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.4.2
                    
#r "nuget: Puffix.Rest, 3.4.2"
                    
#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.4.2
                    
#: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.4.2
                    
Install as a Cake Addin
#tool nuget:?package=Puffix.Rest&version=3.4.2
                    
Install as a Cake Tool

Puffix REST

Library to handle REST API in your project.

Build and pusblish

NuGet Version

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

Four samples are provided in the test project (Tests.Puffix.Rest):

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

Token

The first element to implement is the token. Four types of token are available:

  • the header token (IHeaderToken interface), when the token is passed in the request headers,
  • the query parameter token (IQueryParameterToken interface), when the token is passed as a query parameter.
  • the query path token (IQueryPathToken interface), when the token is passed in the query path.
  • the empty token (IEmptyToken interface), when no token is required for the API.

These interfaces inherit from the IToken interface, which defines the contract to be implemented as shown in the following paragraphs.

Header token

Contract:

public interface ISampleApiToken : IHeaderToken { }

Implementation:

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

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

Query parameter token

Contract / interface:

public interface ISampleApiToken : IQueryParameterToken { }

Implementation:

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

    public string GetQueryParameterName()
    {
        return "queryParameterName";
    }

    public string GetQueryParameterValue()
    {
        return token;
    }
}

Query path token

Contract / interface:

public interface ISampleApiToken : IQueryPathToken { }

Implementation:

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

    public string GetQueryPath()
    {
        return token;
    }
}

Query description

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

Contract / interface:

public interface ISampleApiQueryInformation : IQueryInformation<ISampleApiToken> { }

Implementation:

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

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

The query information implementation can override the GetUriWithParameters, GetQueryContent or GetAuthenticationHeader methods to have more control over the generation of these elements. For example, this can be useful when generating a token that must include information about the generation date/time.

The CreateNewUnauthenticatedQuery 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 contains the logic to implement the HTTP client, to send the query, and to handle the response. To call the base HTTP repository, a contract is defined and it inherits from the base contract, the IRestHttpRepository<QueryInformationContainerT, TokenT> interface. Then a concrete class is created and it inherits from the defined contract, and the RestHttpRepository<QueryInformationContainerT, TokenT> base class.

Contract / interface:

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

Implementation:

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

    public override ISampleApiQueryInformation BuildUnauthenticatedQuery(HttpMethod httpMethod, IDictionary<string, IEnumerable<string>> headers, string apiUri, string queryPath, IDictionary<string, string> queryParameters, string queryContent)
    {
        return SampleApiQueryInformation.CreateNewUnauthenticatedQuery(httpMethod, headers, apiUri, queryPath, queryParameters, queryContent);
    }

    public override ISampleApiQueryInformation BuildAuthenticatedQuery(IAzMapsApiToken token, HttpMethod httpMethod, string apiUri, string queryPath, IDictionary<string, string> queryParameters, string queryContent)
    {
        IDictionary<string, IEnumerable<string>> headers = new Dictionary<string, IEnumerable<string>>();
        return SampleApiQueryInformation.CreateNewAuthenticatedQuery(token, httpMethod, headers, apiUri, queryPath, queryParameters, queryContent);
    }

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

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

Base call

string apiBaseUri = "https://api.ipify.org";
string queryContent = string.Empty;
IDictionary<string, string> queryParameters = new Dictionary<string, string>();

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);

// OR

IResultInformation<string> response = await httpRepository.HttpWithStatusAsync(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 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
3.4.2 9 8/20/2025
3.4.1 13 8/18/2025
3.4.0 12 8/18/2025
3.3.1 241 5/15/2025
3.3.0 231 11/13/2024
3.2.3 134 10/21/2024
3.2.2 267 5/20/2024
3.2.1 194 3/5/2024
3.2.0 153 3/2/2024

Update dependencies