-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
141 lines (124 loc) · 3.31 KB
/
export.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
package requests
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
)
var defaultProxyInfo *Proxy
func SetDefaultProxy(ip string, port string, username string, psw string) {
defaultProxyInfo = &Proxy{Ip: ip, Port: port, UserName: username, Password: psw}
}
var defaultCookie map[string]string
func SetDefaultCookie(cookie map[string]string) {
defaultCookie = cookie
}
var defaultTimeout time.Duration
func SetDefaultTimeout(timeout int) {
defaultTimeout = time.Duration(timeout)
}
var defaultHeaders http.Header
func SetDefaultHeaders(headers map[string]interface{}) {
defaultHeaders = make(http.Header)
for k, v := range headers {
defaultHeaders.Set(k, fmt.Sprintf("%v", v))
}
}
func NewClient() *Client {
client := &Client{
jsonData: nil,
Cookie: make(map[string]string),
httpHeaders: make(http.Header),
dataForm: &url.Values{},
httpClient: http.Client{Timeout: time.Second * 30},
}
if defaultTimeout != 0 {
client.httpClient.Timeout = time.Second * defaultTimeout
}
if defaultHeaders != nil {
client.httpHeaders = defaultHeaders
}
if defaultCookie != nil {
client.SetCookie(defaultCookie)
}
if defaultProxyInfo != nil {
SetProxy(client, defaultProxyInfo)
}
return client
}
func Get(pointPath string, params any, headers map[string]interface{}) HttpResultInterface {
response := NewClient().GetMethod().UrlSite(pointPath).FormQuery(params)
if headers != nil {
response.Headers(headers)
}
for i := 0; i < 3; i++ {
result := response.SetRequest().Send()
if !result.Error() {
return result
}
}
return NewResponse(nil)
}
func Post(pointPath string, params any, headers map[string]interface{}) HttpResultInterface {
response := NewClient().PostMethod().UrlSite(pointPath)
if headers["Content-Type"] == ContentTypeForm {
response.FormQuery(params)
} else if headers["Content-Type"] == ContentTypeJson {
response.JsonQuery(params)
}
if headers != nil {
response.Headers(headers)
}
for i := 0; i < 3; i++ {
result := response.SetRequest().Send()
if !result.Error() {
return result
}
}
return NewResponse(nil)
}
func Put(pointPath string, params any, headers map[string]interface{}) HttpResultInterface {
response := NewClient().PutMethod().UrlSite(pointPath)
if headers["Content-Type"] == ContentTypeForm {
response.FormQuery(params)
} else if headers["Content-Type"] == ContentTypeJson {
response.JsonQuery(params)
}
if headers != nil {
response.Headers(headers)
}
for i := 0; i < 3; i++ {
result := response.SetRequest().Send()
if !result.Error() {
return result
}
}
return NewResponse(nil)
}
func Image(imageUrl string, headers map[string]interface{}) ([]byte, error) {
response := NewClient().GetMethod().UrlSite(imageUrl)
if headers != nil {
response.Headers(headers)
}
for i := 0; i < 3; i++ {
result := response.SetRequest().Send()
if !result.Error() {
return result.Bytes(), nil
}
}
return nil, fmt.Errorf("failed to get image: %s", imageUrl)
}
func NewImage(imageUrl, imagePath string, headers map[string]interface{}) error {
if _, ok := os.Stat(imagePath); os.IsNotExist(ok) {
image, err := Image(imageUrl, headers)
if err != nil {
return fmt.Errorf("failed to get image: %w", err)
}
err = os.WriteFile(imagePath, image, 0644)
if err != nil {
return fmt.Errorf("failed to write image file: %w", err)
}
}
return nil
}