-
Notifications
You must be signed in to change notification settings - Fork 1
/
pages.go
573 lines (435 loc) · 12.3 KB
/
pages.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package notion
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/mkfsn/notion-go/rest"
)
type Parent interface {
isParent()
}
type baseParent struct {
Type ParentType `json:"type"`
}
func (b baseParent) isParent() {}
type DatabaseParent struct {
baseParent
DatabaseID string `json:"database_id"`
}
type PageParent struct {
baseParent
PageID string `json:"page_id"`
}
type WorkspaceParent struct {
baseParent
}
type ParentInput interface {
isParentInput()
}
type baseParentInput struct{}
func (b baseParentInput) isParentInput() {}
type DatabaseParentInput struct {
baseParentInput
DatabaseID string `json:"database_id"`
}
type PageParentInput struct {
baseParentInput
PageID string `json:"page_id"`
}
type Page struct {
// Always "page".
Object ObjectType `json:"object"`
// Unique identifier of the page.
ID string `json:"id"`
// The page's parent
Parent Parent `json:"parent"`
// Property values of this page.
Properties map[string]PropertyValue `json:"properties"`
// Date and time when this page was created. Formatted as an ISO 8601 date time string.
CreatedTime time.Time `json:"created_time"`
// Date and time when this page was updated. Formatted as an ISO 8601 date time string.
LastEditedTime time.Time `json:"last_edited_time"`
// The archived status of the page.
Archived bool `json:"archived"`
}
func (p *Page) UnmarshalJSON(data []byte) error {
type Alias Page
alias := struct {
*Alias
Parent parentDecoder `json:"parent"`
Properties map[string]propertyValueDecoder `json:"properties"`
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal Page: %w", err)
}
p.Parent = alias.Parent.Parent
p.Properties = make(map[string]PropertyValue)
for name, decoder := range alias.Properties {
p.Properties[name] = decoder.PropertyValue
}
return nil
}
func (p Page) isSearchable() {}
type PropertyValue interface {
isPropertyValue()
}
type basePropertyValue struct {
// Underlying identifier for the property. This identifier is guaranteed to remain constant when the property name changes.
// It may be a UUID, but is often a short random string.
// The id may be used in place of name when creating or updating pages.
ID string `json:"id,omitempty"`
// Type of the property
Type PropertyValueType `json:"type,omitempty"`
}
func (p basePropertyValue) isPropertyValue() {}
type TitlePropertyValue struct {
basePropertyValue
Title []RichText `json:"title"`
}
func (t *TitlePropertyValue) UnmarshalJSON(data []byte) error {
type Alias TitlePropertyValue
alias := struct {
*Alias
Title []richTextDecoder `json:"title"`
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal TitlePropertyValue: %w", err)
}
t.Title = make([]RichText, 0, len(alias.Title))
for _, decoder := range alias.Title {
t.Title = append(t.Title, decoder.RichText)
}
return nil
}
type RichTextPropertyValue struct {
basePropertyValue
RichText []RichText `json:"rich_text"`
}
func (r *RichTextPropertyValue) UnmarshalJSON(data []byte) error {
type Alias RichTextPropertyValue
alias := struct {
*Alias
RichText []richTextDecoder `json:"rich_text"`
}{
Alias: (*Alias)(r),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal RichTextPropertyValue: %w", err)
}
r.RichText = make([]RichText, 0, len(alias.RichText))
for _, decoder := range alias.RichText {
r.RichText = append(r.RichText, decoder.RichText)
}
return nil
}
type NumberPropertyValue struct {
basePropertyValue
Number float64 `json:"number"`
}
type SelectPropertyValueOption struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Color Color `json:"color,omitempty"`
}
type SelectPropertyValue struct {
basePropertyValue
Select SelectPropertyValueOption `json:"select"`
}
type MultiSelectPropertyValueOption struct {
ID string `json:"id"`
Name string `json:"name"`
Color Color `json:"color"`
}
type MultiSelectPropertyValue struct {
basePropertyValue
MultiSelect []MultiSelectPropertyValueOption `json:"multi_select"`
}
type Date struct {
Start string `json:"start"`
End *string `json:"end"`
}
type DatePropertyValue struct {
basePropertyValue
Date Date `json:"date"`
}
type FormulaValue interface {
isFormulaValue()
}
type baseFormulaValue struct {
Type FormulaValueType `json:"type"`
}
func (b baseFormulaValue) isFormulaValue() {}
type StringFormulaValue struct {
baseFormulaValue
String *string `json:"string"`
}
type NumberFormulaValue struct {
baseFormulaValue
Number *float64 `json:"number"`
}
type BooleanFormulaValue struct {
baseFormulaValue
Boolean bool `json:"boolean"`
}
type DateFormulaValue struct {
baseFormulaValue
Date DatePropertyValue `json:"date"`
}
type FormulaPropertyValue struct {
basePropertyValue
Formula FormulaValue `json:"formula"`
}
func (f *FormulaPropertyValue) UnmarshalJSON(data []byte) error {
type Alias FormulaPropertyValue
alias := struct {
*Alias
Formula formulaValueDecoder `json:"formula"`
}{
Alias: (*Alias)(f),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal FormulaPropertyValue: %w", err)
}
f.Formula = alias.Formula.FormulaValue
return nil
}
type PageReference struct {
ID string `json:"id"`
}
type RelationPropertyValue struct {
basePropertyValue
Relation []PageReference `json:"relation"`
}
type RollupValueType interface {
isRollupValueType()
}
type baseRollupValueType struct {
Type string `json:"type"`
}
func (b baseRollupValueType) isRollupValueType() {}
type NumberRollupValue struct {
baseRollupValueType
Number float64 `json:"number"`
}
type DateRollupValue struct {
baseRollupValueType
Date DatePropertyValue `json:"date"`
}
type ArrayRollupValue struct {
baseRollupValueType
Array []interface{} `json:"array"`
}
type RollupPropertyValue struct {
basePropertyValue
Rollup RollupValueType `json:"rollup"`
}
type PeoplePropertyValue struct {
basePropertyValue
People []User `json:"people"`
}
type File struct {
Name string `json:"name"`
}
type FilesPropertyValue struct {
basePropertyValue
Files []File `json:"files"`
}
type CheckboxPropertyValue struct {
basePropertyValue
Checkbox bool `json:"checkbox"`
}
type URLPropertyValue struct {
basePropertyValue
URL string `json:"url"`
}
type EmailPropertyValue struct {
basePropertyValue
Email string `json:"email"`
}
type PhoneNumberPropertyValue struct {
basePropertyValue
PhoneNumber string `json:"phone_number"`
}
type CreatedTimePropertyValue struct {
basePropertyValue
CreatedTime time.Time `json:"created_time"`
}
type CreatedByPropertyValue struct {
basePropertyValue
CreatedBy User `json:"created_by"`
}
type LastEditedTimePropertyValue struct {
basePropertyValue
LastEditedTime time.Time `json:"last_edited_time"`
}
type LastEditedByPropertyValue struct {
basePropertyValue
LastEditedBy User `json:"last_edited_by"`
}
type PagesRetrieveParameters struct {
PageID string `json:"-" url:"-"`
}
type PagesRetrieveResponse struct {
Page
}
type PagesUpdateParameters struct {
PageID string `json:"-" url:"-"`
Properties map[string]PropertyValue `json:"properties" url:"-"`
}
type PagesUpdateResponse struct {
Page
}
type PagesCreateParameters struct {
// A DatabaseParentInput or PageParentInput
Parent ParentInput `json:"parent" url:"-"`
// Property values of this page. The keys are the names or IDs of the property and the values are property values.
Properties map[string]PropertyValue `json:"properties" url:"-"`
// Page content for the new page as an array of block objects
Children []Block `json:"children,omitempty" url:"-"`
}
type PagesCreateResponse struct {
Page
}
type PagesInterface interface {
Retrieve(ctx context.Context, params PagesRetrieveParameters) (*PagesRetrieveResponse, error)
Update(ctx context.Context, params PagesUpdateParameters) (*PagesUpdateResponse, error)
Create(ctx context.Context, params PagesCreateParameters) (*PagesCreateResponse, error)
}
type pagesClient struct {
restClient rest.Interface
}
func newPagesClient(restClient rest.Interface) *pagesClient {
return &pagesClient{
restClient: restClient,
}
}
func (p *pagesClient) Retrieve(ctx context.Context, params PagesRetrieveParameters) (*PagesRetrieveResponse, error) {
var result PagesRetrieveResponse
var failure HTTPError
err := p.restClient.New().Get().
Endpoint(strings.Replace(APIPagesRetrieveEndpoint, "{page_id}", params.PageID, 1)).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
func (p *pagesClient) Update(ctx context.Context, params PagesUpdateParameters) (*PagesUpdateResponse, error) {
var result PagesUpdateResponse
var failure HTTPError
err := p.restClient.New().Patch().
Endpoint(strings.Replace(APIPagesUpdateEndpoint, "{page_id}", params.PageID, 1)).
QueryStruct(params).
BodyJSON(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
func (p *pagesClient) Create(ctx context.Context, params PagesCreateParameters) (*PagesCreateResponse, error) {
var result PagesCreateResponse
var failure HTTPError
err := p.restClient.New().Post().
Endpoint(APIPagesCreateEndpoint).
QueryStruct(params).
BodyJSON(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
type formulaValueDecoder struct {
FormulaValue
}
func (f *formulaValueDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Type FormulaValueType `json:"type"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal FormulaValue: %w", err)
}
switch decoder.Type {
case FormulaValueTypeString:
f.FormulaValue = &StringFormulaValue{}
case FormulaValueTypeNumber:
f.FormulaValue = &NumberFormulaValue{}
case FormulaValueTypeBoolean:
f.FormulaValue = &BooleanFormulaValue{}
case FormulaValueTypeDate:
f.FormulaValue = &DateFormulaValue{}
}
return json.Unmarshal(data, &f.FormulaValue)
}
type parentDecoder struct {
Parent
}
func (p *parentDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Type ParentType `json:"type"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal Parent: %w", err)
}
switch decoder.Type {
case ParentTypeDatabase:
p.Parent = &DatabaseParent{}
case ParentTypePage:
p.Parent = &PageParent{}
case ParentTypeWorkspace:
p.Parent = &WorkspaceParent{}
}
return json.Unmarshal(data, p.Parent)
}
type propertyValueDecoder struct {
PropertyValue
}
// UnmarshalJSON implements json.Unmarshaler
// nolint: cyclop
func (p *propertyValueDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Type PropertyValueType `json:"type,omitempty"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal PropertyValue: %w", err)
}
switch decoder.Type {
case PropertyValueTypeRichText:
p.PropertyValue = &RichTextPropertyValue{}
case PropertyValueTypeNumber:
p.PropertyValue = &NumberPropertyValue{}
case PropertyValueTypeSelect:
p.PropertyValue = &SelectPropertyValue{}
case PropertyValueTypeMultiSelect:
p.PropertyValue = &MultiSelectPropertyValue{}
case PropertyValueTypeDate:
p.PropertyValue = &DatePropertyValue{}
case PropertyValueTypeFormula:
p.PropertyValue = &FormulaPropertyValue{}
case PropertyValueTypeRelation:
p.PropertyValue = &RelationPropertyValue{}
case PropertyValueTypeRollup:
p.PropertyValue = &RollupPropertyValue{}
case PropertyValueTypeTitle:
p.PropertyValue = &TitlePropertyValue{}
case PropertyValueTypePeople:
p.PropertyValue = &PeoplePropertyValue{}
case PropertyValueTypeFiles:
p.PropertyValue = &FilesPropertyValue{}
case PropertyValueTypeCheckbox:
p.PropertyValue = &CheckboxPropertyValue{}
case PropertyValueTypeURL:
p.PropertyValue = &URLPropertyValue{}
case PropertyValueTypeEmail:
p.PropertyValue = &EmailPropertyValue{}
case PropertyValueTypePhoneNumber:
p.PropertyValue = &PhoneNumberPropertyValue{}
case PropertyValueTypeCreatedTime:
p.PropertyValue = &CreatedTimePropertyValue{}
case PropertyValueTypeCreatedBy:
p.PropertyValue = &CreatedByPropertyValue{}
case PropertyValueTypeLastEditedTime:
p.PropertyValue = &LastEditedTimePropertyValue{}
case PropertyValueTypeLastEditedBy:
p.PropertyValue = &LastEditedByPropertyValue{}
}
return json.Unmarshal(data, p.PropertyValue)
}