-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
93 lines (80 loc) · 2.03 KB
/
request.go
File metadata and controls
93 lines (80 loc) · 2.03 KB
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
package fans
import (
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
"time"
)
const (
applicationJson = "application/json"
applicationForm = "application/x-www-form-urlencoded"
)
// E 一个键值对
type E struct {
name string
value interface{}
}
// ES 一组有顺序的键值对
type ES []E
func (e ES) Map() M {
m := make(M)
for i := range e {
m[e[i].name] = e[i].value
}
return m
}
// M 一组无顺序的键值对
type M map[string]interface{}
func (m M) Param() string {
v := make(url.Values)
for k := range m {
v.Add(k, fmt.Sprint(m[k]))
}
return v.Encode()
}
type Request interface {
Get(u string, param M, body io.Reader, header ...E) (*gjson.Result, error)
Post(u string, param M, body io.Reader, header ...E) (*gjson.Result, error)
}
type R struct {
c *http.Client
reqHeader map[string]string
}
func NewR(timeout int) *R {
c := http.Client{Timeout: time.Duration(timeout) * time.Second}
return &R{
c: &c,
reqHeader: map[string]string{
"User-Agent": "Mozilla/5.0 BiliDroid/6.73.1 (bbcallen@gmail.com) os/android model/Mi 10 Pro mobi_app/android build/6731100 channel/xiaomi innerVer/6731110 osVer/12 network/2",
"Accept": applicationJson,
"Accept-Language": "zh-CN,zh;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
},
}
}
func (r *R) request(method string, u string, param M,
body io.Reader, header ...E) (*gjson.Result, error) {
var req *http.Request
var err error
req, err = http.NewRequest(method, u+"?"+param.Param(), body)
if err != nil {
return nil, err
}
h := req.Header
rh := r.reqHeader
for k := range rh {
h.Add(k, rh[k])
}
for i := range header {
h.Add(header[i].name, fmt.Sprint(header[i].value))
}
return handResp(r.c.Do(req))
}
func (r *R) Get(u string, param M, body io.Reader, header ...E) (*gjson.Result, error) {
return r.request(http.MethodGet, u, param, body, header...)
}
func (r *R) Post(u string, param M, body io.Reader, header ...E) (*gjson.Result, error) {
return r.request(http.MethodPost, u, param, body, header...)
}