-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPHttp.go
1271 lines (1164 loc) · 33.2 KB
/
XPHttp.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package XPSuperKit
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"fmt"
"log"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"os"
"io"
"reflect"
"strconv"
"strings"
"sort"
"time"
"mime/multipart"
"net/textproto"
"path/filepath"
"golang.org/x/net/publicsuffix"
)
type HTTPRequest *http.Request
type HTTPResponse *http.Response
const (
HTTP_POST = "POST"
HTTP_GET = "GET"
HTTP_HEAD = "HEAD"
HTTP_PUT = "PUT"
HTTP_DELETE = "DELETE"
HTTP_PATCH = "PATCH"
HTTP_OPTIONS = "OPTIONS"
)
type XPHttpImpl struct {
Url string
Method string
Headers map[string]string
TargetType string
ForceType string
Data map[string]interface{}
SliceData []interface{}
FormData url.Values
QueryData url.Values
FileData []HTTP_File
BounceToRawString bool
RawString string
Client *http.Client
Transport *http.Transport
ReqCookies []*http.Cookie
CookieJar *cookiejar.Jar
Errors []error
BasicAuth struct{ Username, Password string }
Debug bool
CurlCommand bool
logger *log.Logger
Retryable struct {
RetryableStatus []int
RetryerTime time.Duration
RetryerCount int
Attempt int
Enable bool
}
}
var HTTP_DisableTransportSwap = false
func NewHttp() *XPHttpImpl {
cookiejarOptions := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, _ := cookiejar.New(&cookiejarOptions)
debug := os.Getenv("XPSUPERKIT_DEBUG") == "1"
h := &XPHttpImpl{
TargetType: "json",
Data: make(map[string]interface{}),
Headers: make(map[string]string),
RawString: "",
SliceData: []interface{}{},
FormData: url.Values{},
QueryData: url.Values{},
FileData: make([]HTTP_File, 0),
BounceToRawString: false,
Client: &http.Client{Jar: jar},
Transport: &http.Transport{},
CookieJar: jar,
ReqCookies: make([]*http.Cookie, 0),
Errors: nil,
BasicAuth: struct{ Username, Password string }{},
Debug: debug,
CurlCommand: false,
logger: log.New(os.Stderr, "[gorequest]", log.LstdFlags),
}
h.Transport.DisableKeepAlives = true
return h
}
func (h *XPHttpImpl) SetDebug(enable bool) *XPHttpImpl {
h.Debug = enable
return h
}
func (h *XPHttpImpl) SetCurlCommand(enable bool) *XPHttpImpl {
h.CurlCommand = enable
return h
}
func (h *XPHttpImpl) SetLogger(logger *log.Logger) *XPHttpImpl {
h.logger = logger
return h
}
func (h *XPHttpImpl) GetCookie(u *url.URL, key string) *http.Cookie {
cookies := h.CookieJar.Cookies(u)
var result *http.Cookie
for _, cookie := range cookies {
if strings.EqualFold(key, cookie.Name) {
result = cookie
break
}
}
return result
}
func (h *XPHttpImpl) Reset() {
h.Url = ""
h.Method = ""
h.Headers = make(map[string]string)
h.Data = make(map[string]interface{})
h.SliceData = []interface{}{}
h.FormData = url.Values{}
h.QueryData = url.Values{}
h.FileData = make([]HTTP_File, 0)
h.BounceToRawString = false
h.RawString = ""
h.ForceType = ""
h.TargetType = "json"
h.ReqCookies = make([]*http.Cookie, 0)
h.Errors = nil
}
func (h *XPHttpImpl) CustomMethod(method, targetUrl string) *XPHttpImpl {
switch method {
case HTTP_POST:
return h.Post(targetUrl)
case HTTP_GET:
return h.Get(targetUrl)
case HTTP_HEAD:
return h.Head(targetUrl)
case HTTP_PUT:
return h.Put(targetUrl)
case HTTP_DELETE:
return h.Delete(targetUrl)
case HTTP_PATCH:
return h.Patch(targetUrl)
case HTTP_OPTIONS:
return h.Options(targetUrl)
default:
h.Reset()
h.Method = method
h.Url = targetUrl
h.Errors = nil
return h
}
}
func (h *XPHttpImpl) Get(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_GET
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Post(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_POST
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Head(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_HEAD
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Put(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_PUT
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Delete(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_DELETE
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Patch(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_PATCH
h.Url = targetUrl
h.Errors = nil
return h
}
func (h *XPHttpImpl) Options(targetUrl string) *XPHttpImpl {
h.Reset()
h.Method = HTTP_OPTIONS
h.Url = targetUrl
h.Errors = nil
return h
}
// 自定义请求头
//
// 例如
// XPSuperKit.NewHttp().
// Header("Accept", "application/json").
// Post("/gamelist").
// End()
func (h *XPHttpImpl) Header(key string, value string) *XPHttpImpl {
h.Headers[key] = value
return h
}
// 用于设置一个重试机制
//
// 例如 每隔5秒重试一次,最多重试3次,当状态为 StatusInternalServerError 或 StatusInternalServerError 时重试
// XPSuperKit.NewHttp().
// Post("/gamelist").
// Retry(3, 5 * time.seconds, http.StatusBadRequest, http.StatusInternalServerError).
// End()
func (h *XPHttpImpl) Retry(retryerCount int, retryerTime time.Duration, statusCode ...int) *XPHttpImpl {
for _, code := range statusCode {
statusText := http.StatusText(code)
if len(statusText) == 0 {
h.Errors = append(h.Errors, ErrorN("StatusCode '"+strconv.Itoa(code)+"' doesn't exist in http package"))
}
}
h.Retryable = struct {
RetryableStatus []int
RetryerTime time.Duration
RetryerCount int
Attempt int
Enable bool
}{
statusCode,
retryerTime,
retryerCount,
0,
true,
}
return h
}
// 用于设置基本认证
//
// 例如 设置认证用户名 authUser 和 认证密码 authPassword
// XPSuperKit.NewHttp().
// Post("/gamelist").
// Auth("authUser", "authPassword").
// End()
func (h *XPHttpImpl) Auth(username string, password string) *XPHttpImpl {
h.BasicAuth = struct{ Username, Password string }{username, password}
return h
}
// 添加 Cookie
func (h *XPHttpImpl) Cookie(c *http.Cookie) *XPHttpImpl {
h.ReqCookies = append(h.ReqCookies, c)
return h
}
// 添加 Cookies
func (h *XPHttpImpl) Cookies(cookies []*http.Cookie) *XPHttpImpl {
h.ReqCookies = append(h.ReqCookies, cookies...)
return h
}
var HTTP_ContentTypes = map[string]string{
"html": "text/html",
"json": "application/json",
"xml": "application/xml",
"text": "text/plain",
"urlencoded": "application/x-www-form-urlencoded",
"form": "application/x-www-form-urlencoded",
"form-data": "application/x-www-form-urlencoded",
"multipart": "multipart/form-data",
}
// 用于设置请求数据类型
//
// 例如 请求将发送 `application/x-www-form-urlencoded` 格式的数据
// XPSuperKit.NewHttp().
// Post("/recipe").
// Type("form").
// Send(`{ "name": "egg benedict", "category": "brunch" }`).
// End()
func (h *XPHttpImpl) ContentType(typeStr string) *XPHttpImpl {
if _, ok := HTTP_ContentTypes[typeStr]; ok {
h.ForceType = typeStr
} else {
h.Errors = append(h.Errors, ErrorN("Type func: incorrect type \""+typeStr+"\""))
}
return h
}
// 用于设置 Query 参数
// For example, making "/search?query=bicycle&size=50x50&weight=20kg" using GET method:
// 例如 要构建 "/search?query=bicycle&size=50x50&weight=20kg" 形式的 Query 参数
// XPSuperKit.NewHttp().
// Get("/search").
// Query(`{ query: 'bicycle' }`).
// Query(`{ size: '50x50' }`).
// Query(`{ weight: '20kg' }`).
// End()
//
// 也可以直接接收 Json 形式的字符串
//
// XPSuperKit.NewHttp().
// Get("/search").
// Query(`{ query: 'bicycle', size: '50x50', weight: '20kg' }`).
// End()
//
// 或者直接是 Query 参数形式(key=value&key=value)字符串
//
// XPSuperKit.NewHttp().
// Get("/search").
// Query("query=bicycle&size=50x50").
// Query("weight=20kg").
// End()
//
// 或者两者混合
//
// XPSuperKit.NewHttp().
// Get("/search").
// Query("query=bicycle").
// Query(`{ size: '50x50', weight:'20kg' }`).
// End()
//
func (h *XPHttpImpl) Query(content interface{}) *XPHttpImpl {
switch v := reflect.ValueOf(content); v.Kind() {
case reflect.String:
h.queryString(v.String())
case reflect.Struct:
h.queryStruct(v.Interface())
case reflect.Map:
h.queryMap(v.Interface())
default:
}
return h
}
func (h *XPHttpImpl) queryStruct(content interface{}) *XPHttpImpl {
if marshalContent, err := json.Marshal(content); err != nil {
h.Errors = append(h.Errors, err)
} else {
var val map[string]interface{}
if err := json.Unmarshal(marshalContent, &val); err != nil {
h.Errors = append(h.Errors, err)
} else {
for k, v := range val {
k = strings.ToLower(k)
var queryVal string
switch t := v.(type) {
case string:
queryVal = t
case float64:
queryVal = strconv.FormatFloat(t, 'f', -1, 64)
case time.Time:
queryVal = t.Format(time.RFC3339)
default:
j, err := json.Marshal(v)
if err != nil {
continue
}
queryVal = string(j)
}
h.QueryData.Add(k, queryVal)
}
}
}
return h
}
func (h *XPHttpImpl) queryString(content string) *XPHttpImpl {
var val map[string]string
if err := json.Unmarshal([]byte(content), &val); err == nil {
for k, v := range val {
h.QueryData.Add(k, v)
}
} else {
if queryData, err := url.ParseQuery(content); err == nil {
for k, queryValues := range queryData {
for _, queryValue := range queryValues {
h.QueryData.Add(k, string(queryValue))
}
}
} else {
h.Errors = append(h.Errors, err)
}
// TODO: need to check correct format of 'field=val&field=val&...'
}
return h
}
func (h *XPHttpImpl) queryMap(content interface{}) *XPHttpImpl {
return h.queryStruct(content)
}
// 用于接收特殊形式的参数,如 fields=f1;f2;f3
func (h *XPHttpImpl) Param(key string, value string) *XPHttpImpl {
h.QueryData.Add(key, value)
return h
}
// 用于设置超时
func (h *XPHttpImpl) Timeout(timeout time.Duration) *XPHttpImpl {
h.Transport.Dial = func(network, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, timeout)
if err != nil {
h.Errors = append(h.Errors, err)
return nil, err
}
conn.SetDeadline(time.Now().Add(timeout))
return conn, nil
}
return h
}
// 用于设置 TLS
//
// 例如 可以用以下形式禁用 HTTPS 安全校验
// XPSuperKit.NewHttp().
// TLS(&tls.Config{ InsecureSkipVerify: true}).
// Get("https://disable-security-check.com").
// End()
func (h *XPHttpImpl) TLS(config *tls.Config) *XPHttpImpl {
h.Transport.TLSClientConfig = config
return h
}
// 用于设置请求代理
//
// XPSuperKit.NewHttp().
// Proxy("http://myproxy:9999").
// Post("http://www.google.com").
// End()
//
// 取消代理, 只需要传空字符串:
//
// XPSuperKit.NewHttp().
// Proxy("").
// Post("http://www.google.com").
// End()
//
func (h *XPHttpImpl) Proxy(proxyUrl string) *XPHttpImpl {
parsedProxyUrl, err := url.Parse(proxyUrl)
if err != nil {
h.Errors = append(h.Errors, err)
} else if proxyUrl == "" {
h.Transport.Proxy = nil
} else {
h.Transport.Proxy = http.ProxyURL(parsedProxyUrl)
}
return h
}
// 用于接收一个函数来处理 Redirect,如果该函数返回 error, 则当跳转指令返回后不会创建下一个请求
// 该函数的入参是即将跳转的 Request 以及之前的 Request 序列
func (h *XPHttpImpl) Redirect(policy func(req HTTPRequest, via []HTTPRequest) error) *XPHttpImpl {
h.Client.CheckRedirect = func(r *http.Request, v []*http.Request) error {
vv := make([]HTTPRequest, len(v))
for i, r := range v {
vv[i] = HTTPRequest(r)
}
return policy(HTTPRequest(r), vv)
}
return h
}
// 用于POST 和 PUT 方法发送数据,无特定参数形式,可以是以下各种类型的参数:
// 1、Json 字符串
// XPSuperKit.NewHttp().
// Post("/search").
// Send(`{ query: 'sushi' }`).
// End()
//
// 2、键值对字符串
// XPSuperKit.NewHttp().
// Post("/search").
// Send("query=tonkatsu").
// End()
//
// 3、Json 与 键值对混合
// XPSuperKit.NewHttp().
// Post("/search").
// Send("query=bicycle&size=50x50").
// Send(`{ wheel: '4'}`).
// End()
//
// 4、Struct
// type BrowserVersionSupport struct {
// Chrome string
// Firefox string
// }
// ver := BrowserVersionSupport{ Chrome: "37.0.2041.6", Firefox: "30.0" }
// XPSuperKit.NewHttp().
// Post("/update_version").
// Send(ver).
// Send(`{"Safari":"5.1.10"}`).
// End()
//
// 5、普通字符串
// XPSuperKit.NewHttp().
// Post("/greet").
// Type("text").
// Send("hello world").
// End()
func (h *XPHttpImpl) Send(content interface{}) *XPHttpImpl {
// TODO: add normal text mode or other mode to Send func
switch v := reflect.ValueOf(content); v.Kind() {
case reflect.String:
h.SendString(v.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: // includes rune
h.SendString(strconv.FormatInt(v.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: // includes byte
h.SendString(strconv.FormatUint(v.Uint(), 10))
case reflect.Float64:
h.SendString(strconv.FormatFloat(v.Float(), 'f', -1, 64))
case reflect.Float32:
h.SendString(strconv.FormatFloat(v.Float(), 'f', -1, 32))
case reflect.Bool:
h.SendString(strconv.FormatBool(v.Bool()))
case reflect.Struct:
h.SendStruct(v.Interface())
case reflect.Slice:
h.SendSlice(makeSliceOfReflectValue(v))
case reflect.Array:
h.SendSlice(makeSliceOfReflectValue(v))
case reflect.Ptr:
h.Send(v.Elem().Interface())
case reflect.Map:
h.SendMap(v.Interface())
default:
// TODO: leave default for handling other types in the future, such as complex numbers, (nested) maps, etc
return h
}
return h
}
func makeSliceOfReflectValue(v reflect.Value) (slice []interface{}) {
kind := v.Kind()
if kind != reflect.Slice && kind != reflect.Array {
return slice
}
slice = make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
slice[i] = v.Index(i).Interface()
}
return slice
}
// 发送 Slice
func (h *XPHttpImpl) SendSlice(content []interface{}) *XPHttpImpl {
h.SliceData = append(h.SliceData, content...)
return h
}
// 发送 Map
func (h *XPHttpImpl) SendMap(content interface{}) *XPHttpImpl {
return h.SendStruct(content)
}
// 发送 Struct
func (h *XPHttpImpl) SendStruct(content interface{}) *XPHttpImpl {
if marshalContent, err := json.Marshal(content); err != nil {
h.Errors = append(h.Errors, err)
} else {
var val map[string]interface{}
d := json.NewDecoder(bytes.NewBuffer(marshalContent))
d.UseNumber()
if err := d.Decode(&val); err != nil {
h.Errors = append(h.Errors, err)
} else {
for k, v := range val {
h.Data[k] = v
}
}
}
return h
}
// 发送字符串
func (h *XPHttpImpl) SendString(content string) *XPHttpImpl {
if !h.BounceToRawString {
var val interface{}
d := json.NewDecoder(strings.NewReader(content))
d.UseNumber()
if err := d.Decode(&val); err == nil {
switch v := reflect.ValueOf(val); v.Kind() {
case reflect.Map:
for k, v := range val.(map[string]interface{}) {
h.Data[k] = v
}
// add to SliceData
case reflect.Slice:
h.SendSlice(val.([]interface{}))
// bounce to rawstring if it is arrayjson, or others
default:
h.BounceToRawString = true
}
} else if formData, err := url.ParseQuery(content); err == nil {
for k, formValues := range formData {
for _, formValue := range formValues {
// make it array if already have key
if val, ok := h.Data[k]; ok {
var strArray []string
strArray = append(strArray, string(formValue))
// check if previous data is one string or array
switch oldValue := val.(type) {
case []string:
strArray = append(strArray, oldValue...)
case string:
strArray = append(strArray, oldValue)
}
h.Data[k] = strArray
} else {
// make it just string if does not already have same key
h.Data[k] = formValue
}
}
}
h.TargetType = "form"
} else {
h.BounceToRawString = true
}
}
// Dump all contents to RawString in case in the end user doesn't want json or form.
h.RawString += content
return h
}
type HTTP_File struct {
Filename string
Fieldname string
Data []byte
}
// 用于通过 "multipart" 形式发送文件
//
// 1、可以以文件路径字符串作为参数
// XPSuperKit.NewHttp().
// Post("http://example.com").
// Type("multipart").
// SendFile("./example_file.ext").
// End()
//
// 2、也可以以已被类似于 ioutil.ReadFile 读取的 []byte Slice 作为参数
// b, _ := ioutil.ReadFile("./example_file.ext")
// XPSuperKit.NewHttp().
// Post("http://example.com").
// Type("multipart").
// SendFile(b).
// End()
//
// 3、也可以以 os.File 作为参数
// f, _ := os.Open("./example_file.ext")
// XPSuperKit.NewHttp().
// Post("http://example.com").
// Type("multipart").
// SendFile(f).
// End()
//
// 4、当参数多于 1 时,第一个参数为文件,可以是路径字符串、已读的[]byte Slice、os.File,当为[]byte Slice时,(第二个参数)默认为"filename",(第二个参数)均可
// 自定义
// b, _ := ioutil.ReadFile("./example_file.ext")
// XPSuperKit.NewHttp().
// Post("http://example.com").
// Type("multipart").
// SendFile(b, "my_custom_filename").
// End()
//
// 5、当参数多于 2 时,第三个参数默认 为 file1, file2, ...(发送多个文件的情况),也可以自定义
// b, _ := ioutil.ReadFile("./example_file.ext")
// XPSuperKit.NewHttp().
// Post("http://example.com").
// Type("multipart").
// SendFile(b, "", "my_custom_fieldname"). // filename left blank, will become "example_file.ext"
// End()
//
func (h *XPHttpImpl) SendFile(file interface{}, args ...string) *XPHttpImpl {
filename := ""
fieldname := "file"
if len(args) >= 1 && len(args[0]) > 0 {
filename = strings.TrimSpace(args[0])
}
if len(args) >= 2 && len(args[1]) > 0 {
fieldname = strings.TrimSpace(args[1])
}
if fieldname == "file" || fieldname == "" {
fieldname = "file" + strconv.Itoa(len(h.FileData)+1)
}
switch v := reflect.ValueOf(file); v.Kind() {
case reflect.String:
pathToFile, err := filepath.Abs(v.String())
if err != nil {
h.Errors = append(h.Errors, err)
return h
}
if filename == "" {
filename = filepath.Base(pathToFile)
}
data, err := ioutil.ReadFile(v.String())
if err != nil {
h.Errors = append(h.Errors, err)
return h
}
h.FileData = append(h.FileData, HTTP_File{
Filename: filename,
Fieldname: fieldname,
Data: data,
})
case reflect.Slice:
slice := makeSliceOfReflectValue(v)
if filename == "" {
filename = "filename"
}
f := HTTP_File{
Filename: filename,
Fieldname: fieldname,
Data: make([]byte, len(slice)),
}
for i := range slice {
f.Data[i] = slice[i].(byte)
}
h.FileData = append(h.FileData, f)
case reflect.Ptr:
if len(args) == 1 {
return h.SendFile(v.Elem().Interface(), args[0])
}
if len(args) >= 2 {
return h.SendFile(v.Elem().Interface(), args[0], args[1])
}
return h.SendFile(v.Elem().Interface())
default:
if v.Type() == reflect.TypeOf(os.File{}) {
osfile := v.Interface().(os.File)
if filename == "" {
filename = filepath.Base(osfile.Name())
}
data, err := ioutil.ReadFile(osfile.Name())
if err != nil {
h.Errors = append(h.Errors, err)
return h
}
h.FileData = append(h.FileData, HTTP_File{
Filename: filename,
Fieldname: fieldname,
Data: data,
})
return h
}
h.Errors = append(h.Errors, ErrorN("SendFile currently only supports either a string (path/to/file), a slice of bytes (file content itself), or a os.File!"))
}
return h
}
func changeMapToURLValues(data map[string]interface{}) url.Values {
var newUrlValues = url.Values{}
for k, v := range data {
switch val := v.(type) {
case string:
newUrlValues.Add(k, val)
case bool:
newUrlValues.Add(k, strconv.FormatBool(val))
// if a number, change to string
// json.Number used to protect against a wrong (for GoRequest) default conversion
// which always converts number to float64.
// This type is caused by using Decoder.UseNumber()
case json.Number:
newUrlValues.Add(k, string(val))
case int:
newUrlValues.Add(k, strconv.FormatInt(int64(val), 10))
// TODO add all other int-Types (int8, int16, ...)
case float64:
newUrlValues.Add(k, strconv.FormatFloat(float64(val), 'f', -1, 64))
case float32:
newUrlValues.Add(k, strconv.FormatFloat(float64(val), 'f', -1, 64))
// following slices are mostly needed for tests
case []string:
for _, element := range val {
newUrlValues.Add(k, element)
}
case []int:
for _, element := range val {
newUrlValues.Add(k, strconv.FormatInt(int64(element), 10))
}
case []bool:
for _, element := range val {
newUrlValues.Add(k, strconv.FormatBool(element))
}
case []float64:
for _, element := range val {
newUrlValues.Add(k, strconv.FormatFloat(float64(element), 'f', -1, 64))
}
case []float32:
for _, element := range val {
newUrlValues.Add(k, strconv.FormatFloat(float64(element), 'f', -1, 64))
}
// these slices are used in practice like sending a struct
case []interface{}:
if len(val) <= 0 {
continue
}
switch val[0].(type) {
case string:
for _, element := range val {
newUrlValues.Add(k, element.(string))
}
case bool:
for _, element := range val {
newUrlValues.Add(k, strconv.FormatBool(element.(bool)))
}
case json.Number:
for _, element := range val {
newUrlValues.Add(k, string(element.(json.Number)))
}
}
default:
// TODO add ptr, arrays, ...
}
}
return newUrlValues
}
// End is the most important function that you need to call when ending the chain. The request won't proceed without calling it.
// End function returns Response which matchs the structure of Response type in Golang's http package (but without Body data). The body data itself returns as a string in a 2nd return value.
// Lastly but worth noticing, error array (NOTE: not just single error value) is returned as a 3rd value and nil otherwise.
//
// For example:
//
// resp, body, errs := gorequest.New().Get("http://www.google.com").End()
// if (errs != nil) {
// fmt.Println(errs)
// }
// fmt.Println(resp, body)
//
// Moreover, End function also supports callback which you can put as a parameter.
// This extends the flexibility and makes GoRequest fun and clean! You can use GoRequest in whatever style you love!
//
// For example:
//
// func printBody(resp gorequest.Response, body string, errs []error){
// fmt.Println(resp.Status)
// }
// gorequest.New().Get("http://www..google.com").End(printBody)
//
func (h *XPHttpImpl) End(callback ...func(response HTTPResponse, body string, errs []error)) (HTTPResponse, string, []error) {
var bytesCallback []func(response HTTPResponse, body []byte, errs []error)
if len(callback) > 0 {
bytesCallback = []func(response HTTPResponse, body []byte, errs []error){
func(response HTTPResponse, body []byte, errs []error) {
callback[0](response, string(body), errs)
},
}
}
resp, body, errs := h.EndBytes(bytesCallback...)
bodyString := string(body)
return resp, bodyString, errs
}
// EndBytes should be used when you want the body as bytes. The callbacks work the same way as with `End`, except that a byte array is used instead of a string.
func (h *XPHttpImpl) EndBytes(callback ...func(response HTTPResponse, body []byte, errs []error)) (HTTPResponse, []byte, []error) {
var (
errs []error
resp HTTPResponse
body []byte
)
for {
resp, body, errs = h.getResponseBytes()
if errs != nil {
return nil, nil, errs
}
if h.isRetryableRequest(resp) {
resp.Header.Set("Retry-Count", strconv.Itoa(h.Retryable.Attempt))
break
}
}
respCallback := *resp
if len(callback) != 0 {
callback[0](&respCallback, body, h.Errors)
}
return resp, body, nil
}
func (h *XPHttpImpl) isRetryableRequest(resp HTTPResponse) bool {
if h.Retryable.Enable && h.Retryable.Attempt < h.Retryable.RetryerCount && contains(resp.StatusCode, h.Retryable.RetryableStatus) {
time.Sleep(h.Retryable.RetryerTime)
h.Retryable.Attempt++
return false
}
return true
}
func contains(respStatus int, statuses []int) bool {
for _, status := range statuses {
if status == respStatus {
return true
}
}
return false
}
// EndStruct should be used when you want the body as a struct. The callbacks work the same way as with `End`, except that a struct is used instead of a string.
func (h *XPHttpImpl) EndStruct(v interface{}, callback ...func(response HTTPResponse, v interface{}, body []byte, errs []error)) (HTTPResponse, []byte, []error) {
resp, body, errs := h.EndBytes()
if errs != nil {
return nil, body, errs
}
err := json.Unmarshal(body, &v)
if err != nil {
h.Errors = append(h.Errors, err)
return resp, body, h.Errors
}
respCallback := *resp
if len(callback) != 0 {
callback[0](&respCallback, v, body, h.Errors)
}
return resp, body, nil
}
func (h *XPHttpImpl) getResponseBytes() (HTTPResponse, []byte, []error) {
var (
req *http.Request
err error
resp HTTPResponse
)
// check whether there is an error. if yes, return all errors
if len(h.Errors) != 0 {
return nil, nil, h.Errors
}
// check if there is forced type
switch h.ForceType {
case "json", "form", "xml", "text", "multipart":
h.TargetType = h.ForceType
// If forcetype is not set, check whether user set Content-Type header.
// If yes, also bounce to the correct supported TargetType automatically.
default:
for k, v := range HTTP_ContentTypes {
if h.Headers["Content-Type"] == v {
h.TargetType = k
}
}
}
// if slice and map get mixed, let's bounce to rawstring
if len(h.Data) != 0 && len(h.SliceData) != 0 {
h.BounceToRawString = true
}
// Make Request
req, err = h.MakeRequest()
if err != nil {
h.Errors = append(h.Errors, err)
return nil, nil, h.Errors