Skip to content

Commit

Permalink
misc: use NewRequestWithContext
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Feb 15, 2022
1 parent dacbdf8 commit d89251b
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions misc.go
Expand Up @@ -66,13 +66,12 @@ func (e *RateLimitedError) Retryable() bool {
}

func fileUploadReq(ctx context.Context, path string, values url.Values, r io.Reader) (*http.Request, error) {
req, err := http.NewRequest("POST", path, r)
req, err := http.NewRequestWithContext(ctx, "POST", path, r)
if err != nil {
return nil, err
}

req = req.WithContext(ctx)
req.URL.RawQuery = (values).Encode()
req.URL.RawQuery = values.Encode()
return req, nil
}

Expand All @@ -81,14 +80,13 @@ func downloadFile(ctx context.Context, client httpClient, token string, download
return fmt.Errorf("received empty download URL")
}

req, err := http.NewRequest("GET", downloadURL, &bytes.Buffer{})
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, &bytes.Buffer{})
if err != nil {
return err
}

var bearer = "Bearer " + token
req.Header.Add("Authorization", bearer)
req.WithContext(ctx)

resp, err := client.Do(req)
if err != nil {
Expand All @@ -107,22 +105,22 @@ func downloadFile(ctx context.Context, client httpClient, token string, download
return err
}

func formReq(endpoint string, values url.Values) (req *http.Request, err error) {
if req, err = http.NewRequest("POST", endpoint, strings.NewReader(values.Encode())); err != nil {
func formReq(ctx context.Context, endpoint string, values url.Values) (req *http.Request, err error) {
if req, err = http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(values.Encode())); err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}

func jsonReq(endpoint string, body interface{}) (req *http.Request, err error) {
func jsonReq(ctx context.Context, endpoint string, body interface{}) (req *http.Request, err error) {
buffer := bytes.NewBuffer([]byte{})
if err = json.NewEncoder(buffer).Encode(body); err != nil {
return nil, err
}

if req, err = http.NewRequest("POST", endpoint, buffer); err != nil {
if req, err = http.NewRequestWithContext(ctx, "POST", endpoint, buffer); err != nil {
return nil, err
}

Expand Down Expand Up @@ -184,7 +182,6 @@ func postWithMultipartResponse(ctx context.Context, client httpClient, path, nam
}
req.Header.Add("Content-Type", wr.FormDataContentType())
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req = req.WithContext(ctx)
resp, err := client.Do(req)

if err != nil {
Expand All @@ -206,7 +203,6 @@ func postWithMultipartResponse(ctx context.Context, client httpClient, path, nam
}

func doPost(ctx context.Context, client httpClient, req *http.Request, parser responseParser, d Debug) error {
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
return err
Expand All @@ -224,7 +220,7 @@ func doPost(ctx context.Context, client httpClient, req *http.Request, parser re
// post JSON.
func postJSON(ctx context.Context, client httpClient, endpoint, token string, json []byte, intf interface{}, d Debug) error {
reqBody := bytes.NewBuffer(json)
req, err := http.NewRequest("POST", endpoint, reqBody)
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, reqBody)
if err != nil {
return err
}
Expand All @@ -237,7 +233,7 @@ func postJSON(ctx context.Context, client httpClient, endpoint, token string, js
// post a url encoded form.
func postForm(ctx context.Context, client httpClient, endpoint string, values url.Values, intf interface{}, d Debug) error {
reqBody := strings.NewReader(values.Encode())
req, err := http.NewRequest("POST", endpoint, reqBody)
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, reqBody)
if err != nil {
return err
}
Expand All @@ -246,7 +242,7 @@ func postForm(ctx context.Context, client httpClient, endpoint string, values ur
}

func getResource(ctx context.Context, client httpClient, endpoint, token string, values url.Values, intf interface{}, d Debug) error {
req, err := http.NewRequest("GET", endpoint, nil)
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit d89251b

Please sign in to comment.