forked from jomei/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
128 lines (108 loc) · 3.42 KB
/
database.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
package notionapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type DatabaseID string
func (dID DatabaseID) String() string {
return string(dID)
}
type DatabaseService interface {
Get(context.Context, DatabaseID) (*Database, error)
List(context.Context, *Pagination) (*DatabaseListResponse, error)
Query(context.Context, DatabaseID, *DatabaseQueryRequest) (*DatabaseQueryResponse, error)
}
type DatabaseClient struct {
apiClient *Client
}
// Get https://developers.notion.com/reference/get-database
func (dc *DatabaseClient) Get(ctx context.Context, id DatabaseID) (*Database, error) {
res, err := dc.apiClient.request(ctx, http.MethodGet, fmt.Sprintf("databases/%s", id.String()), nil, nil)
if err != nil {
return nil, err
}
var response Database
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// List https://developers.notion.com/reference/get-databases
func (dc *DatabaseClient) List(ctx context.Context, pagination *Pagination) (*DatabaseListResponse, error) {
res, err := dc.apiClient.request(ctx, http.MethodGet, "databases", pagination.ToQuery(), nil)
if err != nil {
return nil, err
}
var response DatabaseListResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// Query https://developers.notion.com/reference/post-database-query
func (dc *DatabaseClient) Query(ctx context.Context, id DatabaseID, requestBody *DatabaseQueryRequest) (*DatabaseQueryResponse, error) {
res, err := dc.apiClient.request(ctx, http.MethodPost, fmt.Sprintf("databases/%s/query", id.String()), nil, requestBody)
if err != nil {
return nil, err
}
var response DatabaseQueryResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
type Database struct {
Object ObjectType `json:"object"`
ID ObjectID `json:"id"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
Title Paragraph `json:"title"`
Properties Properties `json:"properties"`
}
func (db *Database) GetObject() ObjectType {
return db.Object
}
type DatabaseListResponse struct {
Object ObjectType `json:"object"`
Results []Database `json:"results"`
NextCursor string `json:"next_cursor"`
HasMore bool `json:"has_more"`
}
type DatabaseQueryRequest struct {
PropertyFilter *PropertyFilter
CompoundFilter *CompoundFilter
Sorts []SortObject `json:"sorts,omitempty"`
StartCursor Cursor `json:"start_cursor,omitempty"`
PageSize int `json:"page_size,omitempty"`
}
func (qr *DatabaseQueryRequest) MarshalJSON() ([]byte, error) {
var filter interface{}
if qr.PropertyFilter != nil {
filter = qr.PropertyFilter
} else if qr.CompoundFilter != nil{
filter = qr.CompoundFilter
}
return json.Marshal(struct {
Sorts []SortObject `json:"sorts,omitempty"`
StartCursor Cursor `json:"start_cursor,omitempty"`
PageSize int `json:"page_size,omitempty"`
Filter interface{} `json:"filter,omitempty"`
}{
Sorts: qr.Sorts,
StartCursor: qr.StartCursor,
PageSize: qr.PageSize,
Filter: filter,
})
}
type DatabaseQueryResponse struct {
Object ObjectType `json:"object"`
Results []Page `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}