-
Notifications
You must be signed in to change notification settings - Fork 26
feat: idempotent function #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hjgraca
merged 2 commits into
aws-powertools:develop
from
hossambarakat:feature/idempotent-function
Jul 14, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
libraries/src/AWS.Lambda.Powertools.Idempotency/IdempotencyKeyAttribute.cs
This file contains hidden or 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 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace AWS.Lambda.Powertools.Idempotency; | ||
|
||
/// <summary> | ||
/// IdempotencyKey is used to signal that a method parameter is used as a key for idempotency. | ||
/// Must be used in conjunction with the Idempotency attribute. | ||
/// | ||
/// Example: | ||
/// | ||
/// [Idempotent] | ||
/// private Basket SubMethod([IdempotencyKey]string magicProduct, Product p) { ... } | ||
/// Note: This annotation is not needed when the method only has one parameter. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public class IdempotencyKeyAttribute: Attribute | ||
{ | ||
|
||
} |
This file contains hidden or 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 hidden or 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
41 changes: 41 additions & 0 deletions
41
...ies/tests/AWS.Lambda.Powertools.Idempotency.Tests/Handlers/IdempotencyInternalFunction.cs
This file contains hidden or 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,41 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Lambda.Core; | ||
using AWS.Lambda.Powertools.Idempotency.Tests.Model; | ||
|
||
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers; | ||
|
||
/// <summary> | ||
/// Simple Lambda function with Idempotent attribute on a sub method (not the Lambda handler one) | ||
/// </summary> | ||
public class IdempotencyInternalFunction | ||
{ | ||
public Basket HandleRequest(Product input, ILambdaContext context) | ||
{ | ||
return CreateBasket("fake", input); | ||
} | ||
|
||
[Idempotent] | ||
private Basket CreateBasket([IdempotencyKey]string magicProduct, Product p) | ||
{ | ||
IsSubMethodCalled = true; | ||
Basket b = new Basket(p); | ||
b.Add(new Product(0, magicProduct, 0)); | ||
return b; | ||
} | ||
|
||
public bool IsSubMethodCalled { get; private set; } = false; | ||
} |
36 changes: 36 additions & 0 deletions
36
...WS.Lambda.Powertools.Idempotency.Tests/Handlers/IdempotencyInternalFunctionInternalKey.cs
This file contains hidden or 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,36 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Lambda.Core; | ||
using AWS.Lambda.Powertools.Idempotency.Tests.Model; | ||
|
||
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers; | ||
|
||
/// <summary> | ||
/// Simple Lambda function with Idempotent attribute on a sub method (not the Lambda handler one) | ||
/// </summary> | ||
public class IdempotencyInternalFunctionInternalKey | ||
{ | ||
public Basket HandleRequest(Product input, ILambdaContext context) | ||
{ | ||
return CreateBasket(input); | ||
} | ||
|
||
[Idempotent] | ||
private Basket CreateBasket(Product p) | ||
{ | ||
return new Basket(p); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ts/AWS.Lambda.Powertools.Idempotency.Tests/Handlers/IdempotencyInternalFunctionInvalid.cs
This file contains hidden or 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,40 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Lambda.Core; | ||
using AWS.Lambda.Powertools.Idempotency.Tests.Model; | ||
|
||
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers; | ||
|
||
/// <summary> | ||
/// Simple Lambda function with Idempotent attribute on a sub method. | ||
/// This one is invalid as there are two parameters and IdempotencyKey attribute | ||
/// is not used to specify which one will be used as a key for persistence. | ||
/// </summary> | ||
public class IdempotencyInternalFunctionInvalid | ||
{ | ||
public Basket HandleRequest(Product input, ILambdaContext context) | ||
{ | ||
return CreateBasket("fake", input); | ||
} | ||
|
||
[Idempotent] | ||
private Basket CreateBasket(string magicProduct, Product p) | ||
{ | ||
Basket b = new Basket(p); | ||
b.Add(new Product(0, magicProduct, 0)); | ||
return b; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...tests/AWS.Lambda.Powertools.Idempotency.Tests/Handlers/IdempotencyInternalFunctionVoid.cs
This file contains hidden or 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,40 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Lambda.Core; | ||
using AWS.Lambda.Powertools.Idempotency.Tests.Model; | ||
|
||
namespace AWS.Lambda.Powertools.Idempotency.Tests.Handlers; | ||
|
||
/// <summary> | ||
/// Simple Lambda function with Idempotent attribute a sub method. | ||
/// This one is invalid because the annotated method return type is void, thus we cannot store any response. | ||
/// </summary> | ||
public class IdempotencyInternalFunctionVoid | ||
{ | ||
public Basket HandleRequest(Product input, ILambdaContext context) | ||
{ | ||
Basket b = new Basket(input); | ||
AddProduct("fake", b); | ||
return b; | ||
} | ||
|
||
[Idempotent] | ||
private void AddProduct([IdempotencyKey] string productName, Basket b) | ||
{ | ||
b.Add(new Product(0, productName, 0)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small note: ILambdaContext is an optional argument, which will return false on IsPlacedOnRequestHandler, but from the looks of it the if there is only one argument it is also valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got it right, either one parameter or 2 parameters where the second parameter is
ILambdaContext