Skip to content
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

feat: introduce base client that utilizes context #196

Conversation

natebrennand
Copy link
Contributor

@natebrennand natebrennand commented Oct 30, 2022

Works on #98

This PR introduces alternative base client interfaces, BaseClientWithCtx and RequestHandlerWithCtx, that allow context.Context objects to be provided to every API request. This will allow the Twilio SDK to support context cancellations, and for custom HTTP clients to access the context while executing the request.

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

@natebrennand
Copy link
Contributor Author

natebrennand commented Oct 30, 2022

closing in favor of #197

Needs to be landed before twilio/twilio-oai-generator#301 tests will work.

Copy link
Contributor

@childish-sambino childish-sambino left a comment

Choose a reason for hiding this comment

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

Overall changes look good. Just a question and a nit.


func (w wrapperClient) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
return w.SendRequest(method, rawURL, data, headers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be calling SendRequestWithCtx instead? Otherwise the context is swallowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, there really should've been a comment over wrapperClient to make this more explicit.

wrapperClient is wrapping the older BaseClient implementation. So this method is the new interface passing though to the context-free client.
This enables the SDK to use the BaseClientWithCtx interface everywhere since we can transform the older BaseClient

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, understanding better now the intent. I'm thinking it would be a little easier to understand the implications by actually having the wrapperClient send through the provided context (which is what I was expecting in the initial review).

wrapping/upgrading is adding the ability to an existing client/handler to pass a context through (by adding an implementation for SendRequestWithCtx to it). So it no longer becomes a noop that swallows the context. That way if I wanted to take an existing integration and start adding context to API calls, all I'd need to do is switch to use the context-based version of the API method (no initialization changes needed to the client/service/handler).

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So there are a few different ways for folks to instantiate a twiliogo client, in all but one the *WithCtx methods will operate as expected. There's just one scenario where it will be misleading to the integration developer, but I think it's the most sophisticated setup so there's the highest likelihood they will identify this.

Integrations where the client will "just work" with *WithCtx methods:

The only scenario where a context may be provided but not used is when the integration is using client.RequestHandler and has provided a custom BaseClient. If this is the case, there's no way for the library to pass the context in unless we introduce breaking changes, since the specified client by the integration (which is opaque to the SDK) has no method available to ingest a context.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, now I I understand. Thanks for bearing with me.

client/base_client.go Show resolved Hide resolved
Copy link
Contributor Author

@natebrennand natebrennand left a comment

Choose a reason for hiding this comment

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

I'll push a commit documenting wrapperClient shortly


func (w wrapperClient) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
return w.SendRequest(method, rawURL, data, headers)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, there really should've been a comment over wrapperClient to make this more explicit.

wrapperClient is wrapping the older BaseClient implementation. So this method is the new interface passing though to the context-free client.
This enables the SDK to use the BaseClientWithCtx interface everywhere since we can transform the older BaseClient

client/base_client.go Show resolved Hide resolved
@natebrennand natebrennand force-pushed the natebrennand/add-context-methods-to-base-client branch from 1385df6 to fa94b6c Compare November 7, 2022 18:19
Copy link
Contributor

@childish-sambino childish-sambino left a comment

Choose a reason for hiding this comment

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

🏅


func (w wrapperClient) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
return w.SendRequest(method, rawURL, data, headers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, now I I understand. Thanks for bearing with me.

@childish-sambino childish-sambino merged commit b61e334 into twilio:main Nov 8, 2022
childish-sambino pushed a commit that referenced this pull request Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants