-
Notifications
You must be signed in to change notification settings - Fork 0
/
demopost.go
66 lines (52 loc) · 1.23 KB
/
demopost.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
package go_gin_api
import (
"encoding/json"
"net/url"
"github.com/skyzhouzj/xhframwork/pkg/httpclient"
"github.com/pkg/errors"
)
// 接口地址
var demoPostApi = "http://127.0.0.1:9999/demo/post/"
// 接口返回结构
type demoPostResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}
// DemoPost 发起请求
func DemoPost(name string, opts ...httpclient.Option) (res *demoPostResponse, err error) {
api := demoPostApi
params := url.Values{}
params.Set("name", name)
body, err := httpclient.PostForm(api, params, opts...)
if err != nil {
return nil, err
}
res = new(demoPostResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, errors.Wrap(err, "DemoPost json unmarshal error")
}
return res, nil
}
// DemoPostRetryVerify 设置重试规则
func DemoPostRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoPostAlarmVerify 设置告警规则
func DemoPostAlarmVerify(body []byte) (shouldAlarm bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoPostMock 设置 Mock 数据
func DemoPostMock() (body []byte) {
res := new(demoPostResponse)
res.Name = "BB"
res.Job = "BB_JOB"
body, _ = json.Marshal(res)
return body
}