feat: get header from sendRequestRaw (#694)

* feat: get header from sendRequestRaw

* Fix ci lint
This commit is contained in:
Qiying Wang
2024-04-06 03:15:54 +08:00
committed by GitHub
parent 0925563e86
commit 2646bce71c
3 changed files with 16 additions and 12 deletions

View File

@@ -38,6 +38,12 @@ func (h *httpHeader) GetRateLimitHeaders() RateLimitHeaders {
return newRateLimitHeaders(h.Header())
}
type RawResponse struct {
io.ReadCloser
httpHeader
}
// NewClient creates new OpenAI API client.
func NewClient(authToken string) *Client {
config := DefaultConfig(authToken)
@@ -134,8 +140,8 @@ func (c *Client) sendRequest(req *http.Request, v Response) error {
return decodeResponse(res.Body, v)
}
func (c *Client) sendRequestRaw(req *http.Request) (body io.ReadCloser, err error) {
resp, err := c.config.HTTPClient.Do(req)
func (c *Client) sendRequestRaw(req *http.Request) (response RawResponse, err error) {
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body should be closed by outer function
if err != nil {
return
}
@@ -144,7 +150,10 @@ func (c *Client) sendRequestRaw(req *http.Request) (body io.ReadCloser, err erro
err = c.handleErrorResp(resp)
return
}
return resp.Body, nil
response.SetHeader(resp.Header)
response.ReadCloser = resp.Body
return
}
func sendRequestStream[T streamable](client *Client, req *http.Request) (*streamReader[T], error) {

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
)
@@ -159,13 +158,12 @@ func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err err
return
}
func (c *Client) GetFileContent(ctx context.Context, fileID string) (content io.ReadCloser, err error) {
func (c *Client) GetFileContent(ctx context.Context, fileID string) (content RawResponse, err error) {
urlSuffix := fmt.Sprintf("/files/%s/content", fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
if err != nil {
return
}
content, err = c.sendRequestRaw(req)
return
return c.sendRequestRaw(req)
}

View File

@@ -3,7 +3,6 @@ package openai
import (
"context"
"errors"
"io"
"net/http"
)
@@ -67,7 +66,7 @@ func isValidVoice(voice SpeechVoice) bool {
return contains([]SpeechVoice{VoiceAlloy, VoiceEcho, VoiceFable, VoiceOnyx, VoiceNova, VoiceShimmer}, voice)
}
func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response io.ReadCloser, err error) {
func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response RawResponse, err error) {
if !isValidSpeechModel(request.Model) {
err = ErrInvalidSpeechModel
return
@@ -84,7 +83,5 @@ func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest)
return
}
response, err = c.sendRequestRaw(req)
return
return c.sendRequestRaw(req)
}