ReqHub 2.3.0

Install-Package ReqHub -Version 2.3.0
dotnet add package ReqHub --version 2.3.0
<PackageReference Include="ReqHub" Version="2.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReqHub --version 2.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ReqHub, 2.3.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ReqHub as a Cake Addin
#addin nuget:?package=ReqHub&version=2.3.0

// Install ReqHub as a Cake Tool
#tool nuget:?package=ReqHub&version=2.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ReqHubDotNet

ReqHub middleware for C# projects. Distribute your API using the ReqHub platform in just a few lines! For more information, visit https://reqhub.io.

Distributing an API

To distribute an API for clients to consume with API keys, add these two lines to your Startup.cs:

/// Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Visit https://reqhub.io to get your API keys
        services.AddReqHub("yourPublicKey", "yourPrivateKey");
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...
        // app.UseRouting();
        
        // Put this after app.UseRouting() and before app.UseEndpoints().
        // It verifies requests against the platform and either
        // continues execution or returns a 403 forbidden response
        // if the request is invalid (bad API key, contents tampered with, etc.)
        app.UseReqHub();
        
        // app.UseEndpoints(...);
        // ...
    }
}

That's it! 🎉

Identity

You may want to be able to uniquely identify a client. Inside your controller methods you can access the clientId with:

this.User.GetClientId();

You can also access plan information:

this.User.GetPlanName();
this.User.GetNormalizedPlanName();
this.User.GetPlanSku();
this.User.GetNormalizedPlanSku();
How it works

Clients consuming your API create a request hash using their own API keys, which the middleware forwards to the platform along with your request hash. If everything matches up on the platform, the request is allowed to continue.

Consuming an API

To consume an API, configure a client and inject it into your controllers/services.

/// Startup.cs

public class Startup
{
    // ...
    
    public void ConfigureServices(IServiceCollection services)
    {
        // Visit https://reqhub.io to get your API keys
        services.AddApiClient("https://api-base-address", "yourClientPublicKey", "yourClientPrivateKey", "serviceName");
        
        // Add as many as you like!
        services.AddApiClient("https://api2-base-address", "anotherClientPublicKey", "anotherClientPrivateKey", "serviceName2");
    }
}

(it looks like a lot of code, but we promise it isn't 👌):

/// ExampleController.cs

public class ExampleController : ControllerBase
{
    private readonly IApiClient exampleApiClient;

    // Resolve the API client using dependency injection
    public ExampleController(ApiClientResolver apiClientResolver)
    {
        this.exampleApiClient = apiClientResolver("serviceName");
    }

    [HttpGet]
    public async Task<IEnumerable<Example>> Get()
    {
        return await this.exampleApiClient.GetAsync<IEnumerable<Example>>("/example/endpoint");
    }
}

.NET Framework

For those of you not using .NET Core, ReqHub supports .NET Framework (4.5+) WebApi projects.

/// FilterConfig.cs

public class FilterConfig
{
    public static void RegisterGlobalFilters(HttpFilterCollection filters)
    {
        // ...
        filters.Add(new ReqHubAttribute("yourPublicKey", "yourPrivateKey"));
    }
}

To consume an API:

// A little bit of setup
// (note that the HttpClient should be instantiated once and reused throughout the life of the application https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1#remarks)
var httpClient = HttpClientFactory.Create(new ReqHubClientHttpMessageHandler("yourClientPublicKey", "yourClientPrivateKey"));
httpClient.BaseAddress = new Uri("https://api-base-address");
var exampleApiClient = new ApiClient(httpClient);

await this.exampleApiClient.GetAsync<IEnumerable<Example>>("/example/endpoint");

Contributing

Go for it! If we're missing something or you're running into a problem, either let us know in an issue or send us a pull request. We think we're pretty reasonable 😘

License

MIT, babe -- go nuts! 🎉

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
2.3.0 127 11/23/2020
2.2.0 121 10/4/2020
2.1.4 146 8/14/2020
2.1.3 197 7/6/2020
2.1.2 202 7/6/2020
2.1.1 211 7/5/2020
2.1.0 213 7/2/2020
1.2.2 199 6/24/2020
1.2.1 159 6/19/2020
1.2.0 176 4/22/2020
1.1.3 193 3/27/2020
1.1.2 185 2/25/2020