Skip to content

Commit

Permalink
feat: get header from sendRequestRaw (#694)
Browse files Browse the repository at this point in the history
* feat: get header from sendRequestRaw

* Fix ci lint
  • Loading branch information
WqyJh committed Apr 5, 2024
1 parent 0925563 commit 2646bce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
15 changes: 12 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
)
Expand Down Expand Up @@ -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)
}
7 changes: 2 additions & 5 deletions speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package openai
import (
"context"
"errors"
"io"
"net/http"
)

Expand Down Expand Up @@ -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
Expand All @@ -84,7 +83,5 @@ func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest)
return
}

response, err = c.sendRequestRaw(req)

return
return c.sendRequestRaw(req)
}

0 comments on commit 2646bce

Please sign in to comment.