MondoCore.Rest 1.2.0

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

MondoCore.Rest

Class/interface for calling REST apis <br>

Dependency Injection

  • In your dependency injection code create an instance of an api.
    • Each unique api should be a different instance of RestApi.
    • Differentiate each api by using a different class.
    • You can pass in an optional IHeaderFactory. This is used to insert headers, e.g. a bearer token, at request time.

<br/>

    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // ...

            builder.Services.AddHttpClient();

            // Register the apis. Automobile and Truck are just used to differentiate apis and can be blank classes
            builder.Services.AddRestApi<Automobile>("cars", "https://www.notrealcars.com)
                            .AddRestApi<Truck>("trucks", "https://www.notrealtrucks.com, myHeaderFactory);

            // ...
        }
    }

    public static class Extensions
    {
        public static IServiceCollection AddRestApi<T>(this IServiceCollection services, string name, string url)
        {
            // Register the api and url with the HttpClientFactory
            services.AddHttpClient(name: name, configureClient: (p, client) =>
                    {
                        client.BaseAddress = new Uri(url);
                        client.Timeout = TimeSpan.FromSeconds(60);
                    });

            // Now inject the RestApi class
            services.AddScoped<IRestApi<T>>( p=> new RestApi<T>(p.GetRequiredService<IHttpClientFactory>(), name));

            return services;
        }
    }

<br>

Calling an API with IRestApi

Inject the IRestApi interface into your class

using MondoCore.Rest;

public class CarService
{
    private readonly IRestApi<Automoble> _api;

    public CarService(IRestApi<Automoble> api)
    {
        _api = api;
    }

    public async Task<List<Automobile>> GetGreenCars()
    {
        return _api.Get<List<Automobile>>("all/green"); // Appends "/all/green" onto the url given at DI time
    }

    public async Task<Automobile> GetCar(string id)
    {
        return _api.Get<Automobile>("car/" + id); // Appends "car/1" (for instance) onto the url given at DI time
    }
}

<br>

Reference

IRestAPI<T>

Interface used to call REST API. Note that the generic type isn't actually used in the interface itself, it's just used to differentiate apis in dependency injection.

Task SendRequest<TRequest>(HttpMethod method, string url, TRequest? content = default(TRequest?), object? headers = null)

No need to call this directly. See the default interface methods below.

Task<TResponse> SendRequest<TRequest, TResponse>(HttpMethod method, string url, TRequest? content = default(TRequest?), object? headers = null);

No need to call this directly. See the default interface methods below.

Default Methods
Task<T> Get<T>(string url, object? headers = null)

Does a GET request to the api. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task<TResponse> Post<TRequest, TResponse>(string url, TRequest content, object? headers = null)

Does a POST request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task Post<TRequest>(string url, TRequest content, object? headers = null)

Does a POST request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task<TResponse> Put<TRequest, TResponse>(string url, TRequest content, object? headers = null)

Does a PUT request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task Put<TRequest>(string url, TRequest content, object? headers = null)

Does a PUT request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task<TResponse> Patch<TRequest, TResponse>(string url, TRequest content, object? headers = null)

Does a PATCH request to the api that returns a response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task Patch<TRequest>(string url, TRequest content, object? headers = null)

Does a PATCH request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

Task Delete<TRequest>(string url, object? headers = null)

Does a DELETE request to the api thats returns no response. Note that url is appended to the url defined in dependency injection. "headers" can be a POCO, anonymous object or a dictionary

License

MIT

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.  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. 
.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.2.0 183 10/21/2024
1.1.0 128 9/2/2024
1.0.0 152 3/28/2024