Feat Add headers to openai responses (#506)

* feat: add headers to http response

* chore: add test

* fix: rename to httpHeader
This commit is contained in:
Simone Vellei
2023-10-09 17:41:54 +02:00
committed by GitHub
parent 533935e4fc
commit 8e165dc9aa
14 changed files with 107 additions and 2 deletions

View File

@@ -20,6 +20,20 @@ type Client struct {
createFormBuilder func(io.Writer) utils.FormBuilder
}
type Response interface {
SetHeader(http.Header)
}
type httpHeader http.Header
func (h *httpHeader) SetHeader(header http.Header) {
*h = httpHeader(header)
}
func (h httpHeader) Header() http.Header {
return http.Header(h)
}
// NewClient creates new OpenAI API client.
func NewClient(authToken string) *Client {
config := DefaultConfig(authToken)
@@ -82,7 +96,7 @@ func (c *Client) newRequest(ctx context.Context, method, url string, setters ...
return req, nil
}
func (c *Client) sendRequest(req *http.Request, v any) error {
func (c *Client) sendRequest(req *http.Request, v Response) error {
req.Header.Set("Accept", "application/json; charset=utf-8")
// Check whether Content-Type is already set, Upload Files API requires
@@ -103,6 +117,10 @@ func (c *Client) sendRequest(req *http.Request, v any) error {
return c.handleErrorResp(res)
}
if v != nil {
v.SetHeader(res.Header)
}
return decodeResponse(res.Body, v)
}