forked from tj/go-dropbox
/
paper.go
257 lines (221 loc) · 8.19 KB
/
paper.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package dropbox
import (
"context"
"encoding/json"
"io"
"time"
)
// Paper client for Dropbox Paper.
type Paper struct {
*Client
}
// NewPaper cretes a new Dropbox Paper client.
func NewPaper(config *Config) *Paper {
return &Paper{
Client: &Client{
Config: config,
},
}
}
const (
// PaperDocsFilterAccessed indicates that for this paper/docs/list call we
// only want to get documents that the user accessed.
PaperDocsFilterAccessed = "docs_accessed"
// PaperDocsFilterCreated indicates that for this paper/docs/list call we
// only want to get documents that the user created.
PaperDocsFilterCreated = "docs_created"
// PaperDocsSortByAccessed indicates that for this paper/docs/list call we
// want to sort the returned doc IDs in the order in which the documents
// were accessed by the user
PaperDocsSortByAccessed = "accessed"
// PaperDocsSortByModified indicates that for this paper/docs/list call we
// want to sort the returned doc IDs in the order in which the documents
// were modified by any user
PaperDocsSortByModified = "modified"
// PaperDocsSortByCreated indicates that for this paper/docs/list call we
// want to sort the returned doc IDs in the order in which the documents
// were created
PaperDocsSortByCreated = "created"
// PaperDocsSortAscending indicates that for this paper/docs/list call we
// want to return the doc IDs in ascending order for how they are sorted
PaperDocsSortAscending = "ascending"
// PaperDocsSortDescending indicates that for this paper/docs/list call we
// want to return the doc IDs in decending order for how they are sorted
PaperDocsSortDescending = "decending"
)
// PaperDocsListInput is the payload for /paper/docs/list requests.
type PaperDocsListInput struct {
FilterBy string `json:"filter_by,omitempty"`
SortBy string `json:"sort_by,omitempty"`
SortOrder string `json:"sort_order,omitempty"`
Limit int `json:"limit,omitempty"`
}
// PaperDocsListCursor is a cursor to use in /paper/docs/list/continue calls
// to continue listing papers where the previous list call left off.
type PaperDocsListCursor struct {
Value string `json:"value"`
Expiration time.Time `json:"expiration"`
}
// PaperDocsListOutput is the response format for /paper/docs/list and
// /paper/docs/list/continue.
type PaperDocsListOutput struct {
DocIDs []string `json:"doc_ids"`
Cursor PaperDocsListCursor `json:"cursor"`
HasMore bool `json:"has_more"`
}
// ListDocs returns the documents in a user's Dropbox Paper.
func (c *Paper) ListDocs(ctx context.Context, in *PaperDocsListInput) (out *PaperDocsListOutput, err error) {
body, err := c.call(ctx, "/paper/docs/list", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// PaperDocsListContinueInput is the payload for /paper/docs/list/continue
// requests.
type PaperDocsListContinueInput struct {
Cursor string `json:"cursor"`
}
// ListDocsContinue paginates using the cursor from ListDocs.
func (c *Paper) ListDocsContinue(ctx context.Context, in *PaperDocsListContinueInput) (out *PaperDocsListOutput, err error) {
body, err := c.call(ctx, "/paper/docs/list/continue", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
const (
// ExportFormatHTML indicates that we want to download a Dropbox Paper file
// as HTML.
ExportFormatHTML = "html"
// ExportFormatMarkdown indicates that we want to download a Dropbox Paper
// file as Markdown.
ExportFormatMarkdown = "markdown"
)
// PaperDownloadInput is the request format for downloading a Dropbox paper,
// including the paper's ID and the format we want to download it in.
type PaperDownloadInput struct {
DocID string `json:"doc_id"`
ExportFormat string `json:"export_format"`
}
// Download a Dropbox Paper.
func (c *Paper) Download(ctx context.Context, in *PaperDownloadInput) (out *DownloadOutput, err error) {
body, l, err := c.download(ctx, "api", "/paper/docs/download", in, nil)
if err != nil {
return
}
out = &DownloadOutput{body, l}
return
}
// PaperGetFolderInfoInput is the request payload format for
// /paper/docs/get_folder_info requests, indicating the ID of the Dropbox Paper
// document that the caller wants to get folder information for.
type PaperGetFolderInfoInput struct {
DocID string `json:"doc_id"`
}
// PaperGetFolderInfoOutput is the response format for
// /paper/docs/get_folder_info requests, containing information on which
// folders the requested Dropbox Paper is a part of.
type PaperGetFolderInfoOutput struct {
FolderSharingPolicyType PaperFolderSharingPolicyType `json:"folder_sharing_policy_type,omitempty"`
Folders []PaperFolder `json:"folders,omitempty"`
}
// PaperFolderSharingPolicyType is the folder sharing policy for the folder
// that contains the requested Drobox Paper; can be either "team or
// "invite_only".
type PaperFolderSharingPolicyType struct {
Tag string `json:".tag"`
}
// PaperFolder contains the ID and display name of a folder containing a
// Dropbox Paper.
type PaperFolder struct {
ID string `json:"id"`
Name string `json:"name"`
}
// GetFolderInfo returns information on which folders the requested Dropbox
// Paper is part of.
func (c *Paper) GetFolderInfo(ctx context.Context, in *PaperGetFolderInfoInput) (out *PaperGetFolderInfoOutput, err error) {
body, err := c.call(ctx, "/paper/docs/get_folder_info", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// PaperCreateInput is the Dropbox-API-Arg JSON format for providing options on
// the format, and the parent folder ID if passed in, of the Dropbox Paper
// being created.
type PaperCreateInput struct {
ImportFormat string `json:"import_format"`
ParentFolderID string `json:"parent_folder_id,omitempty"`
Reader io.Reader `json:"-"`
}
// PaperCreateOutput is the returned response format for a successful request
// to the /paper/create endpoint.
type PaperCreateOutput struct {
DocID string `json:"doc_id"`
Revision int64 `json:"revision"`
Title string `json:"title"`
}
// Create creates a Dropbox Paper file on a user's Dropbox Paper.
func (c *Paper) Create(ctx context.Context, in *PaperCreateInput) (out *PaperCreateOutput, err error) {
body, _, err := c.download(ctx, "api", "/paper/docs/create", in, in.Reader)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// PaperPermanentlyDeleteInput request input.
type PaperPermanentlyDeleteInput struct {
DocID string `json:"doc_id"`
}
// PermanentlyDelete a file or folder and its contents.
func (c *Paper) PermanentlyDelete(ctx context.Context, in *PaperPermanentlyDeleteInput) (err error) {
body, err := c.call(ctx, "/paper/docs/permanently_delete", in)
if err != nil {
return
}
defer body.Close()
return
}
// PaperGetMetadataInput is the request payload format for the alpha
// /paper/docs/get_metadata requests, indicating the ID of the Dropbox Paper
// document that the caller wants to get metadata for.
type PaperGetMetadataInput struct {
DocID string `json:"doc_id"`
}
// PaperGetMetadataOutput is the response format for the alpha
// /paper/docs/get_metadata endpoint, containing metadata on a Dropbox Paper.
type PaperGetMetadataOutput struct {
DocID string `json:"doc_id"`
Owner string `json:"owner"`
Title string `json:"title"`
CreatedDate time.Time `json:"created_date"`
Status PaperDocStatus `json:"status"`
Revision int64 `json:"revision"`
LastUpdatedDate time.Time `json:"last_updated_date"`
LastEditor string `json:"last_editor"`
}
// PaperDocStatus contains information about whether a Dropbox Paper is active
// or deleted.
type PaperDocStatus struct {
Tag string `json:".tag"`
}
// AlphaGetMetadata returns metadata for the requested file. Note that this
// is an currently an alpha endpoint, and may disappear.
func (c *Paper) AlphaGetMetadata(ctx context.Context, in *PaperGetMetadataInput) (out *PaperGetMetadataOutput, err error) {
body, err := c.call(ctx, "/paper/docs/get_metadata", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}