Git Product home page Git Product logo

consumers.aspnetcore's Introduction

Consumers.AspNetCore

Route ASP.NET Core Web API endpoints based on consumer type. This is particularly useful when you need to perform different logic or return different responses to different API consumers using the same route.

Key Features

  • Routes requests based on consumer type passed in a custom header.
  • Provides a clean and declarative way to separate endpoints with the same method/path combination.

Usage

1. Configure the services

builder.Services.AddConsumers(options => options.HeaderName = "X-Consumer");

2. Annotate the endpoints

2.1. Controllers

[HttpGet]
[Consumers("Vendor")]
public IActionResult GetProductsForVendor()
{
    return Ok("This is the list of products for vendor");
}

[HttpGet]
[Consumers("Customer")]
public IActionResult GetProductsForCustomer()
{
    return Ok("This is the list of products for customer");
}

2.2. Minimal APIs

app.MapGet("/hello", () => "Hello Vendor!")
    .RequireConsumers("Vendor");

app.MapGet("/hello", () => "Hello Customer!")
    .RequireConsumers("Customer");

Known issues

OpenAPI (Swagger)

OpenAPI doesn't support multiple endpoints with the same path and method combination in the same document.

You can create multiple swagger documents for each API consumer

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("vendors", new OpenApiInfo { Title = "E-commerce Vendors API", Version = "v1" });
    options.SwaggerDoc("customers", new OpenApiInfo { Title = "E-commerce Customers API", Version = "v1" });
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/vendors/swagger.json", "E-commerce Vendors API");
        options.SwaggerEndpoint("/swagger/customers/swagger.json", "E-commerce Customers API");
    });
}

And annotate the controller actions with ApiExplorerSettings attribute

[HttpGet]
[Consumers("Vendor")]
[ApiExplorerSettings(GroupName = "vendors")] // <-- Add this line
public IActionResult GetProductsForVendor()
{
    return Ok("This is the list of products for vendor");
}

[HttpGet]
[Consumers("Customer")]
[ApiExplorerSettings(GroupName = "customers")] // <-- Add this line
public IActionResult GetProdcutsForCustomer()
{
    return Ok("This is the list of products for customer");
}

Or annotate the minimal API endpoints with WithGroupName method

app.MapGet("/hello", () => "Hello Vendor!")
    .RequireConsumers("Vendor")
    .WithGroupName("vendors"); // <-- Add this line

app.MapGet("/hello", () => "Hello Customer!")
    .RequireConsumers("Customer")
    .WithGroupName("customers"); // <-- Add this line

Then create an operation filter to add the X-Consumer header to the swagger UI

public class AddConsumerHeadersOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        operation.Parameters.Add(new OpenApiParameter
        {
            Name = "X-Consumer",
            In = ParameterLocation.Header,
            Required = true,
            Examples = new Dictionary<String, OpenApiExample>
            {
                { "Vendor", new OpenApiExample { Value = new OpenApiString("Vendor") } },
                { "Customer", new OpenApiExample { Value = new OpenApiString("Customer") } },
            }
        });
    }
}

then register it in the swagger configuration

builder.Services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddConsumerHeadersOperationFilter>(); // <-- Add this line
    options.SwaggerDoc("vendors", new OpenApiInfo { Title = "E-commerce Vendors API", Version = "v1" });
    options.SwaggerDoc("customers", new OpenApiInfo { Title = "E-commerce Customers API", Version = "v1" });
});

consumers.aspnetcore's People

Contributors

ahmedkabbary avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.