add Context counterparts methods for Client - #161
Open
dungdm93 wants to merge 7 commits into
Open
Conversation
Contributor
Author
Member
|
why do you move the creation of ctx.background from net.http package into our client? what is the benefit? |
dungdm93
commented
May 12, 2025
| } | ||
|
|
||
| func (s *RepositoryService[R]) Create(repo R) error { | ||
| return s.CreateContext(context.Background(), repo) |
Contributor
Author
There was a problem hiding this comment.
@anmoel Because it's shorter than duplicate code with CreateContext like this:
func (s *RepositoryService[R]) Create(ctx context.Context, repo R) error {
data, err := tools.JsonMarshalInterfaceToIOReader(repo)
if err != nil {
return err
}
body, resp, err := s.client.Post(s.endpoint, data)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("could not create repository %v: HTTP: %d, %s", repo, resp.StatusCode, string(body))
}
return nil
}
Member
There was a problem hiding this comment.
i mean, why you want to manage the context? in our old code, we don't manage the context. we used the http client functions without context.
Contributor
Author
There was a problem hiding this comment.
To support request cancellation, timeout and so on.
An example use-case is terraform-provider-nexus: All Resource.Create|Delete|Exists|Read|Update methods are deprecated in favor of XContext counterparts
// Deprecated: Use CreateContext or CreateWithoutTimeout instead. This
// implementation does not support request cancellation initiated by
// Terraform, such as a system or practitioner sending SIGINT (Ctrl-c).
// This implementation also does not support warning diagnostics.
Create [CreateFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema@v2.36.1#CreateFunc)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Merge after #163