-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
59 lines (50 loc) · 1.29 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
package bili
import (
"errors"
"github.com/juju/persistent-cookiejar"
"net/http"
)
// Client represents a client for the bili-go APIs
type Client struct {
Auth
Zone
config *Config
}
func setup(client *Client) (*Client, error) {
// try loading cookies
var err error
var jar *cookiejar.Jar
var jarPath = client.config.Cookies
if jar, err = cookiejar.New(&cookiejar.Options{
PublicSuffixList: nil,
Filename: jarPath,
}); err != nil {
// if cannot loading cookie file from
// then create a new cookie jar
if jar, err = cookiejar.New(nil); err != nil {
return nil, err
}
}
client.client.Jar = jar // set the cookie jar
return client, nil
}
// New use the DefaultConfig if the path to a config.json is not specified
func New() (client *Client, err error) {
client = &Client{
Auth: Auth{client: &http.Client{}},
}
client.config = &DefaultConfig
return setup(client)
}
// NewWithConfig create a bili client if the path to a config.json file is specified
func NewWithConfig(configPath string) (client *Client, err error) {
client = &Client{
Auth: Auth{client: &http.Client{}},
}
var config *Config
if config, err = LoadFromJSON(configPath); err != nil {
return nil, errors.New("cannot load config: " + err.Error())
}
client.config = config
return setup(client)
}