Skip to content

Commit 58d99eb

Browse files
authored
refactor: use http.NewRequestWithContext instead of http.NewRequest (#97)
1 parent de9a632 commit 58d99eb

File tree

11 files changed

+17
-32
lines changed

11 files changed

+17
-32
lines changed

answers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ func (c *Client) Answers(ctx context.Context, request AnswerRequest) (response A
4040
return
4141
}
4242

43-
req, err := http.NewRequest("POST", c.fullURL("/answers"), bytes.NewBuffer(reqBytes))
43+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/answers"), bytes.NewBuffer(reqBytes))
4444
if err != nil {
4545
return
4646
}
4747

48-
req = req.WithContext(ctx)
4948
err = c.sendRequest(req, &response)
5049
return
5150
}

chat.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ func (c *Client) CreateChatCompletion(
6767
}
6868

6969
urlSuffix := "/chat/completions"
70-
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
70+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
7171
if err != nil {
7272
return
7373
}
7474

75-
req = req.WithContext(ctx)
7675
err = c.sendRequest(req, &response)
7776
return
7877
}

completion.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ func (c *Client) CreateCompletion(
9999
}
100100

101101
urlSuffix := "/completions"
102-
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
102+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
103103
if err != nil {
104104
return
105105
}
106106

107-
req = req.WithContext(ctx)
108107
err = c.sendRequest(req, &response)
109108
return
110109
}

edits.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ func (c *Client) Edits(ctx context.Context, request EditsRequest) (response Edit
3939
return
4040
}
4141

42-
req, err := http.NewRequest("POST", c.fullURL("/edits"), bytes.NewBuffer(reqBytes))
42+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/edits"), bytes.NewBuffer(reqBytes))
4343
if err != nil {
4444
return
4545
}
4646

47-
req = req.WithContext(ctx)
4847
err = c.sendRequest(req, &response)
4948
return
5049
}

embeddings.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,11 @@ func (c *Client) CreateEmbeddings(ctx context.Context, request EmbeddingRequest)
141141
}
142142

143143
urlSuffix := "/embeddings"
144-
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
144+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
145145
if err != nil {
146146
return
147147
}
148148

149-
req = req.WithContext(ctx)
150149
err = c.sendRequest(req, &resp)
151150

152151
return

engines.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ type EnginesList struct {
2222
// ListEngines Lists the currently available engines, and provides basic
2323
// information about each option such as the owner and availability.
2424
func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) {
25-
req, err := http.NewRequest("GET", c.fullURL("/engines"), nil)
25+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/engines"), nil)
2626
if err != nil {
2727
return
2828
}
2929

30-
req = req.WithContext(ctx)
3130
err = c.sendRequest(req, &engines)
3231
return
3332
}
@@ -39,12 +38,11 @@ func (c *Client) GetEngine(
3938
engineID string,
4039
) (engine Engine, err error) {
4140
urlSuffix := fmt.Sprintf("/engines/%s", engineID)
42-
req, err := http.NewRequest("GET", c.fullURL(urlSuffix), nil)
41+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
4342
if err != nil {
4443
return
4544
}
4645

47-
req = req.WithContext(ctx)
4846
err = c.sendRequest(req, &engine)
4947
return
5048
}

files.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File
9898

9999
w.Close()
100100

101-
req, err := http.NewRequest("POST", c.fullURL("/files"), &b)
101+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/files"), &b)
102102
if err != nil {
103103
return
104104
}
105105

106-
req = req.WithContext(ctx)
107106
req.Header.Set("Content-Type", w.FormDataContentType())
108107

109108
err = c.sendRequest(req, &file)
@@ -113,25 +112,23 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File
113112

114113
// DeleteFile deletes an existing file.
115114
func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
116-
req, err := http.NewRequest("DELETE", c.fullURL("/files/"+fileID), nil)
115+
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.fullURL("/files/"+fileID), nil)
117116
if err != nil {
118117
return
119118
}
120119

121-
req = req.WithContext(ctx)
122120
err = c.sendRequest(req, nil)
123121
return
124122
}
125123

126124
// ListFiles Lists the currently available files,
127125
// and provides basic information about each file such as the file name and purpose.
128126
func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
129-
req, err := http.NewRequest("GET", c.fullURL("/files"), nil)
127+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/files"), nil)
130128
if err != nil {
131129
return
132130
}
133131

134-
req = req.WithContext(ctx)
135132
err = c.sendRequest(req, &files)
136133
return
137134
}
@@ -140,12 +137,11 @@ func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
140137
// such as the file name and purpose.
141138
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
142139
urlSuffix := fmt.Sprintf("/files/%s", fileID)
143-
req, err := http.NewRequest("GET", c.fullURL(urlSuffix), nil)
140+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
144141
if err != nil {
145142
return
146143
}
147144

148-
req = req.WithContext(ctx)
149145
err = c.sendRequest(req, &file)
150146
return
151147
}

image.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (respons
5353
}
5454

5555
urlSuffix := "/images/generations"
56-
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
56+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
5757
if err != nil {
5858
return
5959
}
6060

61-
req = req.WithContext(ctx)
6261
err = c.sendRequest(req, &response)
6362
return
6463
}
@@ -111,12 +110,11 @@ func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest)
111110
}
112111
writer.Close()
113112
urlSuffix := "/images/edits"
114-
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), body)
113+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), body)
115114
if err != nil {
116115
return
117116
}
118117

119-
req = req.WithContext(ctx)
120118
req.Header.Set("Content-Type", writer.FormDataContentType())
121119
err = c.sendRequest(req, &response)
122120
return

models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ type ModelsList struct {
4040
// ListModels Lists the currently available models,
4141
// and provides basic information about each model such as the model id and parent.
4242
func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error) {
43-
req, err := http.NewRequest("GET", c.fullURL("/models"), nil)
43+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/models"), nil)
4444
if err != nil {
4545
return
4646
}
47-
req = req.WithContext(ctx)
47+
4848
err = c.sendRequest(req, &models)
4949
return
5050
}

moderation.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (re
5858
return
5959
}
6060

61-
req, err := http.NewRequest("POST", c.fullURL("/moderations"), bytes.NewBuffer(reqBytes))
61+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/moderations"), bytes.NewBuffer(reqBytes))
6262
if err != nil {
6363
return
6464
}
6565

66-
req = req.WithContext(ctx)
6766
err = c.sendRequest(req, &response)
6867
return
6968
}

stream.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (c *Client) CreateCompletionStream(
7979
}
8080

8181
urlSuffix := "/completions"
82-
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
82+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
8383
req.Header.Set("Content-Type", "application/json")
8484
req.Header.Set("Accept", "text/event-stream")
8585
req.Header.Set("Cache-Control", "no-cache")
@@ -89,7 +89,6 @@ func (c *Client) CreateCompletionStream(
8989
return
9090
}
9191

92-
req = req.WithContext(ctx)
9392
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
9493
if err != nil {
9594
return

0 commit comments

Comments
 (0)