Giter VIP home page Giter VIP logo

Comments (10)

Viincenttt avatar Viincenttt commented on July 4, 2024 1

One idea that I currently have is to be able to specify a custom idempotency key inside a using block. This would work very similar to the structure that logger.BeginScope uses. For example:

using IPaymentClient paymentClient = new PaymentClient("api-key");
using (paymentClient.WithIdempotencyKey("my-custom-key"))
{
    PaymentRequest paymentRequest = new PaymentRequest() {
        Amount = new Amount(Currency.EUR, "100.00"),
        Description = "description",
        RedirectUrl = "url"
    };

     await paymentClient.CreatePaymentAsync(paymentRequest);
}

All requests within the using scope will use the custom idempotency key that you set.

Let me know what you think :)

from mollieapi.

Viincenttt avatar Viincenttt commented on July 4, 2024

Hi,

Currently this is not possible, but I definitely like the idea of being able to configure the idempotency key. However, I don't want to add an optional idempotency key to all the API methods, so I'm not yet sure about what the best way would be to implement this. I'll have to think about it for a bit.

If you have any suggestions on how to implement it, feel free to make a suggestion.

Kind regards,
Vincent

from mollieapi.

JSCProjects avatar JSCProjects commented on July 4, 2024

from mollieapi.

JSCProjects avatar JSCProjects commented on July 4, 2024

If you are using dependency injection then the first line will not work. I'm wondering how you would implement the second line. IMHO this looks better then what I've thought out. Is it possible to implement this also in v2? Because v3 gives us problems because it's depending on new v7 packages from Microsoft and that doesn't work in all .NET 6 hosts.

Thanx in advance

from mollieapi.

Viincenttt avatar Viincenttt commented on July 4, 2024

The first line is purely for demonstration. The payment client can be instantiated through dependency injection or by simply creating a new object like I did in this example.

The implementation of the second line is going to be interesting indeed. I think it should be possible to store the custom idempotency key in a AsyncLocal variable. This ensures that the value is stored in a thread-safe matter and contexts between different threads will never overlap. The variable needs to be reset when the using block ends, so it should be contained inside a class that implements the IDisposable interface.

It sounds more complicated then it is, I think something like this should do the trick (untested):

public class AsyncLocalVariable<T> : IDisposable
{
    private readonly AsyncLocal<T> _asyncLocal = new AsyncLocal<T>();

    public AsyncLocalVariable(T value)
    {
        _asyncLocal.Value = value;
    }

    public void Set(T value)
    {
        _asyncLocal.Value = value;
    }

    publid T Get() 
    {
         return _asyncLocal.Value;
    }

    public void Dispose()
    {
        _asyncLocal.Value = null;
    }
}

// In BaseMollieClient.cs
private readonly AsyncLocalVariable<string> _idempotencyKey = new AsyncLocalVariable<string>(null);
public IDisposable WithIdempotencyKey(string value) {
    _idempotencyKey.Set(value);
    return _idempotencyKey;
}

protected virtual HttpRequestMessage CreateHttpRequest(HttpMethod method, string relativeUri, HttpContent? content = null) 
{
    ...
    var idemPotencyKey = _idempotencyKey.Get() ?? Guid.NewGuid().ToString();
    httpRequest.Headers.Add("Idempotency-Key", idemPotencyKey);
    ....
}

I'll play around with this idea next weekend.

I'm also interested in hearing the issues you have with v3 of the library. Can you tell me to what platform you are publishing and what errors you are getting? Perhaps there is something I can do in the library side to fix this.

from mollieapi.

Viincenttt avatar Viincenttt commented on July 4, 2024

Hi @JSCProjects ,

I have added the feature to the development branch and plan to release a new version on NuGet later this weekend. Example code on how to use the feature can be seen in the integration test linked below. I have also downgraded the Microsoft dependencies to version 6, so hopefully you'll be able to upgrade to v3 of the Mollie library.

I'll update the issue when the new version has been released.

https://github.com/Viincenttt/MollieApi/pull/352/files#diff-07edb06086f59977e3efd91607ee0a9b93f135e9036de12bbd33073d1b5de448

// Given: we create a payment request with only the required parameters
PaymentRequest paymentRequest = new PaymentRequest() {
	Amount = new Amount(Currency.EUR, "100.00"),
	Description = "Description",
	RedirectUrl = DefaultRedirectUrl
};

// When: We send the payment request to Mollie
using (_paymentClient.WithIdempotencyKey("my-idempotency-key"))
{
	PaymentResponse firstAttempt = await _paymentClient.CreatePaymentAsync(paymentRequest);
	PaymentResponse secondAttempt = await _paymentClient.CreatePaymentAsync(paymentRequest);

	// Then: Make sure the responses have the same payment Id
	firstAttempt.Id.Should().Be(secondAttempt.Id);
}

from mollieapi.

Viincenttt avatar Viincenttt commented on July 4, 2024

This is now live in version 3.6.0 of the library. Keep in mind I downgraded the Microsoft dependencies from version 7 to version 6. Please let me know if this solved the issues you had with .NET6 hosts.

from mollieapi.

JSCProjects avatar JSCProjects commented on July 4, 2024

Hi @Viincenttt good work. I will test it ASAP.

from mollieapi.

JSCProjects avatar JSCProjects commented on July 4, 2024

Hi @Viincenttt I've tested all my hosts and all are starting and accepting payments.

from mollieapi.

Viincenttt avatar Viincenttt commented on July 4, 2024

Good to hear that all is working 👍

from mollieapi.

Related Issues (20)

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.