-
Notifications
You must be signed in to change notification settings - Fork 27
/
client.go
67 lines (56 loc) · 1.12 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
package impl
import (
"net/http"
"net/http/cookiejar"
"github.com/deadblue/elevengo/internal/crypto/ec115"
"github.com/deadblue/elevengo/plugin"
)
type ClientImpl struct {
// HTTP client
hc plugin.HttpClient
// Cookie jar
cj http.CookieJar
// Should Client manage cookie
mc bool
// User agent
ua string
// EC cipher
ecc *ec115.Cipher
// Valve to control API request frequency
v Valve
}
func NewClient(hc plugin.HttpClient, cdMin uint, cdMax uint) *ClientImpl {
impl := &ClientImpl{
ecc: ec115.New(),
}
if hc == nil {
impl.cj, _ = cookiejar.New(nil)
impl.hc = defaultHttpClient(impl.cj)
impl.mc = false
} else {
impl.hc = hc
switch hc := hc.(type) {
case *http.Client:
impl.cj = hc.Jar
case plugin.HttpClientWithJar:
impl.cj = hc.Jar()
}
if impl.cj != nil {
impl.mc = false
} else {
impl.mc = true
impl.cj, _ = cookiejar.New(nil)
}
}
if cdMax > 0 && cdMax >= cdMin {
impl.v.enabled = true
impl.v.cdMin, impl.v.cdMax = cdMin, cdMax
}
return impl
}
func (c *ClientImpl) SetUserAgent(name string) {
c.ua = name
}
func (c *ClientImpl) GetUserAgent() string {
return c.ua
}