-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrequest.go
220 lines (185 loc) · 4.66 KB
/
request.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package httpc
import (
"compress/gzip"
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/Albert-Zhan/httpc/body"
"io"
"net/http"
"net/url"
"os"
"strings"
)
type Request struct {
httpc *HttpClient
request *http.Request
response *http.Response
method string
url string
param *url.Values
header map[string]string
cookies *[]*http.Cookie
data body.Body
debug bool
err error
}
func NewRequest(client *HttpClient) *Request {
return &Request{
httpc: client,
method: "GET",
param: &url.Values{},
header: make(map[string]string),
cookies: new([]*http.Cookie),
debug: false,
err: nil,
}
}
func (this *Request) SetClient(client *HttpClient) *Request {
this.httpc = client
return this
}
func (this *Request) SetMethod(name string) *Request {
this.method = strings.ToUpper(name)
return this
}
func (this *Request) SetUrl(url string) *Request {
this.url = url
return this
}
func (this *Request) SetParam(name, value string) *Request {
this.param.Add(name, value)
return this
}
func (this *Request) SetHeader(name, value string) *Request {
this.header[name] = value
return this
}
func (this *Request) SetBasicAuth(username, password string) *Request {
auth := username + ":" + password
value := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
this.header["Authorization"] = value
return this
}
func (this *Request) SetCookies(cookies *[]*http.Cookie) *Request {
this.cookies = cookies
return this
}
func (this *Request) SetDebug(d bool) *Request {
this.debug = d
return this
}
func (this *Request) SetBody(body body.Body) *Request {
this.data = body
return this
}
func (this *Request) Send(ctxs ...context.Context) *Request {
param := this.param.Encode()
if param != "" {
this.url += "?" + param
}
var data io.Reader
contentType := ""
if this.data != nil {
data = this.data.Encode()
contentType = this.data.GetContentType()
}
ctx := context.Background()
if len(ctxs) > 0 {
ctx = ctxs[0]
}
this.request, this.err = http.NewRequestWithContext(ctx, this.method, this.url, data)
defer this.log()
if this.err != nil {
return this
}
if contentType != "" {
this.request.Header.Set("Content-Type", contentType)
}
for k, v := range this.header {
this.request.Header.Set(k, v)
}
for _, v := range *this.cookies {
this.request.AddCookie(v)
}
this.response, this.err = this.httpc.client.Do(this.request)
if this.err != nil {
return this
}
return this
}
func (this *Request) GetResponse() *http.Response {
return this.response
}
func (this *Request) GetError() error {
return this.err
}
func (this *Request) log() {
if this.debug == true {
data := ""
if this.data != nil {
data = this.data.GetData()
}
fmt.Printf("[httpc Debug]\n")
fmt.Printf("-------------------------------------------------------------------\n")
fmt.Printf("Request: %s %s\nHeader: %v\nCookies: %v\n", this.method, this.url, this.request.Header, this.request.Cookies())
fmt.Printf("Body: %s\n", data)
fmt.Printf("-------------------------------------------------------------------\n")
}
}
func (this *Request) End() (*http.Response, string, error) {
if this.err != nil {
return nil, "", errors.New(this.err.Error())
}
var bodyByte []byte
if strings.ToLower(this.response.Header.Get("Content-Encoding")) == "gzip" {
reader, _ := gzip.NewReader(this.response.Body)
bodyByte, _ = io.ReadAll(reader)
_ = reader.Close()
} else {
bodyByte, _ = io.ReadAll(this.response.Body)
}
_ = this.response.Body.Close()
return this.response, string(bodyByte), nil
}
func (this *Request) EndByte() (*http.Response, []byte, error) {
if this.err != nil {
return nil, []byte(""), errors.New(this.err.Error())
}
var bodyByte []byte
if strings.ToLower(this.response.Header.Get("Content-Encoding")) == "gzip" {
reader, _ := gzip.NewReader(this.response.Body)
bodyByte, _ = io.ReadAll(reader)
_ = reader.Close()
} else {
bodyByte, _ = io.ReadAll(this.response.Body)
}
_ = this.response.Body.Close()
return this.response, bodyByte, nil
}
func (this *Request) EndFile(savePath, saveFileName string) (*http.Response, error) {
if this.err != nil {
return nil, errors.New(this.err.Error())
}
if this.response.StatusCode != http.StatusOK {
return nil, errors.New("Not written")
}
if saveFileName == "" {
path := strings.Split(this.request.URL.String(), "/")
if len(path) > 1 {
saveFileName = path[len(path)-1]
}
}
f, err := os.Create(savePath + saveFileName)
if err != nil {
return nil, errors.New(err.Error())
}
defer f.Close()
_, err = io.Copy(f, this.response.Body)
_ = this.response.Body.Close()
if err != nil {
return nil, errors.New(err.Error())
}
return this.response, nil
}