Skip to content

Commit 2646bce

Browse files
authored
feat: get header from sendRequestRaw (#694)
* feat: get header from sendRequestRaw * Fix ci lint
1 parent 0925563 commit 2646bce

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

client.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func (h *httpHeader) GetRateLimitHeaders() RateLimitHeaders {
3838
return newRateLimitHeaders(h.Header())
3939
}
4040

41+
type RawResponse struct {
42+
io.ReadCloser
43+
44+
httpHeader
45+
}
46+
4147
// NewClient creates new OpenAI API client.
4248
func NewClient(authToken string) *Client {
4349
config := DefaultConfig(authToken)
@@ -134,8 +140,8 @@ func (c *Client) sendRequest(req *http.Request, v Response) error {
134140
return decodeResponse(res.Body, v)
135141
}
136142

137-
func (c *Client) sendRequestRaw(req *http.Request) (body io.ReadCloser, err error) {
138-
resp, err := c.config.HTTPClient.Do(req)
143+
func (c *Client) sendRequestRaw(req *http.Request) (response RawResponse, err error) {
144+
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body should be closed by outer function
139145
if err != nil {
140146
return
141147
}
@@ -144,7 +150,10 @@ func (c *Client) sendRequestRaw(req *http.Request) (body io.ReadCloser, err erro
144150
err = c.handleErrorResp(resp)
145151
return
146152
}
147-
return resp.Body, nil
153+
154+
response.SetHeader(resp.Header)
155+
response.ReadCloser = resp.Body
156+
return
148157
}
149158

150159
func sendRequestStream[T streamable](client *Client, req *http.Request) (*streamReader[T], error) {

files.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io"
87
"net/http"
98
"os"
109
)
@@ -159,13 +158,12 @@ func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err err
159158
return
160159
}
161160

162-
func (c *Client) GetFileContent(ctx context.Context, fileID string) (content io.ReadCloser, err error) {
161+
func (c *Client) GetFileContent(ctx context.Context, fileID string) (content RawResponse, err error) {
163162
urlSuffix := fmt.Sprintf("/files/%s/content", fileID)
164163
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
165164
if err != nil {
166165
return
167166
}
168167

169-
content, err = c.sendRequestRaw(req)
170-
return
168+
return c.sendRequestRaw(req)
171169
}

speech.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package openai
33
import (
44
"context"
55
"errors"
6-
"io"
76
"net/http"
87
)
98

@@ -67,7 +66,7 @@ func isValidVoice(voice SpeechVoice) bool {
6766
return contains([]SpeechVoice{VoiceAlloy, VoiceEcho, VoiceFable, VoiceOnyx, VoiceNova, VoiceShimmer}, voice)
6867
}
6968

70-
func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response io.ReadCloser, err error) {
69+
func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response RawResponse, err error) {
7170
if !isValidSpeechModel(request.Model) {
7271
err = ErrInvalidSpeechModel
7372
return
@@ -84,7 +83,5 @@ func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest)
8483
return
8584
}
8685

87-
response, err = c.sendRequestRaw(req)
88-
89-
return
86+
return c.sendRequestRaw(req)
9087
}

0 commit comments

Comments
 (0)