mirror of
https://github.com/dstotijn/go-notion.git
synced 2025-06-08 23:46:12 +02:00
Add "list users" endpoint support
This commit is contained in:
parent
9b807dd473
commit
874858ea2a
@ -27,7 +27,7 @@ Go client for the [Notion API](https://developers.notion.com/reference).
|
|||||||
### Users
|
### Users
|
||||||
|
|
||||||
- [x] [Retrieve a user](client.go)
|
- [x] [Retrieve a user](client.go)
|
||||||
- [ ] List all users
|
- [x] [List all users](client.go)
|
||||||
|
|
||||||
### Search
|
### Search
|
||||||
|
|
||||||
|
2
block.go
2
block.go
@ -56,7 +56,7 @@ const (
|
|||||||
BlockTypeUnsupported BlockType = "unsupported"
|
BlockTypeUnsupported BlockType = "unsupported"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FindBlockChildrenQuery struct {
|
type PaginationQuery struct {
|
||||||
StartCursor string
|
StartCursor string
|
||||||
PageSize int
|
PageSize int
|
||||||
}
|
}
|
||||||
|
39
client.go
39
client.go
@ -225,7 +225,7 @@ func (c *Client) UpdatePageProps(ctx context.Context, pageID string, params Upda
|
|||||||
|
|
||||||
// FindBlockChildrenByID returns a list of block children for a given block ID.
|
// FindBlockChildrenByID returns a list of block children for a given block ID.
|
||||||
// See: https://developers.notion.com/reference/post-database-query
|
// See: https://developers.notion.com/reference/post-database-query
|
||||||
func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, query *FindBlockChildrenQuery) (result BlockChildrenResponse, err error) {
|
func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, query *PaginationQuery) (result BlockChildrenResponse, err error) {
|
||||||
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/blocks/%v/children", blockID), nil)
|
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/blocks/%v/children", blockID), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return BlockChildrenResponse{}, fmt.Errorf("notion: invalid request: %w", err)
|
return BlockChildrenResponse{}, fmt.Errorf("notion: invalid request: %w", err)
|
||||||
@ -323,3 +323,40 @@ func (c *Client) FindUserByID(ctx context.Context, id string) (user User, err er
|
|||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListUsers returns a list of all users, and pagination metadata.
|
||||||
|
// See: https://developers.notion.com/reference/get-users
|
||||||
|
func (c *Client) ListUsers(ctx context.Context, query *PaginationQuery) (result ListUsersResponse, err error) {
|
||||||
|
req, err := c.newRequest(ctx, http.MethodGet, "/users", nil)
|
||||||
|
if err != nil {
|
||||||
|
return ListUsersResponse{}, fmt.Errorf("notion: invalid request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if query != nil {
|
||||||
|
q := url.Values{}
|
||||||
|
if query.StartCursor != "" {
|
||||||
|
q.Set("start_cursor", query.StartCursor)
|
||||||
|
}
|
||||||
|
if query.PageSize != 0 {
|
||||||
|
q.Set("page_size", strconv.Itoa(query.PageSize))
|
||||||
|
}
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := c.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return ListUsersResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
return ListUsersResponse{}, fmt.Errorf("notion: failed to list users: %w", parseErrorResponse(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewDecoder(res.Body).Decode(&result)
|
||||||
|
if err != nil {
|
||||||
|
return ListUsersResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
7
user.go
7
user.go
@ -15,3 +15,10 @@ type User struct {
|
|||||||
Person *Person `json:"person"`
|
Person *Person `json:"person"`
|
||||||
Bot *Bot `json:"bot"`
|
Bot *Bot `json:"bot"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListUsersResponse contains results (users) and pagination data returned from a list request.
|
||||||
|
type ListUsersResponse struct {
|
||||||
|
Results []User `json:"results"`
|
||||||
|
HasMore bool `json:"has_more"`
|
||||||
|
NextCursor *string `json:"next_cursor"`
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user