Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding GetCurrentUser method #199

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ type GetUserOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}

// GetCurrentUserOptions is the data structure used when calling the GetCurrentUser API endpoint.
type GetCurrentUserOptions struct {
Includes [] string `url:"include,omitempty,brackets"`
}

// ListUsers lists users of your PagerDuty account, optionally filtered by a search query.
func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
v, err := query.Values(o)
Expand Down Expand Up @@ -123,6 +128,16 @@ func (c *Client) UpdateUser(u User) (*User, error) {
return getUserFromResponse(c, resp, err)
}

// GetCurrentUser gets details about the authenticated user when using a user-level API key or OAuth token
func (c *Client) GetCurrentUser(o GetCurrentUserOptions) (*User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/users/me?" + v.Encode())
return getUserFromResponse(c, resp, err)
}

func getUserFromResponse(c *Client, resp *http.Response, err error) (*User, error) {
if err != nil {
return nil, err
Expand Down
29 changes: 29 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ func TestUser_Update(t *testing.T) {
testEqual(t, want, res)
}

// Get Current User
func TestUser_GetCurrent(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/users/me", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"user": {"id": "1", "email":"foo@bar.com"}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}
opts := GetCurrentUserOptions{
Includes: []string{},
}
res, err := client.GetCurrentUser(opts)

want := &User{
APIObject: APIObject{
ID: "1",
},
Email: "foo@bar.com",
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

// List User Contactmethods
func TestUser_ListContactMethods(t *testing.T) {
setup()
Expand Down