Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Aug 29, 2025

Please provide the issue number

Issue number: #795

Summary

Changes

  • Introduce ResponseHook delegate to IdempotencyOptions and builder
  • Invoke response hook on idempotent responses in IdempotencyAspectHandler
  • Update documentation with usage examples and best practices for response hooks
  • Add integration tests for response hook execution and exception handling

This pull request introduces a new "response hook" feature to the idempotency utility, allowing users to manipulate the returned data for idempotent operations. It adds configuration options, implements the hook logic in the idempotency handler.

Response Hook Feature

  • Added a ResponseHook delegate property to the IdempotencyOptions class, enabling users to specify a function to modify responses for idempotent operations.
  • Implemented the response hook logic in IdempotencyAspectHandler.HandleForStatus, ensuring the hook is called only for idempotent responses and gracefully handles exceptions or type mismatches.

Configuration and Builder Updates

  • Extended the IdempotencyOptionsBuilder with a WithResponseHook method, allowing users to set the hook during configuration.
  • Updated builder and documentation comments for consistency and clarity regarding usage.
Idempotency.Config()
    .WithConfig(IdempotencyOptions.Builder()
        .WithEventKeyJmesPath("powertools_json(body).address")
        .WithResponseHook((responseData, dataRecord) => {
            if (responseData is APIGatewayProxyResponse proxyResponse)
            {
                proxyResponse.Headers ??= new Dictionary<string, string>();
                proxyResponse.Headers["x-idempotency-response"] = "true";
                proxyResponse.Headers["x-idempotency-expiration"] = dataRecord.ExpiryTimestamp.ToString();
                return proxyResponse;
            }
            return responseData;
        })
        .Build())
    .WithPersistenceStore(DynamoDBPersistenceStore.Builder()
        .WithTableName(Environment.GetEnvironmentVariable("IDEMPOTENCY_TABLE"))
        .Build())
    .Configure();

User experience

Please share what the user experience looks like before and after this change

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

- Introduce ResponseHook delegate to IdempotencyOptions and builder
- Invoke response hook on idempotent responses in IdempotencyAspectHandler
- Update documentation with usage examples and best practices for response hooks
- Add integration tests for response hook execution and exception handling
- Improve XML doc comments for builder and persistence store
@boring-cyborg boring-cyborg bot added area/idempotency documentation Improvements or additions to documentation tests labels Aug 29, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 29, 2025
@github-actions github-actions bot added the feature New features or minor changes label Aug 29, 2025
@hjgraca hjgraca linked an issue Aug 29, 2025 that may be closed by this pull request
2 tasks
Copy link

codecov bot commented Aug 29, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.47%. Comparing base (e1c72a2) to head (87f6387).
⚠️ Report is 30 commits behind head on develop.

Files with missing lines Patch % Lines
...s.Idempotency/Internal/IdempotencyAspectHandler.cs 76.92% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #968   +/-   ##
========================================
  Coverage    77.47%   77.47%           
========================================
  Files          271      271           
  Lines        10886    10900   +14     
  Branches      1284     1287    +3     
========================================
+ Hits          8434     8445   +11     
- Misses        2040     2041    +1     
- Partials       412      414    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

/// <summary>
/// Constructor of <see cref="IdempotencyOptions"/>.
/// </summary>
/// <param name="eventKeyJmesPath"></param>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar alerted for the constructor now having more that 7 arguments. Refactor to builder pattern.

/// </summary>
private Func<object, AWS.Lambda.Powertools.Idempotency.Persistence.DataRecord, object> _responseHook;

/// <summary>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now properties for builder pattern

Copy link

@hjgraca hjgraca requested a review from phipag August 29, 2025 12:56
@hjgraca hjgraca merged commit 7d26f07 into develop Sep 2, 2025
9 checks passed
@hjgraca hjgraca deleted the feature/idempotency-hooks branch September 2, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/idempotency documentation Improvements or additions to documentation feature New features or minor changes size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add support for response hooks in Idempotency
2 participants