forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
link.go
125 lines (103 loc) · 3.43 KB
/
link.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
121
122
123
124
125
package proton
import (
"context"
"encoding/json"
"net/http"
"github.com/go-resty/resty/v2"
)
func (c *Client) GetLink(ctx context.Context, shareID, linkID string) (Link, error) {
var res struct {
Link Link
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get("/drive/shares/" + shareID + "/links/" + linkID)
}); err != nil {
return Link{}, err
}
return res.Link, nil
}
func (c *Client) MoveLink(ctx context.Context, shareID, linkID string, req MoveLinkReq) error {
var res struct {
Code int
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Put("/drive/shares/" + shareID + "/links/" + linkID + "/move")
}); err != nil {
return err
}
return nil
}
func (c *Client) CreateFile(ctx context.Context, shareID string, req CreateFileReq) (CreateFileRes, error) {
var res struct {
Code int
File CreateFileRes
}
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/files")
})
if err != nil { // if the status code is not 200~299, it's considered an error
// handle the file or folder name exists error
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFileRes{}, err
}
if apiError.Code == AFileOrFolderNameExist {
return CreateFileRes{}, ErrFileNameExist // since we are in CreateFile, so we return this error
}
}
// handle draft exists error
if resp.StatusCode() == http.StatusConflict /* 409 */ {
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFileRes{}, err
}
if apiError.Code == ADraftExist {
return CreateFileRes{}, ErrADraftExist
}
}
return CreateFileRes{}, err
}
return res.File, nil
}
func (c *Client) CreateFolder(ctx context.Context, shareID string, req CreateFolderReq) (CreateFolderRes, error) {
var res struct {
Folder CreateFolderRes
}
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/folders")
})
if err != nil { // if the status code is not 200~299, it's considered an error
// handle the file or folder name exists error
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
// log.Println(resp.String())
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFolderRes{}, err
}
if apiError.Code == AFileOrFolderNameExist {
return CreateFolderRes{}, ErrFolderNameExist // since we are in CreateFolder, so we return this error
}
}
return CreateFolderRes{}, err
}
return res.Folder, nil
}
func (c *Client) CheckAvailableHashes(ctx context.Context, shareID, linkID string, req CheckAvailableHashesReq) (CheckAvailableHashesRes, error) {
var res struct {
AvailableHashes []string
PendingHashesData []PendingHashData
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/links/" + linkID + "/checkAvailableHashes")
}); err != nil {
return CheckAvailableHashesRes{}, err
}
return CheckAvailableHashesRes{
AvailableHashes: res.AvailableHashes,
PendingHashesData: res.PendingHashesData,
}, nil
}