Comments (10)
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.
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.
from mollieapi.
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.
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.
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.
// 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.
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.
Hi @Viincenttt good work. I will test it ASAP.
from mollieapi.
Hi @Viincenttt I've tested all my hosts and all are starting and accepting payments.
from mollieapi.
Good to hear that all is working 👍
from mollieapi.
Related Issues (20)
- Restructure folder structure to make the repository in-line with modern open-source repositories HOT 1
- Update project to .NET standard 2.0 HOT 2
- Add extension method to register all Mollie API clients using dependency injection HOT 1
- IPaymentClient should inherit from IDisposable HOT 7
- Support for get payment refund HOT 2
- Pagination on GetChargebacksListAsync HOT 3
- Wallet API support HOT 1
- GetInvoiceListAsync : Unprocessable Entity - Non-existent query parameter "include" for this API call HOT 4
- Add support for Klarna One payment method HOT 1
- UpdateCustomerAsync throws error 502 HOT 3
- Create customer throws error when using the testmode property in the request HOT 2
- Enable nullable references in Mollie.Api project HOT 1
- Add an .editorconfig file to help maintain styling HOT 2
- Some issues with list balance transactions HOT 3
- Add support for refunding split payments HOT 3
- Support for Lines in CreatePayment HOT 3
- Veranderingen in 4.0.0 versie. HOT 2
- Add support for HTTP error code "Conflict" HOT 2
- Nullable issue in CreditCardPaymentResponse HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mollieapi.