-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
121 lines (100 loc) · 2.83 KB
/
api.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
package notion
import (
"context"
"net/http"
"github.com/mkfsn/notion-go/rest"
)
const (
APIBaseURL = "https://api.notion.com"
APIUsersListEndpoint = "/v1/users"
APIUsersRetrieveEndpoint = "/v1/users/{user_id}"
APIBlocksListChildrenEndpoint = "/v1/blocks/{block_id}/children"
APIBlocksAppendChildrenEndpoint = "/v1/blocks/{block_id}/children"
APIPagesCreateEndpoint = "/v1/pages"
APIPagesRetrieveEndpoint = "/v1/pages/{page_id}"
APIPagesUpdateEndpoint = "/v1/pages/{page_id}"
APIDatabasesListEndpoint = "/v1/databases"
APIDatabasesRetrieveEndpoint = "/v1/databases/{database_id}"
APIDatabasesQueryEndpoint = "/v1/databases/{database_id}/query"
APISearchEndpoint = "/v1/search"
)
const (
DefaultNotionVersion = "2021-05-13"
DefaultUserAgent = "mkfsn/notion-go"
)
var (
defaultSettings = apiSettings{
baseURL: APIBaseURL,
notionVersion: DefaultNotionVersion,
userAgent: DefaultUserAgent,
httpClient: http.DefaultClient,
}
)
type API struct {
searchClient SearchInterface
usersClient UsersInterface
databasesClient DatabasesInterface
pagesClient PagesInterface
blocksClient BlocksInterface
}
func New(authToken string, setters ...APISetting) *API {
settings := defaultSettings
for _, setter := range setters {
setter(&settings)
}
restClient := rest.New().
BearerToken(authToken).
BaseURL(settings.baseURL).
UserAgent(settings.userAgent).
Client(settings.httpClient).
Header("Notion-Version", settings.notionVersion)
return &API{
searchClient: newSearchClient(restClient),
usersClient: newUsersClient(restClient),
databasesClient: newDatabasesClient(restClient),
pagesClient: newPagesClient(restClient),
blocksClient: newBlocksClient(restClient),
}
}
func (c *API) Users() UsersInterface {
return c.usersClient
}
func (c *API) Databases() DatabasesInterface {
return c.databasesClient
}
func (c *API) Pages() PagesInterface {
return c.pagesClient
}
func (c *API) Blocks() BlocksInterface {
return c.blocksClient
}
func (c *API) Search(ctx context.Context, params SearchParameters) (*SearchResponse, error) {
return c.searchClient.Search(ctx, params)
}
type apiSettings struct {
baseURL string
notionVersion string
userAgent string
httpClient *http.Client
}
type APISetting func(o *apiSettings)
func WithBaseURL(baseURL string) APISetting {
return func(o *apiSettings) {
o.baseURL = baseURL
}
}
func WithNotionVersion(notionVersion string) APISetting {
return func(o *apiSettings) {
o.notionVersion = notionVersion
}
}
func WithUserAgent(userAgent string) APISetting {
return func(o *apiSettings) {
o.userAgent = userAgent
}
}
func WithHTTPClient(httpClient *http.Client) APISetting {
return func(o *apiSettings) {
o.httpClient = httpClient
}
}