-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.go
233 lines (205 loc) · 4.91 KB
/
util.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package bilibili
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"time"
"unicode"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
type paramHandler func(*resty.Request) error
func fillCsrf(c *Client) paramHandler {
return func(r *resty.Request) error {
csrf := c.getCookie("bili_jct")
if len(csrf) == 0 {
return errors.New("B站登录过期")
}
r.SetQueryParam("csrf", csrf)
r.SetQueryParam("csrf_token", csrf)
return nil
}
}
func fillParam(key, value string) paramHandler {
return func(r *resty.Request) error {
r.SetQueryParam(key, value)
return nil
}
}
func fillWbiHandler(wbi *WBI, cookies []*http.Cookie) func(*resty.Request) error {
return func(r *resty.Request) error {
newQuery, err := wbi.SignQuery(r.QueryParam, time.Now())
if err != nil {
return err
}
r.QueryParam = newQuery
r.Cookies = cookies
r.Header.Del("Referer")
return nil
}
}
// execute 发起请求
func execute[Out any](c *Client, method, url string, in any, handlers ...paramHandler) (out Out, err error) {
r := c.resty.R()
if err = withParams(r, in); err != nil {
return
}
for _, handler := range handlers {
if err = handler(r); err != nil {
return
}
}
resp, err := r.Execute(method, url)
if err != nil {
return out, errors.WithStack(err)
}
if resp.StatusCode() != 200 {
return out, errors.Errorf("status code: %d", resp.StatusCode())
}
c.SetCookies(resp.Cookies())
var cr commonResp[Out]
if err = json.Unmarshal(resp.Body(), &cr); err != nil {
return out, errors.WithStack(err)
}
if cr.Code != 0 {
return out, errors.WithStack(Error{Code: cr.Code, Message: cr.Message})
}
return cr.Data, errors.WithStack(err)
}
type commonResp[T any] struct {
Code int `json:"code"`
Message string `json:"message"`
Data T `json:"data"`
}
func withParams(r *resty.Request, in any) error {
if in == nil {
return nil
}
inType := reflect.TypeOf(in)
inValue := reflect.ValueOf(in)
switch inType.Kind() {
case reflect.Ptr:
// 如果是空指针,直接返回
if inValue.IsNil() {
return nil
}
inType = inType.Elem()
inValue = inValue.Elem()
case reflect.Struct:
default:
return errors.New("参数类型错误")
}
bodyMap := make(map[string]any, 4)
contentType := ""
for i := 0; i < inType.NumField(); i++ {
fieldType := inType.Field(i)
if !fieldType.IsExported() {
continue
}
fieldValue := inValue.Field(i)
tValue := fieldType.Tag.Get("request")
if tValue == "-" {
continue
}
// 获取字段名
var fieldName string
tagMap := parseTag(tValue)
if name, ok := tagMap["field"]; ok {
fieldName = name
} else if jsonValue := fieldType.Tag.Get("json"); jsonValue != "" && jsonValue != "-" {
if index := strings.Index(jsonValue, ","); index != -1 {
jsonValue = jsonValue[:index]
}
fieldName = jsonValue
} else {
fieldName = toSnakeCase(fieldType.Name)
}
var realVal any
if !fieldValue.IsZero() {
realVal = fieldValue.Interface()
} else {
// 设置了 omitempty 代表不传
if _, ok := tagMap["omitempty"]; ok {
continue
}
// 设置了 default 代表使用默认值
if v, ok := tagMap["default"]; ok {
realVal = v
} else {
// 否则使用零值
realVal = fieldValue.Interface()
}
}
contentType = "application/x-www-form-urlencoded"
for name := range tagMap {
switch name {
case "query":
contentType = "application/x-www-form-urlencoded"
case "json":
contentType = "application/json"
case "form-data":
contentType = "multipart/form-data"
}
}
if contentType == "application/x-www-form-urlencoded" {
_, ok1 := tagMap["json"]
_, ok2 := tagMap["form-data"]
if !ok1 && !ok2 {
// 对query类型的字段进行特殊处理
if fieldType.Type.Kind() == reflect.Slice {
strSlice := make([]string, 0, 4)
for i := 0; i < fieldValue.Len(); i++ {
strSlice = append(strSlice, cast.ToString(fieldValue.Index(i).Interface()))
}
realVal = strings.Join(strSlice, ",")
}
}
r.SetQueryParam(fieldName, cast.ToString(realVal))
} else {
bodyMap[fieldName] = realVal
}
}
r.SetHeader("Content-Type", contentType)
if len(bodyMap) > 0 {
r.SetBody(bodyMap)
}
return nil
}
func parseTag(tag string) map[string]string {
parts := strings.Split(tag, ",")
pMap := make(map[string]string, 10)
for _, part := range parts {
kv := strings.Split(part, "=")
if len(kv) == 1 {
pMap[kv[0]] = ""
} else {
pMap[kv[0]] = kv[1]
}
}
return pMap
}
func toSnakeCase(s string) string {
var result strings.Builder
result.Grow(len(s) * 2)
for i, r := range s {
if unicode.IsUpper(r) {
if i > 0 {
result.WriteRune('_')
}
result.WriteRune(unicode.ToLower(r))
} else {
result.WriteRune(r)
}
}
return result.String()
}
type Error struct {
Code int
Message string
}
func (e Error) Error() string {
return fmt.Sprintf("错误码: %d, 错误信息: %s", e.Code, e.Message)
}