Skip to content

Commit

Permalink
NewRequest with context
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 29, 2021
1 parent 75deb72 commit 9c71234
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions example/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *RestClient) Auth(key, secret, passphrase string) {
}

// NewRequest create new API request. Relative url can be provided in refURL.
func (c *RestClient) NewRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
func (c *RestClient) NewRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
body, err := castPayload(payload)
if err != nil {
return nil, err
Expand All @@ -70,7 +71,7 @@ func (c *RestClient) NewRequest(method, refURL string, params url.Values, payloa
}

pathURL := c.BaseURL.ResolveReference(rel)
return http.NewRequest(method, pathURL.String(), bytes.NewReader(body))
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
}

// SendRequest sends the request to the API server and handle the response
Expand All @@ -95,7 +96,7 @@ func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error
}

// NewAuthenticatedRequest creates new http request for authenticated routes.
func (c *RestClient) NewAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
if len(c.Key) == 0 {
return nil, errors.New("empty api key")
}
Expand Down Expand Up @@ -124,7 +125,7 @@ func (c *RestClient) NewAuthenticatedRequest(method, refURL string, params url.V
return nil, err
}

req, err := http.NewRequest(method, pathURL.String(), bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions example/api/no_param_request_accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/api/place_order_request_accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c71234

Please sign in to comment.