* Support Retrieve file content API (#347) * add timeout test for GetFileContent (#347)
This commit is contained in:
committed by
GitHub
parent
06b16a7281
commit
a243e7331f
43
client.go
43
client.go
@@ -47,13 +47,6 @@ func NewOrgClient(authToken, org string) *Client {
|
||||
|
||||
func (c *Client) sendRequest(req *http.Request, v any) error {
|
||||
req.Header.Set("Accept", "application/json; charset=utf-8")
|
||||
// Azure API Key authentication
|
||||
if c.config.APIType == APITypeAzure {
|
||||
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
|
||||
} else {
|
||||
// OpenAI or Azure AD authentication
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
|
||||
}
|
||||
|
||||
// Check whether Content-Type is already set, Upload Files API requires
|
||||
// Content-Type == multipart/form-data
|
||||
@@ -62,9 +55,7 @@ func (c *Client) sendRequest(req *http.Request, v any) error {
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
}
|
||||
|
||||
if len(c.config.OrgID) > 0 {
|
||||
req.Header.Set("OpenAI-Organization", c.config.OrgID)
|
||||
}
|
||||
c.setCommonHeaders(req)
|
||||
|
||||
res, err := c.config.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -73,13 +64,31 @@ func (c *Client) sendRequest(req *http.Request, v any) error {
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
|
||||
if isFailureStatusCode(res) {
|
||||
return c.handleErrorResp(res)
|
||||
}
|
||||
|
||||
return decodeResponse(res.Body, v)
|
||||
}
|
||||
|
||||
func (c *Client) setCommonHeaders(req *http.Request) {
|
||||
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication
|
||||
// Azure API Key authentication
|
||||
if c.config.APIType == APITypeAzure {
|
||||
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
|
||||
} else {
|
||||
// OpenAI or Azure AD authentication
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
|
||||
}
|
||||
if c.config.OrgID != "" {
|
||||
req.Header.Set("OpenAI-Organization", c.config.OrgID)
|
||||
}
|
||||
}
|
||||
|
||||
func isFailureStatusCode(resp *http.Response) bool {
|
||||
return resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest
|
||||
}
|
||||
|
||||
func decodeResponse(body io.Reader, v any) error {
|
||||
if v == nil {
|
||||
return nil
|
||||
@@ -145,17 +154,7 @@ func (c *Client) newStreamRequest(
|
||||
req.Header.Set("Cache-Control", "no-cache")
|
||||
req.Header.Set("Connection", "keep-alive")
|
||||
|
||||
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication
|
||||
// Azure API Key authentication
|
||||
if c.config.APIType == APITypeAzure {
|
||||
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
|
||||
} else {
|
||||
// OpenAI or Azure AD authentication
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
|
||||
}
|
||||
if c.config.OrgID != "" {
|
||||
req.Header.Set("OpenAI-Organization", c.config.OrgID)
|
||||
}
|
||||
c.setCommonHeaders(req)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user