Skip to content

Commit 52da07e

Browse files
authored
feat(123_open): add new driver support for 123 Open (#9246)
- Implement new driver for 123 Open service, enabling file operations such as listing, uploading, moving, and removing files. - Introduce token management for authentication and authorization. - Add API integration for various file operations and actions. - Include utility functions for handling API requests and responses. - Register the new driver in the existing drivers' list.
1 parent 46de9e9 commit 52da07e

File tree

10 files changed

+975
-0
lines changed

10 files changed

+975
-0
lines changed

drivers/123_open/api.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package _123Open
2+
3+
import (
4+
"fmt"
5+
"github.com/go-resty/resty/v2"
6+
"net/http"
7+
)
8+
9+
const (
10+
// baseurl
11+
ApiBaseURL = "https://open-api.123pan.com"
12+
13+
// auth
14+
ApiToken = "/api/v1/access_token"
15+
16+
// file list
17+
ApiFileList = "/api/v2/file/list"
18+
19+
// direct link
20+
ApiGetDirectLink = "/api/v1/direct-link/url"
21+
22+
// mkdir
23+
ApiMakeDir = "/upload/v1/file/mkdir"
24+
25+
// remove
26+
ApiRemove = "/api/v1/file/trash"
27+
28+
// upload
29+
ApiUploadDomainURL = "/upload/v2/file/domain"
30+
ApiSingleUploadURL = "/upload/v2/file/single/create"
31+
ApiCreateUploadURL = "/upload/v2/file/create"
32+
ApiUploadSliceURL = "/upload/v2/file/slice"
33+
ApiUploadCompleteURL = "/upload/v2/file/upload_complete"
34+
35+
// move
36+
ApiMove = "/api/v1/file/move"
37+
38+
// rename
39+
ApiRename = "/api/v1/file/name"
40+
)
41+
42+
type Response[T any] struct {
43+
Code int `json:"code"`
44+
Message string `json:"message"`
45+
Data T `json:"data"`
46+
}
47+
48+
type TokenResp struct {
49+
Code int `json:"code"`
50+
Message string `json:"message"`
51+
Data TokenData `json:"data"`
52+
}
53+
54+
type TokenData struct {
55+
AccessToken string `json:"accessToken"`
56+
ExpiredAt string `json:"expiredAt"`
57+
}
58+
59+
type FileListResp struct {
60+
Code int `json:"code"`
61+
Message string `json:"message"`
62+
Data FileListData `json:"data"`
63+
}
64+
65+
type FileListData struct {
66+
LastFileId int64 `json:"lastFileId"`
67+
FileList []File `json:"fileList"`
68+
}
69+
70+
type DirectLinkResp struct {
71+
Code int `json:"code"`
72+
Message string `json:"message"`
73+
Data DirectLinkData `json:"data"`
74+
}
75+
76+
type DirectLinkData struct {
77+
URL string `json:"url"`
78+
}
79+
80+
type MakeDirRequest struct {
81+
Name string `json:"name"`
82+
ParentID int64 `json:"parentID"`
83+
}
84+
85+
type MakeDirResp struct {
86+
Code int `json:"code"`
87+
Message string `json:"message"`
88+
Data MakeDirData `json:"data"`
89+
}
90+
91+
type MakeDirData struct {
92+
DirID int64 `json:"dirID"`
93+
}
94+
95+
type RemoveRequest struct {
96+
FileIDs []int64 `json:"fileIDs"`
97+
}
98+
99+
type UploadCreateResp struct {
100+
Code int `json:"code"`
101+
Message string `json:"message"`
102+
Data UploadCreateData `json:"data"`
103+
}
104+
105+
type UploadCreateData struct {
106+
FileID int64 `json:"fileId"`
107+
Reuse bool `json:"reuse"`
108+
PreuploadID string `json:"preuploadId"`
109+
SliceSize int64 `json:"sliceSize"`
110+
Servers []string `json:"servers"`
111+
}
112+
113+
type UploadUrlResp struct {
114+
Code int `json:"code"`
115+
Message string `json:"message"`
116+
Data UploadUrlData `json:"data"`
117+
}
118+
119+
type UploadUrlData struct {
120+
PresignedURL string `json:"presignedUrl"`
121+
}
122+
123+
type UploadCompleteResp struct {
124+
Code int `json:"code"`
125+
Message string `json:"message"`
126+
Data UploadCompleteData `json:"data"`
127+
}
128+
129+
type UploadCompleteData struct {
130+
FileID int `json:"fileID"`
131+
Completed bool `json:"completed"`
132+
}
133+
134+
func (d *Open123) Request(endpoint string, method string, setup func(*resty.Request), result any) (*resty.Response, error) {
135+
client := resty.New()
136+
token, err := d.tm.getToken()
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
req := client.R().
142+
SetHeader("Authorization", "Bearer "+token).
143+
SetHeader("Platform", "open_platform").
144+
SetHeader("Content-Type", "application/json").
145+
SetResult(result)
146+
147+
if setup != nil {
148+
setup(req)
149+
}
150+
151+
switch method {
152+
case http.MethodGet:
153+
return req.Get(ApiBaseURL + endpoint)
154+
case http.MethodPost:
155+
return req.Post(ApiBaseURL + endpoint)
156+
case http.MethodPut:
157+
return req.Put(ApiBaseURL + endpoint)
158+
default:
159+
return nil, fmt.Errorf("unsupported method: %s", method)
160+
}
161+
}
162+
163+
func (d *Open123) RequestTo(fullURL string, method string, setup func(*resty.Request), result any) (*resty.Response, error) {
164+
client := resty.New()
165+
166+
token, err := d.tm.getToken()
167+
if err != nil {
168+
return nil, err
169+
}
170+
171+
req := client.R().
172+
SetHeader("Authorization", "Bearer "+token).
173+
SetHeader("Platform", "open_platform").
174+
SetHeader("Content-Type", "application/json").
175+
SetResult(result)
176+
177+
if setup != nil {
178+
setup(req)
179+
}
180+
181+
switch method {
182+
case http.MethodGet:
183+
return req.Get(fullURL)
184+
case http.MethodPost:
185+
return req.Post(fullURL)
186+
case http.MethodPut:
187+
return req.Put(fullURL)
188+
default:
189+
return nil, fmt.Errorf("unsupported method: %s", method)
190+
}
191+
}

0 commit comments

Comments
 (0)