-
Notifications
You must be signed in to change notification settings - Fork 2
/
documents.go
235 lines (191 loc) · 7.32 KB
/
documents.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
package outline
import (
"context"
"fmt"
"github.com/ioki-mobility/go-outline/internal/common"
"github.com/rsjethani/rsling"
)
// DocumentsClient exposes CRUD operations around the documents resource.
type DocumentsClient struct {
sl *rsling.Sling
}
// newDocumentsClient creates a new instance of DocumentsClient.
func newDocumentsClient(sl *rsling.Sling) *DocumentsClient {
return &DocumentsClient{sl: sl}
}
// Get returns a client for retrieving a single document.
func (cl *DocumentsClient) Get() *DocumentsClientGet {
params := documentsGetParams{}
return &DocumentsClientGet{sl: cl.sl, params: params}
}
// GetAll returns a client for retrieving multiple documents at once.
func (cl *DocumentsClient) GetAll() *DocumentsClientGetAll {
return nil
}
// Create returns a client for creating a single document in the specified collection.
// API reference: https://www.getoutline.com/developers#tag/Documents/paths/~1documents.create/post
func (cl *DocumentsClient) Create(title string, id CollectionID) *DocumentsCreateClient {
return newDocumentsCreateClient(cl.sl, title, id)
}
// Update returns a client for updating a single document in the specified collection.
// API reference: https://www.getoutline.com/developers#tag/Documents/paths/~1documents.update/post
func (cl *DocumentsClient) Update(id DocumentID) *DocumentsUpdateClient {
return newDocumentsUpdateClient(cl.sl, id)
}
// documentsCreateParams represents the Outline Documents.create parameters
type documentsGetParams struct {
DocumentId DocumentID `json:"id,omitempty"`
ShareId DocumentShareID `json:"shareId,omitempty"`
}
// DocumentsClientGet gets a single document.
type DocumentsClientGet struct {
sl *rsling.Sling
params documentsGetParams
}
// ByID configures that document be retrieved by its id.
func (cl *DocumentsClientGet) ByID(id DocumentID) *DocumentsClientGet {
cl.params.DocumentId = id
return cl
}
// ByShareID configures that document be retrieved by its share id.
func (cl *DocumentsClientGet) ByShareID(id DocumentShareID) *DocumentsClientGet {
cl.params.ShareId = id
return cl
}
// Do makes the actual request and returns the document.
func (cl *DocumentsClientGet) Do(ctx context.Context) (*Document, error) {
cl.sl.Post(common.DocumentsGetEndpoint()).BodyJSON(&cl.params)
success := &struct {
Data *Document `json:"data"`
}{}
br, err := request(ctx, cl.sl, success)
if err != nil {
return nil, fmt.Errorf("failed making HTTP request: %w", err)
}
if br != nil {
return nil, fmt.Errorf("bad response: %w", &apiError{br: *br})
}
return success.Data, nil
}
// DocumentsClientGetAll can be used to retrieve more than one document. Use available configuration options to select
// the documents you want to retrieve then finally call [DocumentsClientGetAll.Do].
type DocumentsClientGetAll struct{}
// Collection selects documents belonging to the collection identified by id.
func (cl *DocumentsClientGetAll) Collection(id CollectionID) *DocumentsClientGetAll { return nil }
// Parent selects documents having the parent document identified by id.
func (cl *DocumentsClientGetAll) Parent(id DocumentID) *DocumentsClientGetAll { return nil }
// Do makes the actual request and retrieves selected documents. The user provided callback fn is called for every such
// document. If there is any error during the process then fn is given the error so that it can decide whether to
// continue or not. The callback can return false in case it wants to abort getting documents.
func (cl *DocumentsClientGetAll) Do(ctx context.Context, fn func(*Document, error) bool) error {
return nil
}
// documentsCreateParams represents the Outline Documents.create parameters
type documentsCreateParams struct {
CollectionID CollectionID `json:"collectionId"`
ParentDocumentId DocumentID `json:"parentDocumentId,omitempty"`
Publish bool `json:"publish,omitempty"`
Template bool `json:"template,omitempty"`
TemplateID TemplateID `json:"templateId,omitempty"`
Text string `json:"text,omitempty"`
Title string `json:"title"`
}
// DocumentsCreateClient is a client for creating a single document.
type DocumentsCreateClient struct {
sl *rsling.Sling
params documentsCreateParams
}
func newDocumentsCreateClient(sl *rsling.Sling, title string, id CollectionID) *DocumentsCreateClient {
copy := sl.New()
params := documentsCreateParams{Title: title, CollectionID: id}
return &DocumentsCreateClient{sl: copy, params: params}
}
func (cl *DocumentsCreateClient) Publish(publish bool) *DocumentsCreateClient {
cl.params.Publish = publish
return cl
}
func (cl *DocumentsCreateClient) Text(text string) *DocumentsCreateClient {
cl.params.Text = text
return cl
}
func (cl *DocumentsCreateClient) ParentDocumentID(id DocumentID) *DocumentsCreateClient {
cl.params.ParentDocumentId = id
return cl
}
func (cl *DocumentsCreateClient) TemplateID(id TemplateID) *DocumentsCreateClient {
cl.params.TemplateID = id
return cl
}
func (cl *DocumentsCreateClient) Template(template bool) *DocumentsCreateClient {
cl.params.Template = template
return cl
}
// Do makes the actual request to create a document.
func (cl *DocumentsCreateClient) Do(ctx context.Context) (*Document, error) {
cl.sl.Post(common.DocumentsCreateEndpoint()).BodyJSON(&cl.params)
success := &struct {
Data *Document `json:"data"`
}{}
br, err := request(ctx, cl.sl, success)
if err != nil {
return nil, fmt.Errorf("failed making HTTP request: %w", err)
}
if br != nil {
return nil, fmt.Errorf("bad response: %w", &apiError{br: *br})
}
return success.Data, nil
}
// documentsUpdateParams represents the Outline Documents.update parameters
type documentsUpdateParams struct {
Id DocumentID `json:"id"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
Append bool `json:"append,omitempty"`
Publish bool `json:"publish,omitempty"`
Done bool `json:"done,omitempty"`
}
// DocumentsUpdateClient is a client for updating a single document.
type DocumentsUpdateClient struct {
sl *rsling.Sling
params documentsUpdateParams
}
func newDocumentsUpdateClient(sl *rsling.Sling, id DocumentID) *DocumentsUpdateClient {
copy := sl.New()
params := documentsUpdateParams{Id: id}
return &DocumentsUpdateClient{sl: copy, params: params}
}
func (cl *DocumentsUpdateClient) Title(title string) *DocumentsUpdateClient {
cl.params.Title = title
return cl
}
func (cl *DocumentsUpdateClient) Text(text string) *DocumentsUpdateClient {
cl.params.Text = text
return cl
}
func (cl *DocumentsUpdateClient) Publish(publish bool) *DocumentsUpdateClient {
cl.params.Publish = publish
return cl
}
func (cl *DocumentsUpdateClient) Append(append bool) *DocumentsUpdateClient {
cl.params.Append = append
return cl
}
func (cl *DocumentsUpdateClient) Done(done bool) *DocumentsUpdateClient {
cl.params.Done = done
return cl
}
// Do makes the actual request to update a document.
func (cl *DocumentsUpdateClient) Do(ctx context.Context) (*Document, error) {
cl.sl.Post(common.DocumentsUpdateEndpoint()).BodyJSON(&cl.params)
success := &struct {
Data *Document `json:"data"`
}{}
br, err := request(ctx, cl.sl, success)
if err != nil {
return nil, fmt.Errorf("failed making HTTP request: %w", err)
}
if br != nil {
return nil, fmt.Errorf("bad response: %w", &apiError{br: *br})
}
return success.Data, nil
}