forked from aws-powertools/powertools-lambda-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIdempotencyOptionsBuilder.cs
136 lines (121 loc) · 4.87 KB
/
IdempotencyOptionsBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
namespace AWS.Lambda.Powertools.Idempotency;
/// <summary>
/// Create a builder that can be used to configure and create <see cref="IdempotencyOptions"/>
/// </summary>
public class IdempotencyOptionsBuilder
{
/// <summary>
/// Default maximum number of items in the local cache.
/// </summary>
private readonly int _localCacheMaxItems = 256;
/// <summary>
/// Local cache enabled
/// </summary>
private bool _useLocalCache;
/// <summary>
/// Default expiration in seconds.
/// </summary>
private long _expirationInSeconds = 60 * 60; // 1 hour
/// <summary>
/// Event key JMESPath expression.
/// </summary>
private string _eventKeyJmesPath;
/// <summary>
/// Payload validation JMESPath expression.
/// </summary>
private string _payloadValidationJmesPath;
/// <summary>
/// Throw exception if no idempotency key is found.
/// </summary>
private bool _throwOnNoIdempotencyKey;
/// <summary>
/// Default Hash function
/// </summary>
private string _hashFunction = "MD5";
/// <summary>
/// Initialize and return an instance of IdempotencyConfig.
/// Example:
/// IdempotencyConfig.Builder().WithUseLocalCache().Build();
/// This instance must then be passed to the Idempotency.Config:
/// Idempotency.Config().WithConfig(config).Configure();
/// </summary>
/// <returns>an instance of IdempotencyConfig</returns>
public IdempotencyOptions Build() =>
new(_eventKeyJmesPath,
_payloadValidationJmesPath,
_throwOnNoIdempotencyKey,
_useLocalCache,
_localCacheMaxItems,
_expirationInSeconds,
_hashFunction);
/// <summary>
/// A JMESPath expression to extract the idempotency key from the event record.
/// See <a href="https://jmespath.org/">https://jmespath.org/</a> for more details.
/// </summary>
/// <param name="eventKeyJmesPath">path of the key in the Lambda event</param>
/// <returns>the instance of the builder (to chain operations)</returns>
public IdempotencyOptionsBuilder WithEventKeyJmesPath(string eventKeyJmesPath)
{
_eventKeyJmesPath = eventKeyJmesPath;
return this;
}
/// <summary>
/// Whether to locally cache idempotency results, by default false
/// </summary>
/// <param name="useLocalCache">Indicate if a local cache must be used in addition to the persistence store.</param>
/// <returns>the instance of the builder (to chain operations)</returns>
public IdempotencyOptionsBuilder WithUseLocalCache(bool useLocalCache)
{
_useLocalCache = useLocalCache;
return this;
}
/// <summary>
/// A JMESPath expression to extract the payload to be validated from the event record.
/// See <a href="https://jmespath.org/">https://jmespath.org/</a> for more details.
/// </summary>
/// <param name="payloadValidationJmesPath">JMES Path of a part of the payload to be used for validation</param>
/// <returns>the instance of the builder (to chain operations)</returns>
public IdempotencyOptionsBuilder WithPayloadValidationJmesPath(string payloadValidationJmesPath)
{
_payloadValidationJmesPath = payloadValidationJmesPath;
return this;
}
/// <summary>
/// Whether to throw an exception if no idempotency key was found in the request, by default false
/// </summary>
/// <param name="throwOnNoIdempotencyKey">boolean to indicate if we must throw an Exception when
/// idempotency key could not be found in the payload.</param>
/// <returns>the instance of the builder (to chain operations)</returns>
public IdempotencyOptionsBuilder WithThrowOnNoIdempotencyKey(bool throwOnNoIdempotencyKey)
{
_throwOnNoIdempotencyKey = throwOnNoIdempotencyKey;
return this;
}
/// <summary>
/// The number of seconds to wait before a record is expired
/// </summary>
/// <param name="duration">expiration of the record in the store</param>
/// <returns>the instance of the builder (to chain operations)</returns>
public IdempotencyOptionsBuilder WithExpiration(TimeSpan duration)
{
_expirationInSeconds = (long)duration.TotalSeconds;
return this;
}
/// <summary>
/// Function to use for calculating hashes, by default MD5.
/// </summary>
/// <param name="hashFunction">Can be any algorithm supported by HashAlgorithm.Create</param>
/// <returns>the instance of the builder (to chain operations)</returns>
#if NET8_0_OR_GREATER
[Obsolete("Idempotency uses MD5 and does not support other hash algorithms.")]
#endif
public IdempotencyOptionsBuilder WithHashFunction(string hashFunction)
{
#if NET6_0
// for backward compability keep this code in .net 6
_hashFunction = hashFunction;
#endif
return this;
}
}