Skip to content

Commit

Permalink
refactor: use http.NewRequestWithContext instead of http.NewRequest (#97
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bestgopher authored Mar 2, 2023
1 parent de9a632 commit 58d99eb
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 32 deletions.
3 changes: 1 addition & 2 deletions answers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ func (c *Client) Answers(ctx context.Context, request AnswerRequest) (response A
return
}

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

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
3 changes: 1 addition & 2 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ func (c *Client) CreateChatCompletion(
}

urlSuffix := "/chat/completions"
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
3 changes: 1 addition & 2 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ func (c *Client) CreateCompletion(
}

urlSuffix := "/completions"
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
3 changes: 1 addition & 2 deletions edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ func (c *Client) Edits(ctx context.Context, request EditsRequest) (response Edit
return
}

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

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
3 changes: 1 addition & 2 deletions embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,11 @@ func (c *Client) CreateEmbeddings(ctx context.Context, request EmbeddingRequest)
}

urlSuffix := "/embeddings"
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &resp)

return
Expand Down
6 changes: 2 additions & 4 deletions engines.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ type EnginesList struct {
// ListEngines Lists the currently available engines, and provides basic
// information about each option such as the owner and availability.
func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) {
req, err := http.NewRequest("GET", c.fullURL("/engines"), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/engines"), nil)
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &engines)
return
}
Expand All @@ -39,12 +38,11 @@ func (c *Client) GetEngine(
engineID string,
) (engine Engine, err error) {
urlSuffix := fmt.Sprintf("/engines/%s", engineID)
req, err := http.NewRequest("GET", c.fullURL(urlSuffix), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &engine)
return
}
12 changes: 4 additions & 8 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File

w.Close()

req, err := http.NewRequest("POST", c.fullURL("/files"), &b)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/files"), &b)
if err != nil {
return
}

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

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

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

req = req.WithContext(ctx)
err = c.sendRequest(req, nil)
return
}

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

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

req = req.WithContext(ctx)
err = c.sendRequest(req, &file)
return
}
6 changes: 2 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (respons
}

urlSuffix := "/images/generations"
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
Expand Down Expand Up @@ -111,12 +110,11 @@ func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest)
}
writer.Close()
urlSuffix := "/images/edits"
req, err := http.NewRequest(http.MethodPost, c.fullURL(urlSuffix), body)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), body)
if err != nil {
return
}

req = req.WithContext(ctx)
req.Header.Set("Content-Type", writer.FormDataContentType())
err = c.sendRequest(req, &response)
return
Expand Down
4 changes: 2 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type ModelsList struct {
// ListModels Lists the currently available models,
// and provides basic information about each model such as the model id and parent.
func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error) {
req, err := http.NewRequest("GET", c.fullURL("/models"), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/models"), nil)
if err != nil {
return
}
req = req.WithContext(ctx)

err = c.sendRequest(req, &models)
return
}
3 changes: 1 addition & 2 deletions moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (re
return
}

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

req = req.WithContext(ctx)
err = c.sendRequest(req, &response)
return
}
3 changes: 1 addition & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Client) CreateCompletionStream(
}

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

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

0 comments on commit 58d99eb

Please sign in to comment.