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:
20
client.go
20
client.go
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user