forked from zhouyangtingwen/dify-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
47 lines (42 loc) · 940 Bytes
/
api.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dify
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
)
type API struct {
c *Client
secret string
}
func (api *API) WithSecret(secret string) *API {
api.secret = secret
return api
}
func (api *API) getSecret() string {
if api.secret != "" {
return api.secret
}
return api.c.getAPISecret()
}
func (api *API) createBaseRequest(ctx context.Context, method, apiUrl string, body interface{}) (*http.Request, error) {
var b io.Reader
if body != nil {
reqBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
b = bytes.NewBuffer(reqBytes)
} else {
b = http.NoBody
}
req, err := http.NewRequestWithContext(ctx, method, api.c.getHost()+apiUrl, b)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+api.getSecret())
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
return req, nil
}