forked from jomei/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
80 lines (64 loc) · 1.72 KB
/
user.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
package notionapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type UserID string
func (uID UserID) String() string {
return string(uID)
}
type UserService interface {
Get(context.Context, UserID) (*User, error)
List(context.Context, *Pagination) (*UsersListResponse, error)
}
type UserClient struct {
apiClient *Client
}
// Get https://developers.notion.com/reference/get-user
func (uc *UserClient) Get(ctx context.Context, id UserID) (*User, error) {
res, err := uc.apiClient.request(ctx, http.MethodGet, fmt.Sprintf("users/%s", id.String()), nil, nil)
if err != nil {
return nil, err
}
var response User
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// List https://developers.notion.com/reference/get-users
func (uc *UserClient) List(ctx context.Context, pagination *Pagination) (*UsersListResponse, error) {
res, err := uc.apiClient.request(ctx, http.MethodGet, "users", pagination.ToQuery(), nil)
if err != nil {
return nil, err
}
var response UsersListResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
type UserType string
type User struct {
Object ObjectType `json:"object"`
ID UserID `json:"id"`
Type UserType `json:"type"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
Person *Person `json:"person"`
Bot *Bot `json:"bot"`
}
type Person struct {
Email string `json:"email"`
}
type Bot struct{}
type UsersListResponse struct {
Object ObjectType `json:"object"`
Results []User `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}