-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
156 lines (116 loc) · 3.04 KB
/
users.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
package notion
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/mkfsn/notion-go/rest"
)
type User interface {
isUser()
}
type baseUser struct {
Object ObjectType `json:"object"`
ID string `json:"id"`
Type UserType `json:"type"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
}
func (b baseUser) isUser() {}
type Person struct {
Email string `json:"email"`
}
type PersonUser struct {
baseUser
Person Person `json:"person"`
}
type Bot struct{}
type BotUser struct {
baseUser
Bot Bot `json:"bot"`
}
type UsersRetrieveParameters struct {
UserID string `json:"-" url:"-"`
}
type UsersRetrieveResponse struct {
User
}
func (u *UsersRetrieveResponse) UnmarshalJSON(data []byte) (err error) {
var decoder userDecoder
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal UsersRetrieveResponse: %w", err)
}
u.User = decoder.User
return nil
}
type UsersListParameters struct {
PaginationParameters
}
type UsersListResponse struct {
PaginatedList
Results []User `json:"results"`
}
func (u *UsersListResponse) UnmarshalJSON(data []byte) error {
type Alias UsersListResponse
alias := struct {
*Alias
Results []userDecoder `json:"results"`
}{
Alias: (*Alias)(u),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal UsersListResponse: %w", err)
}
u.Results = make([]User, 0, len(alias.Results))
for _, decoder := range alias.Results {
u.Results = append(u.Results, decoder.User)
}
return nil
}
type UsersInterface interface {
Retrieve(ctx context.Context, params UsersRetrieveParameters) (*UsersRetrieveResponse, error)
List(ctx context.Context, params UsersListParameters) (*UsersListResponse, error)
}
type usersClient struct {
restClient rest.Interface
}
func newUsersClient(restClient rest.Interface) *usersClient {
return &usersClient{
restClient: restClient,
}
}
func (u *usersClient) Retrieve(ctx context.Context, params UsersRetrieveParameters) (*UsersRetrieveResponse, error) {
var result UsersRetrieveResponse
var failure HTTPError
err := u.restClient.New().Get().
Endpoint(strings.Replace(APIUsersRetrieveEndpoint, "{user_id}", params.UserID, 1)).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
func (u *usersClient) List(ctx context.Context, params UsersListParameters) (*UsersListResponse, error) {
var result UsersListResponse
var failure HTTPError
err := u.restClient.New().Get().
Endpoint(APIUsersListEndpoint).
QueryStruct(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
type userDecoder struct {
User
}
func (u *userDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Type UserType `json:"type"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal User: %w", err)
}
switch decoder.Type {
case UserTypePerson:
u.User = &PersonUser{}
case UserTypeBot:
u.User = &BotUser{}
}
return json.Unmarshal(data, u.User)
}