-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
100 lines (83 loc) · 1.96 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
package jike_sdk_go
import (
"context"
"errors"
"log"
"github.com/go-resty/resty/v2"
)
const (
apiBaseURL = "https://api.ruguoapp.com"
apiVersion = "/1.0"
uploadBaseURL = "https://upload.ruguoapp.com"
uploadVersion = "/1.0"
qiNiuBaseURL = "https://upload.qiniup.com"
)
var (
ErrUnknown = errors.New("unknown error")
)
func WithDebug(debug bool) OptFunc {
return func(o *options) {
o.debug = debug
}
}
type OptFunc func(*options)
type options struct {
debug bool
}
func NewClient(opts ...OptFunc) *Client {
o := new(options)
for _, f := range opts {
f(o)
}
c := &Client{
opts: o,
rc: resty.New(),
}
if c.opts.debug {
c.rc.EnableTrace().
OnBeforeRequest(func(client *resty.Client, request *resty.Request) error {
log.Printf("url: %s\n", request.URL)
return nil
}).
OnAfterResponse(func(client *resty.Client, response *resty.Response) error {
log.Printf("header: %v\n", response.Header())
log.Printf("cookies: %v\n", response.Cookies())
log.Printf("body: %s\n", response.Body())
return nil
})
}
return c
}
type Client struct {
opts *options
rc *resty.Client
// status info
xAccessToken string
xRefreshToken string
xRequestID string
}
func (c *Client) apiR(ctx context.Context) *resty.Request {
r := c.rc.R().
SetContext(ctx).
SetHeader("Host", mockAPIHost).
SetHeader("x-jike-access-token", c.xAccessToken).
SetHeader("x-jike-refresh-token", c.xRefreshToken).
SetHeaders(mockAPIHeaders)
return r
}
func (c *Client) uploadTokenR(ctx context.Context) *resty.Request {
r := c.rc.R().
SetContext(ctx).
SetHeader("Host", mockUploadTokenHost).
SetHeader("x-jike-access-token", c.xAccessToken).
SetHeader("x-jike-refresh-token", c.xRefreshToken).
SetHeaders(mockAPIHeaders)
return r
}
func (c *Client) uploadR(ctx context.Context, ut string) *resty.Request {
r := c.rc.R().
SetContext(ctx).
SetHeaders(mockUploadHeaders).
SetHeader("User-Agent", mockUploadUserAgent(ut))
return r
}