Skip to content

Commit 4c929dd

Browse files
committed
Use NewRequestWithContext
1 parent 96fe4ae commit 4c929dd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

client/nginx.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -10,6 +11,7 @@ import (
1011
"net/http"
1112
"reflect"
1213
"strings"
14+
"time"
1315
)
1416

1517
const (
@@ -752,9 +754,12 @@ func (client *NginxClient) post(path string, input interface{}) error {
752754
}
753755

754756
func (client *NginxClient) delete(path string, expectedStatusCode int) error {
757+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
758+
defer cancel()
759+
755760
path = fmt.Sprintf("%v/%v/%v/", client.apiEndpoint, client.version, path)
756761

757-
req, err := http.NewRequest(http.MethodDelete, path, nil)
762+
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, path, nil)
758763
if err != nil {
759764
return fmt.Errorf("failed to create a delete request: %w", err)
760765
}
@@ -774,14 +779,17 @@ func (client *NginxClient) delete(path string, expectedStatusCode int) error {
774779
}
775780

776781
func (client *NginxClient) patch(path string, input interface{}, expectedStatusCode int) error {
782+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
783+
defer cancel()
784+
777785
path = fmt.Sprintf("%v/%v/%v/", client.apiEndpoint, client.version, path)
778786

779787
jsonInput, err := json.Marshal(input)
780788
if err != nil {
781789
return fmt.Errorf("failed to marshall input: %w", err)
782790
}
783791

784-
req, err := http.NewRequest(http.MethodPatch, path, bytes.NewBuffer(jsonInput))
792+
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, path, bytes.NewBuffer(jsonInput))
785793
if err != nil {
786794
return fmt.Errorf("failed to create a patch request: %w", err)
787795
}

0 commit comments

Comments
 (0)