/
requests.go
100 lines (83 loc) · 2.16 KB
/
requests.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
package chatgptuno
import (
"bytes"
"io"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
)
const UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
type Requests struct {
jar http.CookieJar
headers http.Header
timeout int
proxy string
allowRediret bool
response *http.Response
reqReader io.Reader
cookies map[string]string
}
func NewRequests(jar http.CookieJar) *Requests {
r := &Requests{
jar: jar,
headers: http.Header{},
timeout: 60,
allowRediret: true,
reqReader: bytes.NewReader([]byte(``)),
cookies: make(map[string]string, 0),
}
return r
}
func (r *Requests) SetHeaders(headers http.Header) {
r.headers = headers
}
func (r *Requests) SetCookie(name, value string) {
r.cookies[name] = value
}
func (r *Requests) SetTimeout(timeout int) {
if timeout > 0 {
r.timeout = timeout
}
}
func (r *Requests) SetProxy(proxy string) {
r.proxy = proxy
}
func (r *Requests) SetNoRedirects() {
r.allowRediret = false
}
func (r *Requests) Do(method, baseUrl string) (*http.Response, error) {
options := []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(r.timeout),
tls_client.WithClientProfile(tls_client.Chrome_110),
tls_client.WithCookieJar(r.jar),
}
if r.allowRediret {
options = append(options, tls_client.WithNotFollowRedirects())
}
client, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, baseUrl, r.reqReader)
if err != nil {
return nil, err
}
req.Header = r.headers
if r.proxy != "" {
client.SetProxy(r.proxy)
}
resp, err := client.Do(req)
r.response = resp
return resp, err
}
func (r *Requests) SetBody(reader io.Reader) {
r.reqReader = reader
}
func (r *Requests) Get(baseUrl string) (*http.Response, error) {
return r.Do(http.MethodGet, baseUrl)
}
func (r *Requests) Post(baseUrl string) (*http.Response, error) {
return r.Do(http.MethodPost, baseUrl)
}
func (r *Requests) Patch(baseUrl string) (*http.Response, error) {
return r.Do(http.MethodPatch, baseUrl)
}