Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use http.NewRequestWithContext instead of http.NewRequest #97

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -57,12 +57,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