ikyriak / IdempotentAPI

A .NET library that handles the HTTP write operations (POST and PATCH) that can affect only once for the given request data and idempotency-key by using an ASP.NET Core attribute (filter).
MIT License
270 stars 39 forks source link

For Minimal APIs, how to configure the idempotent options for a specific endpoint, not generally? (cache deadline, etc) #73

Closed JonasLeetTheWay closed 3 months ago

JonasLeetTheWay commented 5 months ago

For Minimal APIs, how to configure the idempotent options for a specific endpoint, not generally? (cache deadline, etc)

In controllers, we can use attribute to configure, but how about Minimal APIs?

ikyriak commented 5 months ago

Hello @JonasLeetTheWay,

Thank you for opening this issue.

Currently, we can only set global idempotency options for MinimalAPIs. However, I have prepared a possible solution using an IIdempotencyOptionsProvider to provide the idempotency options based on our needs (IdempotentAPI.MinimalAPI v3.1.0-issue-73-01).

Could this work in your case?

For example, we could return the IIdempotencyOptions based on the requested path.

public class IdempotencyOptionsProvider : IIdempotencyOptionsProvider
{
    public IIdempotencyOptions GetIdempotencyOptions(IHttpContextAccessor httpContextAccessor)
    {
        switch (httpContextAccessor?.HttpContext?.Request.Path)
        {
            case "/v6/TestingIdempotentAPI/test":
                return new IdempotencyOptions()
                {
                    ExpireHours = 1,
                };
        }

        return new IdempotencyOptions();
    }
}

We will need to register IdempotencyOptionsProvider in the Program.cs:

builder.Services.AddIdempotentMinimalAPI(new IdempotencyOptionsProvider());
ikyriak commented 3 months ago

This issue is resolved in the following stable version: