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
253 stars 38 forks source link

FastEndpoints support? #72

Closed CaptainPowerTurtle closed 2 months ago

CaptainPowerTurtle commented 4 months ago

Have anyone tried to use this with FastEndpoints?: https://fast-endpoints.com/

I followed the setup/examples for the minimal api, and it is almost working. It is caching the response, but when i call the endpoint again, using the same key, i get no data back.

I tested it with the MinimalApi endpoints, and it works as it should.

ikyriak commented 3 months ago

Hello @CaptainPowerTurtle,

Thank you for opening this issue. I have not tried using it with FastEndpoints, but I will try to when I have some free time.

dj-nitehawk commented 3 months ago

@ikyriak i'm the lead maintainer of fastendpoints and thought you might find the following info interesting:

screen-record-1716003403

so if there's any interest in adding FE support, you'd have to grab the response DTO instance from the HttpContext.Items dictionary for storing in the cache, since the FE minimal api delegate doesn't return anything itself.

let me know if there's anything we can do from our end to facilitate compatibility.

thanks!

ikyriak commented 3 months ago

Hello @dj-nitehawk,

Thank you for providing me with this helpful information 🙏

ikyriak commented 3 months ago

Hello @dj-nitehawk,

When using Minimal APIs, in the InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) method on an IEndpointFilter, I have access to the actual request class (e.g., the DTO in a POST request).

However, this is not available when using FastEndpoints. Is there a way to get the request body? image

This is needed not to accept the same idempotency key for different request payloads. image

dj-nitehawk commented 3 months ago

i see... the trouble is, the following is how fastendpoints registers the minimal api delegates for endpoints:

https://github.com/FastEndpoints/FastEndpoints/blob/3422ada67cfd628527acb21034dae4f6a513607c/Src/Library/Main/MainExtensions.cs#L117-L120

so, you won't have access to a typed request dto at the outer level of IEndpointFilters. the request dto is created inside FE's endpoint handler.

you could maybe read the request stream via HttpContext.Request.Body for the purpose of building the cache-key. that is what we're doing in our idempotency implementation. which, may not work for you since our implementation is based on the OutputCaching middleware.

i'm assuming you need access to the request dto for building the cache-key by iterating the properties of the dto via reflection yes? in which case, i'm out of ideas 🤔

ikyriak commented 2 months ago

Thank you for your response, @dj-nitehawk. When FastEndpoints are used, I will read the request stream via HttpContext.Request.Body. 🙏

ikyriak commented 2 months ago

Hello @CaptainPowerTurtle,

I have pushed the following prerelease versions that support FastEndpoints:

ikyriak commented 2 months ago

This issue is resolved in the following stable version: