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

View File

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

View File

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