This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
forked from chyroc/go-aliyundrive
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaliyundrive.go
120 lines (96 loc) · 2.65 KB
/
aliyundrive.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright 2022 chyroc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package aliyundrive
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"
"net/http"
)
const KeyAccessToken = "aliyun_drive_access_token"
const KeyRefreshToken = "aliyun_drive_refresh_token"
const userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62`
type AliyunDrive struct {
logger *logrus.Logger
client *resty.Client
store Store
accessToken string
}
type config struct {
Method string
URL string
Body interface{}
}
func (r *AliyunDrive) request(ctx context.Context, req *config, result interface{}) (*resty.Response, error) {
request := r.client.R().SetContext(ctx)
if req.Body != nil {
request.SetBody(req.Body)
}
if result != nil {
request.SetResult(result)
}
response, err := request.Execute(req.Method, req.URL)
if err != nil {
return nil, err
}
if !response.IsSuccess() {
return nil, fmt.Errorf("%s", response.Status())
}
return response, err
}
func (r *AliyunDrive) log(level logrus.Level, args ...interface{}) {
r.logger.Log(level, args...)
}
func New(options ...OptionFunc) *AliyunDrive {
client := resty.New()
r := &AliyunDrive{
logger: logrus.New(),
client: client,
}
client.OnBeforeRequest(func(client *resty.Client, request *resty.Request) error {
request.SetHeader("User-Agent", userAgent)
request.SetHeader("Referer", "https://www.aliyundrive.com/")
if request.Method == http.MethodPost {
request.SetHeader("Content-Type", "application/json; charset=utf-8")
}
if r.accessToken != "" {
request.SetAuthToken(r.accessToken)
}
return nil
})
for _, v := range options {
if v != nil {
v(r)
}
}
return r
}
type OptionFunc func(*AliyunDrive)
func WithLogger(logger *logrus.Logger) OptionFunc {
return func(ins *AliyunDrive) {
ins.logger = logger
}
}
func WithStore(store Store) OptionFunc {
return func(ins *AliyunDrive) {
token, err := store.Get(context.Background(), KeyAccessToken)
if err == err && token != nil {
ins.accessToken = string(token)
}
ins.store = store
}
}