NETAPI 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NETAPI --version 1.0.0
NuGet\Install-Package NETAPI -Version 1.0.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="NETAPI" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NETAPI --version 1.0.0
#r "nuget: NETAPI, 1.0.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.
// Install NETAPI as a Cake Addin
#addin nuget:?package=NETAPI&version=1.0.0

// Install NETAPI as a Cake Tool
#tool nuget:?package=NETAPI&version=1.0.0

NETAPI

NETAPI provides an easy to use yet feature rich API client implementation for use in any C# project.

Installation

Can be found on nuget.com

Usage

Given the following endpoint enum:

    public enum Endpoint
    {
        Upload,
        Download,
        Login,
        FooModel,
        BarModel
    }

And the following environments:

    public enum Environment
    {
        Dev,
        Test,
        Production
    }

Create an ApiConfiguration class:

    public class ApiConfiguration : ApiConfigurationBase<Environment, Endpoint>
    {
        // Implement the abstract configure function
        public override void Configure()
        {
            // Add your envrionments
            AddEnvironment(Environment.Dev, "https://dev.api.com");
            AddEnvironment(Environment.Test, "https://uat.api.com");
            AddEnvironment(Environment.Production, "https://prod.api.com");

            // Set the current environment
            SetCurrentEnvironment(Environment.Dev);

            // Add headers for each endpoint
            AddHeader("Content-Type", "application/json");
            // Add headers per endpoint
            AddHeader(Endpoint.Login, "Content-Type", "application/json");

            // Set max concurrent requests for each endpoint
            SetMaxConcurrentRequests(2);
            // Set max concurrent requests per endpoint
            SetMaxConcurrentRequests(Endpoint.Upload, 5);
            SetMaxConcurrentRequests(Endpoint.Download, 5);
            SetMaxConcurrentRequests(Endpoint.Login, 1);

            // Set the minimum request interval for each endpoint
            SetMinimumInterval(5000);
            // Set the minimum request interval per endpoint
            SetMinimumInterval(Endpoint.Login, 5000);
        }
    }

Inject or instantiate an ApiService:

    ...

    readonly IApiService<Environment, Endpoint> _apiService;

    public Action LoginCommand =>
        new Action(async () => await OnLoginAsync());

    public Action GetItemsCommand =>
        new Action(async () => await GetItemsAsync());

    // Directly constructing the service and configuration
    public Foo()
    {
        _apiService = new ApiService<Environment, Endpoint>(new ApiConfiguration());
    }

    // If you use a dependency injection or IOC, both IApiService and IApiConfiguration can be injected;
    //
    // With explicit registration:
    //     _container.Register<IApiService, ApiService>();
    //     _container.Register<IApiConfiguration<Environment, Endpoint>, ApiConfiguration>();
    public Foo(IApiService<Environment, Endpoint> apiService)
    {
        _apiService = apiService;
    }

    ...

Get models fom the API:

    private async Task BarAsync()
    {
        ResponseBase<FooModel?>? response =
            await _apiService.Get<FooModel>(Endpoint.FooModel);

        if (response != null
            && response.Data != null
            && response.Success) {

            FooModel model = response.Data;

            Debug.WriteLine(model?.BarModel?.Bar);
        }
    }

Authentication

If you need to authenticate with the API, make your auth call, then set service configuration OAuthBearerToken property.

    private async Task LoginAsync()
    {
        ResponseBase<LoginResponse?>? response = await _apiService.Post<LoginRequest, LoginResponse>(
            Endpoint.Login,
            new LoginRequest("test@example.com", "password"));

        if (response != null
            && response.Data != null
            && response.Success) {

            _apiService
                .Configuration
                .OAuthBearerToken = response.Data.Token;
        }
    }

Setting OAuthBearerToken will automatically attach an "Authorization: Bearer {token}" header to every configured endpoint.

Development

Functionallity can be manipulated by creating custom services and configurations.

To get started, first create a custom configuration:

    public class MyConfig : ApiConfigurationBase<Environment, Endpoint> { }

Then create a custom service:

    public MyApiService: ApiService<MyEnvironments, MyEndpoints> { }

You can then override the default methods for any functions in IApiService.

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 (1)

Showing the top 1 NuGet packages that depend on NETAPI:

Package Downloads
XamCore

Core Xamarin classes for rapid application development.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 610 3/31/2021
1.0.1 356 1/19/2021
1.0.0 354 1/19/2021