refactor: use http.NewRequestWithContext instead of http.NewRequest (#97)
This commit is contained in:
@@ -40,12 +40,11 @@ func (c *Client) Answers(ctx context.Context, request AnswerRequest) (response A
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
3
chat.go
3
chat.go
@@ -67,12 +67,11 @@ func (c *Client) CreateChatCompletion(
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := "/chat/completions"
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,12 +99,11 @@ func (c *Client) CreateCompletion(
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := "/completions"
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
3
edits.go
3
edits.go
@@ -39,12 +39,11 @@ func (c *Client) Edits(ctx context.Context, request EditsRequest) (response Edit
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,12 +141,11 @@ func (c *Client) CreateEmbeddings(ctx context.Context, request EmbeddingRequest)
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := "/embeddings"
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &resp)
|
err = c.sendRequest(req, &resp)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -22,12 +22,11 @@ type EnginesList struct {
|
|||||||
// ListEngines Lists the currently available engines, and provides basic
|
// ListEngines Lists the currently available engines, and provides basic
|
||||||
// information about each option such as the owner and availability.
|
// information about each option such as the owner and availability.
|
||||||
func (c *Client) ListEngines(ctx context.Context) (engines EnginesList, err error) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &engines)
|
err = c.sendRequest(req, &engines)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -39,12 +38,11 @@ func (c *Client) GetEngine(
|
|||||||
engineID string,
|
engineID string,
|
||||||
) (engine Engine, err error) {
|
) (engine Engine, err error) {
|
||||||
urlSuffix := fmt.Sprintf("/engines/%s", engineID)
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &engine)
|
err = c.sendRequest(req, &engine)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
12
files.go
12
files.go
@@ -98,12 +98,11 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File
|
|||||||
|
|
||||||
w.Close()
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||||
|
|
||||||
err = c.sendRequest(req, &file)
|
err = c.sendRequest(req, &file)
|
||||||
@@ -113,12 +112,11 @@ func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File
|
|||||||
|
|
||||||
// DeleteFile deletes an existing file.
|
// DeleteFile deletes an existing file.
|
||||||
func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, nil)
|
err = c.sendRequest(req, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -126,12 +124,11 @@ func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
|
|||||||
// ListFiles Lists the currently available files,
|
// ListFiles Lists the currently available files,
|
||||||
// and provides basic information about each file such as the file name and purpose.
|
// 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) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &files)
|
err = c.sendRequest(req, &files)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -140,12 +137,11 @@ func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
|
|||||||
// such as the file name and purpose.
|
// such as the file name and purpose.
|
||||||
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
|
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
|
||||||
urlSuffix := fmt.Sprintf("/files/%s", fileID)
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &file)
|
err = c.sendRequest(req, &file)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
6
image.go
6
image.go
@@ -53,12 +53,11 @@ func (c *Client) CreateImage(ctx context.Context, request ImageRequest) (respons
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := "/images/generations"
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -111,12 +110,11 @@ func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest)
|
|||||||
}
|
}
|
||||||
writer.Close()
|
writer.Close()
|
||||||
urlSuffix := "/images/edits"
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -40,11 +40,11 @@ type ModelsList struct {
|
|||||||
// ListModels Lists the currently available models,
|
// ListModels Lists the currently available models,
|
||||||
// and provides basic information about each model such as the model id and parent.
|
// 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) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &models)
|
err = c.sendRequest(req, &models)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,12 +58,11 @@ func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (re
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
err = c.sendRequest(req, &response)
|
err = c.sendRequest(req, &response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ func (c *Client) CreateCompletionStream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlSuffix := "/completions"
|
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("Content-Type", "application/json")
|
||||||
req.Header.Set("Accept", "text/event-stream")
|
req.Header.Set("Accept", "text/event-stream")
|
||||||
req.Header.Set("Cache-Control", "no-cache")
|
req.Header.Set("Cache-Control", "no-cache")
|
||||||
@@ -89,7 +89,6 @@ func (c *Client) CreateCompletionStream(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
|
||||||
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
|
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user