-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.go
118 lines (106 loc) · 2.86 KB
/
fetch.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
package sdk
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type Fetch struct {
Cookie []*http.Cookie
client *http.Client
}
func (f *Fetch) InitUnSafe() {
f.client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
Timeout: time.Second * 15,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
func (f *Fetch) Init() {
f.client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
Timeout: time.Second * 15,
}
}
func (f *Fetch) SkipTlsCheck() {
f.client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
func (f *Fetch) Get(url string) ([]byte, error) {
response, err := f.GetRaw(url)
if err != nil {
return nil, err
}
s, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return s, nil
}
func (f *Fetch) GetRaw(url string) (*http.Response, error) {
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
for _, v := range f.Cookie {
request.AddCookie(v)
}
response, err := f.client.Do(request)
if err != nil {
return nil, err
}
f.Cookie = cookieMerge(f.Cookie, response.Cookies())
return response, err
}
func (f *Fetch) PostFormRaw(url string, requestData url.Values) (*http.Response, error) {
request, _ := http.NewRequest("POST", url, strings.NewReader(requestData.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for _, v := range f.Cookie {
request.AddCookie(v)
}
return f.client.Do(request)
}
func (f *Fetch) PostForm(url string, requestData url.Values) ([]byte, error) {
response, err := f.PostFormRaw(url, requestData)
if err != nil {
return nil, err
}
f.Cookie = cookieMerge(f.Cookie, response.Cookies())
return ioutil.ReadAll(response.Body)
}
func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]string) (*http.Response, error) {
bytesData, _ := json.Marshal(requestData)
request, _ := http.NewRequest("POST", url, bytes.NewReader(bytesData))
request.Header.Set("Content-Type", "application/json")
for _, v := range f.Cookie {
request.AddCookie(v)
}
return f.client.Do(request)
}
func (f *Fetch) PostJsonForm(url string, requestData map[string]string) ([]byte, error) {
response, err := f.PostJsonFormRaw(url, requestData)
if err != nil {
return nil, err
}
f.Cookie = cookieMerge(f.Cookie, response.Cookies())
return ioutil.ReadAll(response.Body)
}
func cookieMerge(cookieA []*http.Cookie, cookieB []*http.Cookie) []*http.Cookie {
for _, v := range cookieB {
for k, v2 := range cookieA {
if v.Name == v2.Name {
cookieA = append(cookieA[:k], cookieA[k+1:]...)
break
}
}
}
cookieA = append(cookieA, cookieB...)
return cookieA
}