-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
143 lines (117 loc) · 2.8 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
package wopan
import (
"encoding/json"
"net/http"
"path"
"sync"
"github.com/go-resty/resty/v2"
)
type WoClient struct {
accessToken string
refreshToken string
psToken string
client *resty.Client
crypto *Crypto
ua string
jsonMarshalFunc func(v interface{}) ([]byte, error)
jsonUnmarshalFunc func(data []byte, v interface{}) error
Phone string
ZoneURL string
zoneURLOnce sync.Once
ClassifyRuleData *ClassifyRuleData
onRefreshToken func(accessToken, refreshToken string)
}
func New(opts ...Option) *WoClient {
w := &WoClient{
client: resty.New(),
crypto: NewCrypto(),
jsonMarshalFunc: json.Marshal,
jsonUnmarshalFunc: json.Unmarshal,
}
for _, opt := range opts {
opt(w)
}
return w
}
func DefaultWithAccessToken(accessToken string) *WoClient {
w := Default()
w.SetAccessToken(accessToken)
return w
}
func DefaultWithRefreshToken(refreshToken string) *WoClient {
w := Default()
w.SetRefreshToken(refreshToken)
return w
}
func DefaultWithAccessAndPsToken(refreshToken, psToken string) *WoClient {
w := Default()
w.SetAccessToken(refreshToken)
w.SetPsToken(psToken)
return w
}
func Default() *WoClient {
return New(WithUA(DefaultUA))
}
func (w *WoClient) SetUA(ua string) {
w.ua = ua
}
func (w *WoClient) SetJsonMarshalFunc(f func(v interface{}) ([]byte, error)) {
w.jsonMarshalFunc = f
}
func (w *WoClient) SetJsonUnmarshalFunc(f func(data []byte, v interface{}) error) {
w.jsonUnmarshalFunc = f
}
func (w *WoClient) SetAccessToken(token string) {
w.accessToken = token
_ = w.crypto.SetAccessToken(token)
}
func (w *WoClient) SetRefreshToken(token string) {
w.refreshToken = token
}
func (w *WoClient) SetPsToken(psToken string) {
w.psToken = psToken
}
func (w *WoClient) GetToken() (string, string) {
return w.accessToken, w.refreshToken
}
func (w *WoClient) SetHttpClient(httpClient *http.Client) *WoClient {
w.client = resty.NewWithClient(httpClient)
return w
}
func (w *WoClient) SetUserAgent(userAgent string) *WoClient {
w.client.SetHeader("User-Agent", userAgent)
return w
}
func (w *WoClient) SetDebug(d bool) *WoClient {
w.client.SetDebug(d)
return w
}
func (w *WoClient) EnableTrace() *WoClient {
w.client.EnableTrace()
return w
}
func (w *WoClient) SetProxy(proxy string) *WoClient {
w.client.SetProxy(proxy)
return w
}
func (w *WoClient) NewRequest() *resty.Request {
return w.client.R()
}
func (w *WoClient) GetFileType(filename string) string {
ext := path.Ext(filename)
if ext == "" {
return "5"
}
ext = ext[1:]
err := w.InitClassifyRule()
if err != nil {
return "5"
}
if _type, ok := w.ClassifyRuleData.FileTypes[ext]; ok {
return _type.Type
}
return "5"
}
func (w *WoClient) OnRefreshToken(f func(accessToken, refreshToken string)) {
w.onRefreshToken = f
}