Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/kodbox/util.go
1987 views
1
package kodbox
2
3
import (
4
"fmt"
5
"github.com/alist-org/alist/v3/drivers/base"
6
"github.com/alist-org/alist/v3/pkg/utils"
7
"github.com/go-resty/resty/v2"
8
"strings"
9
)
10
11
func (d *KodBox) getToken() error {
12
var authResp CommonResp
13
res, err := base.RestyClient.R().
14
SetResult(&authResp).
15
SetQueryParams(map[string]string{
16
"name": d.UserName,
17
"password": d.Password,
18
}).
19
Post(d.Address + "/?user/index/loginSubmit")
20
if err != nil {
21
return err
22
}
23
if res.StatusCode() >= 400 {
24
return fmt.Errorf("get token failed: %s", res.String())
25
}
26
27
if res.StatusCode() == 200 && authResp.Code.(bool) == false {
28
return fmt.Errorf("get token failed: %s", res.String())
29
}
30
31
d.authorization = fmt.Sprintf("%s", authResp.Info)
32
return nil
33
}
34
35
func (d *KodBox) request(method string, pathname string, callback base.ReqCallback, noRedirect ...bool) ([]byte, error) {
36
full := pathname
37
if !strings.HasPrefix(pathname, "http") {
38
full = d.Address + pathname
39
}
40
req := base.RestyClient.R()
41
if len(noRedirect) > 0 && noRedirect[0] {
42
req = base.NoRedirectClient.R()
43
}
44
req.SetFormData(map[string]string{
45
"accessToken": d.authorization,
46
})
47
callback(req)
48
49
var (
50
res *resty.Response
51
commonResp *CommonResp
52
err error
53
skip bool
54
)
55
for i := 0; i < 2; i++ {
56
if skip {
57
break
58
}
59
res, err = req.Execute(method, full)
60
if err != nil {
61
return nil, err
62
}
63
64
err := utils.Json.Unmarshal(res.Body(), &commonResp)
65
if err != nil {
66
return nil, err
67
}
68
69
switch commonResp.Code.(type) {
70
case bool:
71
skip = true
72
case string:
73
if commonResp.Code.(string) == "10001" {
74
err = d.getToken()
75
if err != nil {
76
return nil, err
77
}
78
req.SetFormData(map[string]string{"accessToken": d.authorization})
79
}
80
}
81
}
82
if commonResp.Code.(bool) == false {
83
return nil, fmt.Errorf("request failed: %s", commonResp.Data)
84
}
85
return res.Body(), nil
86
}
87
88