-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
150 lines (130 loc) · 3.16 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
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
package http_client_helper
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type DataWrapper interface {
DataSetter
ErrInt
}
type DataSetter interface {
SetData(interface{}) DataWrapper //设置 data 并返回 一个 copy
}
type ErrInt interface {
Err() error
}
type H struct {
c *http.Client
domain string
data DataWrapper
requestFunc func(*http.Request)
}
//最合理的接口用这个就行
func NewDefault(url string, requestFunc func(*http.Request)) *H {
return New(http.DefaultClient, url, &Resp{}, requestFunc)
}
func New(c *http.Client, url string, data DataWrapper, requestFunc func(*http.Request)) *H {
return &H{c: c, domain: url, data: data, requestFunc: requestFunc}
}
func (c *H) GetUrl(u string, p fmt.Stringer) string {
if p == nil {
return fmt.Sprintf("%s%s", c.domain, u)
}
return fmt.Sprintf("%s%s?%s", c.domain, u, p)
}
func (c *H) Do(req *http.Request, des interface{}) error {
resp, err := c.c.Do(req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body)
if err := json.Unmarshal(body, des); err != nil {
return fmt.Errorf("unmarshal failed, req:%s, resp:%s, data:%s, err:%w", req.URL, string(body), des, err)
}
return nil
}
func (c *H) Post(ctx context.Context, uri string, p, des interface{}) (err error) {
return c.DoMethod(ctx, uri, p, des, http.MethodPost)
}
func (c *H) Put(ctx context.Context, uri string, p, des interface{}) (err error) {
return c.DoMethod(ctx, uri, p, des, http.MethodPut)
}
func (c *H) Delete(ctx context.Context, uri string, p, des interface{}) (err error) {
return c.DoMethod(ctx, uri, p, des, http.MethodDelete)
}
func (c *H) Get(ctx context.Context, uri string, p Params, des interface{}) (err error) {
u := c.GetUrl(uri, p)
by, err := json.Marshal(p)
if err != nil {
return
}
b := bytes.NewReader(by)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, b)
c.requestFunc(req)
resp := c.data.SetData(des)
err = c.Do(req, &resp)
if err != nil {
return
}
return resp.Err()
}
func (c *H) DoMethod(ctx context.Context, uri string, p, des interface{}, method string) (err error) {
u := c.GetUrl(uri, nil)
fmt.Println(u)
by, err := json.Marshal(p)
if err != nil {
return
}
b := bytes.NewReader(by)
req, err := http.NewRequestWithContext(ctx, method, u, b)
c.requestFunc(req)
resp := c.data.SetData(des)
err = c.Do(req, &resp)
if err != nil {
return
}
return resp.Err()
}
type Params map[string]interface{}
func (p Params) String() string {
a := &url.Values{}
for k, v := range p {
var r string
switch vv := v.(type) {
case string:
r = vv
case int:
r = strconv.Itoa(vv)
case int64:
r = strconv.FormatInt(vv, 10)
}
a.Add(k, r)
}
return a.Encode()
}
// default Resp
type Resp struct {
Code int32 `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func (r Resp) SetData(i interface{}) DataWrapper {
r.Data = i
return &r
}
func (r Resp) Err() (err error) {
if r.Code != 0 {
err = fmt.Errorf("%s code: %d", r.Message, r.Code)
}
if e, ok := r.Data.(ErrInt); ok {
return e.Err()
}
return
}