Debian Codegraph @

link_folder.go

raw
1 package proton
2
3 import (
4 "context"
5 "fmt"
6 "strconv"
7
8 "github.com/bradenaw/juniper/xslices"
9 "github.com/go-resty/resty/v2"
10 )
11
12 func (c *Client) ListChildren(ctx context.Context, shareID, linkID string, showAll bool) ([]Link, error) {
13 var res struct {
14 Links []Link
15 }
16
17 var links []Link
18
19 for page := 0; ; page++ {
20 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
21 return r.
22 SetQueryParams(map[string]string{
23 "Page": strconv.Itoa(page),
24 "PageSize": strconv.Itoa(maxPageSize),
25 "ShowAll": Bool(showAll).FormatURL(),
26 }).
27 SetResult(&res).
28 Get("/drive/shares/" + shareID + "/folders/" + linkID + "/children")
29 }); err != nil {
30 return nil, err
31 }
32
33 if len(res.Links) == 0 {
34 break
35 }
36
37 links = append(links, res.Links...)
38 }
39
40 return links, nil
41 }
42
43 func (c *Client) TrashChildren(ctx context.Context, shareID, linkID string, childIDs ...string) error {
44 var res struct {
45 Responses []struct {
46 LinkID string
47 Response APIError
48 }
49 }
50
51 for _, childIDs := range xslices.Chunk(childIDs, maxPageSize) {
52 req := struct {
53 LinkIDs []string
54 }{
55 LinkIDs: childIDs,
56 }
57
58 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
59 return r.SetBody(req).SetResult(&res).Post("/drive/shares/" + shareID + "/folders/" + linkID + "/trash_multiple")
60 }); err != nil {
61 return err
62 }
63
64 for _, res := range res.Responses {
65 if res.Response.Code != SuccessCode {
66 return fmt.Errorf("failed to trash child: %w", res.Response)
67 }
68 }
69 }
70
71 return nil
72 }
73
74 func (c *Client) EmptyTrash(ctx context.Context, shareID string) error {
75 var res struct {
76 APIError
77 }
78
79 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
80 return r.SetResult(&res).Delete("/drive/shares/" + shareID + "/trash")
81 }); err != nil {
82 return err
83 }
84
85 return nil
86 }
87
88 func (c *Client) DeleteChildren(ctx context.Context, shareID, linkID string, childIDs ...string) error {
89 var res struct {
90 Responses []struct {
91 LinkID string
92 Response APIError
93 }
94 }
95
96 for _, childIDs := range xslices.Chunk(childIDs, maxPageSize) {
97 req := struct {
98 LinkIDs []string
99 }{
100 LinkIDs: childIDs,
101 }
102
103 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
104 return r.SetBody(req).SetResult(&res).Post("/drive/shares/" + shareID + "/folders/" + linkID + "/delete_multiple")
105 }); err != nil {
106 return err
107 }
108
109 for _, res := range res.Responses {
110 if res.Response.Code != SuccessCode {
111 return fmt.Errorf("failed to delete child: %w", res.Response)
112 }
113 }
114 }
115
116 return nil
117 }