-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
186 lines (147 loc) · 3.93 KB
/
client.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package claude
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/tidwall/gjson"
)
const (
BASE_URI = "https://claude.ai"
)
// MixMap is a type alias for map[string]interface{}
type MixMap = map[string]interface{}
// Client is a ChatGPT request client
type Client struct {
opts Options // custom options
httpCli *http.Client
}
// NewClient will return a ChatGPT request client
func NewClient(options ...Option) *Client {
cli := &Client{
opts: Options{
BaseUri: BASE_URI,
UserAgent: "xxx",
Timeout: 120 * time.Second, // set default timeout
Model: "claude-2", // set default chat model
Debug: false,
},
}
// load custom options
for _, option := range options {
option(cli)
}
cli.initHttpClient()
return cli
}
func (c *Client) initHttpClient() {
transport := &http.Transport{}
if c.opts.Proxy != "" {
proxy, err := url.Parse(c.opts.Proxy)
if err == nil {
transport.Proxy = http.ProxyURL(proxy)
}
}
c.httpCli = &http.Client{
Timeout: c.opts.Timeout,
Transport: transport,
}
}
// Get will request api with Get method
func (c *Client) Get(uri string) (*gjson.Result, error) {
if !strings.HasPrefix(uri, "http") {
uri = c.opts.BaseUri + uri
}
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("new request failed: %v", err)
}
resp, err := c.doRequest(req)
if err != nil {
return nil, fmt.Errorf("request failed: %v", err)
}
return c.parseBody(resp)
}
// Post will request api with Post method
func (c *Client) Post(uri string, params MixMap) (*gjson.Result, error) {
resp, err := c.post(uri, params)
if err != nil {
return nil, fmt.Errorf("request failed: %v", err)
}
return c.parseBody(resp)
}
func (c *Client) post(uri string, params MixMap) (*http.Response, error) {
data, err := json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("marshal request body failed: %v", err)
}
if !strings.HasPrefix(uri, "http") {
uri = c.opts.BaseUri + uri
}
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("new request failed: %v", err)
}
req.Header.Set("Content-Type", "application/json")
return c.doRequest(req)
}
// Delete will request api with delete method
func (c *Client) Delete(uri string, params MixMap) (*gjson.Result, error) {
resp, err := c.delete(uri, params)
if err != nil {
return nil, fmt.Errorf("request failed: %v", err)
}
return c.parseBody(resp)
}
func (c *Client) delete(uri string, params MixMap) (*http.Response, error) {
data, err := json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("marshal request body failed: %v", err)
}
if !strings.HasPrefix(uri, "http") {
uri = c.opts.BaseUri + uri
}
req, err := http.NewRequest(http.MethodDelete, uri, bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("new request failed: %v", err)
}
req.Header.Set("Content-Type", "application/json")
return c.doRequest(req)
}
func (c *Client) doRequest(req *http.Request) (*http.Response, error) {
req.Header.Set("Cookie", fmt.Sprintf("sessionKey=%s", c.opts.SessionKey))
req.Header.Set("User-Agent", c.opts.UserAgent)
if c.opts.Debug {
reqInfo, _ := httputil.DumpRequest(req, true)
log.Printf("http request info: \n%s\n", reqInfo)
}
resp, err := c.httpCli.Do(req)
if c.opts.Debug {
respInfo, _ := httputil.DumpResponse(resp, false)
log.Printf("http response info: \n%s\n", respInfo)
}
return resp, err
}
func (c *Client) parseBody(resp *http.Response) (*gjson.Result, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
res := gjson.ParseBytes(body)
return &res, nil
}
// GetOrgid will get orgid set in option
func (c *Client) GetOrgid() string {
return c.opts.Orgid
}
// GetModel will get model set in option
func (c *Client) GetModel() string {
return c.opts.Model
}