Skip to content

Conversation

@armando-rodriguez-cko
Copy link
Contributor

feat: Add Context Support and Improve Readability

Description:

This PR introduces WithContext variants for all publicly exposed methods in the Go SDK clients. These changes provide developers with fine-grained control over timeouts and cancellations using Go’s native context.Context. Additionally, method signatures and calls have been refactored for improved readability and consistency.


Key Changes:

Context Support

  • Added WithContext versions of all public methods across SDK clients.
    • Examples: CreateWithContext, GetWithContext, UpdateWithContext, DeleteWithContext, etc.
  • Existing methods now delegate to their WithContext counterparts using context.Background().

Improved Readability

  • Multi-argument method declarations and calls are now split across multiple lines to improve clarity and reduce merge conflicts.

No Functional Changes

  • All updates are fully backward-compatible.
  • Existing integrations will continue to work without any required code changes.

Impact

  • Better Flexibility: Enables context-aware request management (timeouts, cancellations, etc.).
  • Cleaner Code: Easier to read and maintain.
  • No Breaking Changes: All previous methods remain intact.

Testing

  • Ensured all methods compile and work as expected.
  • Verified WithContext versions internally delegate to the correct context-aware HTTP client functions (PostWithContext, GetWithContext, etc.).
  • Manual validation for backward compatibility.

Notes

  • These changes align the SDK with idiomatic Go practices.
  • Recommended to update usage examples and documentation to promote WithContext versions going forward.

@armando-rodriguez-cko armando-rodriguez-cko requested a review from a team March 21, 2025 13:13
@armando-rodriguez-cko armando-rodriguez-cko linked an issue Mar 21, 2025 that may be closed by this pull request
1 task
@armando-rodriguez-cko armando-rodriguez-cko force-pushed the beta/context-by-jamieaitken branch 3 times, most recently from 3647be0 to f015738 Compare March 28, 2025 10:21
@armando-rodriguez-cko armando-rodriguez-cko force-pushed the beta/context-by-jamieaitken branch from f015738 to e9d71b4 Compare April 3, 2025 08:53
@armando-rodriguez-cko armando-rodriguez-cko force-pushed the beta/context-by-jamieaitken branch from e9d71b4 to 91f3712 Compare April 11, 2025 08:11
@armando-rodriguez-cko
Copy link
Contributor Author

@jamieaitken next week we will release this version, thank you.

@jamieaitken
Copy link

@armando-rodriguez-cko great, thanks for keeping me in the loop!

@armando-rodriguez-cko armando-rodriguez-cko force-pushed the beta/context-by-jamieaitken branch from 91f3712 to b6f1402 Compare April 22, 2025 16:00
@armando-rodriguez-cko
Copy link
Contributor Author

After reviewing and evaluating this PR, it is closed for now due to the need to add more context tests and better cover all areas and endpoints with context. The branch will remain active to add these fixes; we apologise for the inconvenience.

@sonarqubecloud
Copy link

@armando-rodriguez-cko
Copy link
Contributor Author

@jamieaitken reopened and rebased, we will work on this soon.

jamieaitken and others added 2 commits January 28, 2026 09:59
* Add Context to HTTP layer

* Add Context to Customers client

* Add Context to Disputes client

* Add Context to Events client

* Add Context to Hosted client

* Add Context to Instruments client

* Add Context to Links client

* Add Context to Payments client

* Add Context to Reconciliation client

* Add Context to Sources client

* Add Context to Tokens client

* Add Context to Webhook client

* Add Context to iDEAL client

* Add Context to Klarna client

* Add Context to SEPA client

* Update tests

* Add Context to NAS Payments

---------

Co-authored-by: Armando Rodríguez <[email protected]>
@armando-rodriguez-cko armando-rodriguez-cko force-pushed the beta/context-by-jamieaitken branch from 8386d75 to 669a10e Compare January 28, 2026 09:01
* Subdomain added for access validation endpoint

* Access domain test updates

---------

Co-authored-by: david ruiz <[email protected]>
- Subdomain added for access validation endpoint
- Access domain test updates
…ontext support

- Added TestGetAllReports with 3 test cases: success, auth error, API error
- Added TestGetReportDetails with 3 test cases: success, auth error, not found
- Added TestGetReportFile with 3 test cases: success, auth error, not found
- All tests follow established mock pattern with GetWithContext
- Resolves missing test coverage for reports package
- Tests pass: 9/9 passed
- TestContextCancellation: verify context.Canceled is propagated
- TestContextTimeout: verify timeout triggers fast abort
- TestContextTimeoutSuccess: verify generous timeout allows completion
- TestContextPropagation: verify context values reach HTTP server
- TestDefaultMethodUsesBackgroundContext: verify fallback behavior
- TestConcurrentRequestsWithDifferentContexts: verify isolation
- TestContextPostWithCancellation/PutWithCancellation/DeleteWithCancellation: all HTTP verbs
- TestContextDeadline: verify absolute deadline handling
- Tests pass: 10/10
- Added ForwardPaymentWithContext method
- Updated ForwardPaymentWithTests mock pattern
- Backward compatible: Forward() calls ForwardPaymentWithContext(context.Background())
- Tests pass: 6/6
- Added CreatePaymentContextWithContext method
- Backward compatible: CreatePaymentContext() uses context.Background()
- Added four WithContext methods: RequestPaymentSessionWithContext, GetPaymentSessionWithContext, UpdatePaymentSessionWithContext, CompletePaymentSessionWithContext
- Updated test mocks for PostWithContext/PutWithContext pattern
- All tests pass: 12/12
- Added four WithContext methods: CreatePaymentSetupWithContext, UpdatePaymentSetupWithContext, GetPaymentSetupWithContext, ConfirmPaymentSetupWithContext
- Updated test mocks for PostWithContext/PutWithContext pattern
- All tests pass: 12/12
- Fixed type name inconsistencies: HostedPayment* -> PaymentHosted*
- Added CreatePaymentHostedWithContext method
- Updated test mocks to use corrected type names
- Pattern: CreatePaymentHosted() calls CreatePaymentHostedWithContext(context.Background())
- Tests pass: 6/6
- Added GetAllReportsWithContext, GetReportDetailsWithContext, GetReportFileWithContext methods
- Backward compatible: existing methods use context.Background()
…vidence

- Added GetCompiledSubmittedEvidenceWithContext method
- Backward compatible: GetCompiledSubmittedEvidence() uses context.Background()
- Added IncrementAuthorizationWithContext method
- Added CapturePaymentWithoutRequestWithContext method
- Backward compatible: existing methods use context.Background()
…houtRequest

- Added CapturePaymentWithoutRequestWithContext method
- Backward compatible: CapturePaymentWithoutRequest() uses context.Background()
@armando-rodriguez-cko
Copy link
Contributor Author

@jamieaitken new attempt 🤞🏼

- Updated doRequest to accept and pass context to handleResponse
- Updated handleResponse to accept context and pass to readBody
- Updated readBody to check context.Done() before reading body
- Ensures context cancellation/timeout is respected during response parsing
- Added 5 new tests to verify context propagation:
  * TestReadBodyContextCancellation: readBody respects cancelled context
  * TestHandleResponseContextPropagation: handleResponse receives context
  * TestHandleResponseContextCancellation: handleResponse aborts on cancellation
  * TestContextPropagationThroughDoRequest: full propagation through chain
  * TestFullRequestContextFlow: end-to-end context flow
- All 15/15 client tests pass
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Adding context.Context to all API methods

4 participants