forked from GeniusAI-Platform/openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
30 lines (26 loc) · 762 Bytes
/
common.go
File metadata and controls
30 lines (26 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package openai
import (
"github.com/GoFarsi/openai/client"
"github.com/GoFarsi/openai/entity"
"github.com/GoFarsi/openai/errors"
"net/http"
"reflect"
)
func responseHandler[T any](resp *client.Response) (response T, err error) {
errResp := new(entity.ErrorResponse)
m, ok := reflect.New(reflect.TypeOf(response).Elem()).Interface().(T)
if !ok {
return response, errors.New(http.StatusInternalServerError, "", "response type is invalid", "", "")
}
if resp.GetHttpResponse().StatusCode != http.StatusOK {
if err = resp.GetJSON(errResp); err != nil {
return response, err
}
errResp.HttpCode = resp.GetHttpResponse().StatusCode
return response, errResp
}
if err = resp.GetJSON(m); err != nil {
return response, err
}
return m, nil
}