mirror of
https://github.com/fumiama/deepinfra.git
synced 2026-06-09 12:30:31 +08:00
feat: customizable client
This commit is contained in:
11
api.go
11
api.go
@@ -15,19 +15,28 @@ const (
|
|||||||
type API struct {
|
type API struct {
|
||||||
api string // api to call
|
api string // api to call
|
||||||
key string // key in Authorization: Bearer
|
key string // key in Authorization: Bearer
|
||||||
|
cli *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPI(api, key string) API {
|
func NewAPI(api, key string) API {
|
||||||
return API{api: api, key: key}
|
return API{api: api, key: key}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *API) SetHTTPClient(c *http.Client) {
|
||||||
|
api.cli = c
|
||||||
|
}
|
||||||
|
|
||||||
func (api *API) Request(model Model) (string, error) {
|
func (api *API) Request(model Model) (string, error) {
|
||||||
req, err := http.NewRequest("POST", model.API(api.api, api.key), model.Body())
|
req, err := http.NewRequest("POST", model.API(api.api, api.key), model.Body())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
model.Header(api.key, req.Header)
|
model.Header(api.key, req.Header)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
cli := http.DefaultClient
|
||||||
|
if api.cli != nil {
|
||||||
|
cli = api.cli
|
||||||
|
}
|
||||||
|
resp, err := cli.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user