-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement idempotency for B/P requests (#207)
- Loading branch information
1 parent
f8a4150
commit c909ec7
Showing
6 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Examples/BookingSystem.AspNetCore/Stores/IdempotencyStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using OpenActive.Server.NET.OpenBookingHelper; | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Runtime.Caching; | ||
|
||
namespace BookingSystem | ||
{ | ||
public class AcmeIdempotencyStore : IdempotencyStore | ||
{ | ||
private readonly ObjectCache _cache = MemoryCache.Default; | ||
|
||
protected override ValueTask<string> GetSuccessfulOrderCreationResponse(string idempotencyKey) | ||
{ | ||
return new ValueTask<string>((string)_cache.Get(idempotencyKey)); | ||
} | ||
|
||
protected override ValueTask SetSuccessfulOrderCreationResponse(string idempotencyKey, string responseJson) | ||
{ | ||
var policy = new CacheItemPolicy(); | ||
policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5); | ||
_cache.Set(idempotencyKey, responseJson, policy); | ||
return new ValueTask(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
OpenActive.Server.NET/OpenBookingHelper/Stores/IdempotencyStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using OpenActive.NET; | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace OpenActive.Server.NET.OpenBookingHelper | ||
{ | ||
public abstract class IdempotencyStore | ||
{ | ||
protected abstract ValueTask<string> GetSuccessfulOrderCreationResponse(string idempotencyKey); | ||
protected abstract ValueTask SetSuccessfulOrderCreationResponse(string idempotencyKey, string responseJson); | ||
|
||
internal ValueTask<string> GetSuccessfulOrderCreationResponse(OrderIdComponents orderId, string orderJson) { | ||
return GetSuccessfulOrderCreationResponse(CalculateIdempotencyKey(orderId, orderJson)); | ||
} | ||
|
||
internal ValueTask SetSuccessfulOrderCreationResponse(OrderIdComponents orderId, string orderJson, string responseJson) { | ||
return SetSuccessfulOrderCreationResponse(CalculateIdempotencyKey(orderId, orderJson), responseJson); | ||
} | ||
|
||
protected string CalculateIdempotencyKey(OrderIdComponents orderId, string orderJson) { | ||
return $"{orderId.ClientId}|{orderId.uuid}|{orderId.OrderType.ToString()}|{ComputeSHA256Hash(orderJson)}"; | ||
} | ||
|
||
protected static string ComputeSHA256Hash(string text) | ||
{ | ||
using (var sha256 = SHA256.Create()) | ||
{ | ||
return BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(text))).Replace("-", ""); | ||
} | ||
} | ||
} | ||
} |