-
Notifications
You must be signed in to change notification settings - Fork 27
/
agent.go
71 lines (63 loc) · 1.79 KB
/
agent.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
package elevengo
import (
"context"
"github.com/deadblue/elevengo/internal/impl"
"github.com/deadblue/elevengo/internal/protocol"
"github.com/deadblue/elevengo/lowlevel/api"
"github.com/deadblue/elevengo/lowlevel/client"
"github.com/deadblue/elevengo/lowlevel/types"
"github.com/deadblue/elevengo/option"
"github.com/deadblue/elevengo/plugin"
)
// Agent holds signed-in user's credentials, and provides methods to access upstream
// server's features, such as file management, offline download, etc.
type Agent struct {
// Low-level API client
llc *impl.ClientImpl
// Common parameters
common types.CommonParams
// isWeb indicates whether the credential is for web.
// Some API should use PC version when credential is not for web.
isWeb bool
}
// Default creates an Agent with default settings.
func Default() *Agent {
return New()
}
// New creates Agent with customized options.
func New(options ...option.AgentOption) *Agent {
var hc plugin.HttpClient = nil
var cdMin, cdMax uint
var name, appVer string
// Scan options
for _, opt := range options {
switch opt := opt.(type) {
case option.AgentNameOption:
name = string(opt)
case option.AgentCooldownOption:
cdMin, cdMax = opt.Min, opt.Max
case *option.AgentHttpOption:
hc = opt.Client
case option.AgentVersionOption:
appVer = string(opt)
}
}
llc := impl.NewClient(hc, cdMin, cdMax)
if appVer == "" {
appVer, _ = getLatestAppVersion(llc)
}
llc.SetUserAgent(protocol.MakeUserAgent(name, appVer))
return &Agent{
llc: llc,
common: types.CommonParams{
AppVer: appVer,
},
}
}
func getLatestAppVersion(llc client.Client) (appVer string, err error) {
spec := (&api.AppVersionSpec{}).Init()
if err = llc.CallApi(spec, context.Background()); err == nil {
appVer = spec.Result.LinuxApp.VersionCode
}
return
}