forked from kyleconroy/paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper.go
193 lines (159 loc) · 5.03 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
package paper
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func NewClient(token string) *APIClient {
return &APIClient{
Token: token,
HTTP: http.Client{},
}
}
type Client interface {
ListDocs(context.Context, *ListPaperDocsArgs) (*ListPaperDocsResponse, error)
DownloadDoc(context.Context, *PaperDocExport) (*PaperDocExportResult, []byte, error)
GetDocFolderInfo(context.Context, *RefPaperDoc) (*FoldersContainingPaperDoc, error)
}
type APIClient struct {
Token string
HTTP http.Client
}
type APIError struct {
Summary string `json:"error_summary"`
Metadata map[string]string `json:"error"`
}
func (e APIError) Error() string {
return fmt.Sprintf("%s: %q", e.Summary, e.Metadata)
}
func (c *APIClient) rpc(ctx context.Context, url string, in interface{}, out interface{}) error {
body, err := json.Marshal(in)
if err != nil {
return err
}
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTP.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var apierr APIError
if err := json.NewDecoder(resp.Body).Decode(&apierr); err != nil {
return err
}
return apierr
}
return json.NewDecoder(resp.Body).Decode(out)
}
func (c *APIClient) content(ctx context.Context, url string, in interface{}, out interface{}) ([]byte, error) {
var contents []byte
body, err := json.Marshal(in)
if err != nil {
return contents, err
}
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("Dropbox-API-Arg", string(body))
resp, err := c.HTTP.Do(req.WithContext(ctx))
if err != nil {
return contents, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var apierr APIError
if err := json.NewDecoder(resp.Body).Decode(&apierr); err != nil {
return contents, err
}
return contents, apierr
}
if result := resp.Header.Get("Dropbox-API-Result"); result != "" {
if err := json.Unmarshal([]byte(result), out); err != nil {
return contents, err
}
}
// fmt.Println("header: %q", resp.Header)
return ioutil.ReadAll(resp.Body)
}
type ListPaperDocsFilterBy string
const (
ListPaperDocsFilterByAccessed ListPaperDocsFilterBy = "accessed"
ListPaperDocsFilterByModified = "modified"
ListPaperDocsFilterByCreated = "created"
)
type ListPaperDocsSortBy string
const (
ListPaperDocsSortByAccessed ListPaperDocsSortBy = "accessed"
ListPaperDocsSortByModified = "modified"
ListPaperDocsSortByCreated = "created"
)
type ListPaperDocsSortOrder string
const (
ListPaperDocsSortOrderAsc ListPaperDocsSortOrder = "ascending"
ListPaperDocsSortOrderDesc = "descending"
)
type ListPaperDocsArgs struct {
FilterBy ListPaperDocsFilterBy `json:"filter_by,omitempty"`
SortBy ListPaperDocsSortBy `json:"sort_by,omitempty"`
SortOrder ListPaperDocsSortOrder `json:"sort_order,omitempty"`
Limit int32 `json:"limit,omitempty"`
}
type Cursor struct {
Value string `json:"value"`
Expiration string `json:"expiration"` // TODO: Make a time.Time
}
type ListPaperDocsResponse struct {
DocIDs []string `json:"doc_ids"`
Cursor Cursor `json:"cursor"`
HasMore bool `json"has_more"`
}
func (c *APIClient) ListDocs(ctx context.Context, in *ListPaperDocsArgs) (*ListPaperDocsResponse, error) {
var out ListPaperDocsResponse
return &out, c.rpc(ctx, "https://api.dropboxapi.com/2/paper/docs/list", in, &out)
}
type ExportFormat string
const (
ExportFormatMarkdown ExportFormat = "markdown"
ExportFormatHTML ExportFormat = "html"
)
type PaperDocExport struct {
DocID string `json:"doc_id,omitempty"`
Format ExportFormat `json:"export_format,omitempty"`
}
type PaperDocExportResult struct {
Owner string `json:"owner"`
Title string `json:"title"`
Revision int64 `json:"revision"`
MIME string `json:"mime_type"`
}
func (c *APIClient) DownloadDoc(ctx context.Context, in *PaperDocExport) (*PaperDocExportResult, []byte, error) {
var out PaperDocExportResult
blob, err := c.content(ctx, "https://api.dropboxapi.com/2/paper/docs/download", in, &out)
return &out, blob, err
}
type RefPaperDoc struct {
DocID string `json:"doc_id"`
}
type Folder struct {
ID string `json:"id"`
Name string `json:"name"`
}
type FolderSharingPolicyType string
const (
FolderSharingPolicyTeam FolderSharingPolicyType = "team"
FolderSharingPolicyInviteOnly = "invite_only"
)
type FoldersContainingPaperDoc struct {
FolderSharingPolicyType FolderSharingPolicyType
Folders []Folder
}
func (c *APIClient) GetDocFolderInfo(ctx context.Context, in *RefPaperDoc) (*FoldersContainingPaperDoc, error) {
var out FoldersContainingPaperDoc
return &out, c.rpc(ctx, "https://api.dropboxapi.com/2/paper/docs/get_folder_info", in, &out)
}
var _ Client = &APIClient{}