-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathagent.go
60 lines (52 loc) · 1.55 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
package elevengo
import (
"context"
"github.com/deadblue/elevengo/internal/impl"
"github.com/deadblue/elevengo/internal/protocol"
"github.com/deadblue/elevengo/internal/util"
"github.com/deadblue/elevengo/lowlevel/api"
"github.com/deadblue/elevengo/lowlevel/client"
"github.com/deadblue/elevengo/lowlevel/types"
"github.com/deadblue/elevengo/option"
)
// 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
// Is agent use web credential?
isWeb bool
}
// Default creates an Agent with default settings.
func Default() *Agent {
return New()
}
// New creates Agent with customized options.
func New(options ...*option.AgentOptions) *Agent {
opts := util.NotNull(options...)
if opts == nil {
opts = option.Agent()
}
llc := impl.NewClient(opts.HttpClient, opts.CooldownMinMs, opts.CooldownMaxMs)
appVer := opts.Version
if appVer == "" {
appVer, _ = getLatestAppVersion(llc, api.AppBrowserWindows)
}
llc.SetUserAgent(protocol.MakeUserAgent(opts.Name, api.AppNameBrowser, appVer))
return &Agent{
llc: llc,
common: types.CommonParams{
AppVer: appVer,
},
}
}
func getLatestAppVersion(llc client.Client, appType string) (appVer string, err error) {
spec := (&api.AppVersionSpec{}).Init()
if err = llc.CallApi(spec, context.Background()); err == nil {
versionInfo := spec.Result[appType]
appVer = versionInfo.VersionCode
}
return
}