This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
/
client.go
79 lines (69 loc) · 1.59 KB
/
client.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
/**
* Copyright 2019 godog Author. All rights reserved.
* Author: Chuck1024
*/
package httplib
import (
"errors"
"fmt"
"github.com/parnurzeal/gorequest"
"net/http"
"strings"
"time"
)
type HttpClient struct {
Timeout time.Duration
Domain string
}
func (c *HttpClient) Start() error {
if c.Timeout <= 0 {
c.Timeout = 3 * time.Second
}
if c.Domain == "" {
return errors.New("http client need domain")
}
if !strings.HasPrefix(c.Domain, "http") {
return fmt.Errorf("need protocol %v", c.Domain)
}
return nil
}
func (c *HttpClient) Method(method string, path string, header map[string]string, params interface{}) (*http.Response, string, error) {
return c.MethodTimeout(method, path, header, params, c.Timeout)
}
func (c *HttpClient) MethodTimeout(method string, path string, header map[string]string, params interface{}, timeout time.Duration) (*http.Response, string, error) {
dm := c.Domain
if !strings.HasSuffix(dm, "/") && !strings.HasPrefix(path, "/") {
dm = dm + "/"
}
target := dm + path
a := gorequest.New().Timeout(timeout).CustomMethod(strings.ToUpper(method), target)
for k, v := range header {
a.Set(k, v)
}
if params != nil {
if strings.ToUpper(method) == "GET" {
a.Query(params)
} else {
a.Send(params)
}
}
resp, body, errs := a.End()
var err error
el := len(errs)
if el > 0 {
if el == 1 {
err = errs[0]
} else {
var errStrs []string
for _, e := range errs {
if e != nil {
errStrs = append(errStrs, e.Error())
}
}
if len(errStrs) > 0 {
err = errors.New(strings.Join(errStrs, "|"))
}
}
}
return resp, body, err
}