From 1acbd997946daf586993362e8639087034309022 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Thu, 17 Apr 2014 22:57:27 -0700 Subject: [PATCH] Add UsersService.GetByID method. Note: GetByID uses the undocumented GitHub API endpoint /user/:id. Resolves #329 (assuming #332 is also merged). Closes #333 --- github/users.go | 19 +++++++++++++++++++ github/users_test.go | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/github/users.go b/github/users.go index d8c74e2..0312724 100644 --- a/github/users.go +++ b/github/users.go @@ -95,6 +95,25 @@ func (s *UsersService) Get(user string) (*User, *Response, error) { return uResp, resp, err } +// GetByID fetches a user. +// +// Note: GetByID uses the undocumented GitHub API endpoint /user/:id. +func (s *UsersService) GetByID(id int) (*User, *Response, error) { + u := fmt.Sprintf("user/%d", id) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + user := new(User) + resp, err := s.client.Do(req, user) + if err != nil { + return nil, resp, err + } + + return user, resp, err +} + // Edit the authenticated user. // // GitHub API docs: http://developer.github.com/v3/users/#update-the-authenticated-user diff --git a/github/users_test.go b/github/users_test.go index c66ec22..0afc844 100644 --- a/github/users_test.go +++ b/github/users_test.go @@ -100,6 +100,26 @@ func TestUsersService_Get_invalidUser(t *testing.T) { testURLParseError(t, err) } +func TestUsersService_GetByID(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/user/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"id":1}`) + }) + + user, _, err := client.Users.GetByID(1) + if err != nil { + t.Errorf("Users.GetByID returned error: %v", err) + } + + want := &User{ID: Int(1)} + if !reflect.DeepEqual(user, want) { + t.Errorf("Users.GetByID returned %+v, want %+v", user, want) + } +} + func TestUsersService_Edit(t *testing.T) { setup() defer teardown()