Skip to content

Commit

Permalink
Add user-me-Method for bots (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mattia-m authored Jan 6, 2023
1 parent cf0c0e9 commit 8c1a857
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
14 changes: 14 additions & 0 deletions testdata/user_me.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"object": "user",
"id": "some_id",
"name": "John Doe",
"avatar_url": "some.url",
"type": "person",
"bot": {
"owner": {
"type": "workspace",
"workspace": true
},
"workspace_name": "John Doe's Workspace"
}
}
33 changes: 32 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (uID UserID) String() string {
type UserService interface {
Get(context.Context, UserID) (*User, error)
List(context.Context, *Pagination) (*UsersListResponse, error)
Me(context.Context) (*User, error)
}

type UserClient struct {
Expand Down Expand Up @@ -67,6 +68,28 @@ func (uc *UserClient) List(ctx context.Context, pagination *Pagination) (*UsersL
return &response, nil
}

// Me https://developers.notion.com/reference/get-self
func (uc *UserClient) Me(ctx context.Context) (*User, error) {
res, err := uc.apiClient.request(ctx, http.MethodGet, "users/me", nil, nil)
if err != nil {
return nil, err
}

defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()

var response User
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}

return &response, nil
}

type UserType string

type User struct {
Expand All @@ -83,7 +106,15 @@ type Person struct {
Email string `json:"email"`
}

type Bot struct{}
type Bot struct {
Owner Owner `json:"owner"`
WorkspaceName string `json:"workspace_name"`
}

type Owner struct {
Type string `json:"type"`
Workspace bool `json:"workspace"`
}

type UsersListResponse struct {
Object ObjectType `json:"object"`
Expand Down
45 changes: 45 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,49 @@ func TestUserClient(t *testing.T) {
})
}
})

t.Run("Me", func(t *testing.T) {
tests := []struct {
name string
filePath string
statusCode int
want *notionapi.User
wantErr bool
err error
}{
{
name: "returns me-user",
filePath: "testdata/user_me.json",
statusCode: http.StatusOK,
want: &notionapi.User{
Object: notionapi.ObjectTypeUser,
ID: "some_id",
Type: notionapi.UserTypePerson,
Name: "John Doe",
AvatarURL: "some.url",
Bot: &notionapi.Bot{Owner: notionapi.Owner{
Type: "workspace",
Workspace: true,
}, WorkspaceName: "John Doe's Workspace"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newMockedClient(t, tt.filePath, tt.statusCode)
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))

got, err := client.User.Me(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
}
})
}
})
}

0 comments on commit 8c1a857

Please sign in to comment.