Browse Source

add support for basic User methods

includes Users.Get, Users.Edit, Users.List
Will Norris 13 years ago
parent
commit
d67731f509
2 changed files with 196 additions and 4 deletions
  1. +79
    -4
      users.go
  2. +117
    -0
      users_test.go

+ 79
- 4
users.go View File

@ -6,6 +6,12 @@
package github
import (
"fmt"
"net/url"
"strconv"
)
// UsersService handles communication with the user related
// methods of the GitHub API.
//
@ -15,8 +21,77 @@ type UsersService struct {
}
type User struct {
Login string `json:"login,omitempty"`
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Login string `json:"login,omitempty"`
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
GravatarID string `json:"gravatar_id,omitempty"`
Name string `json:"name,omitempty"`
Company string `json:"company,omitempty"`
Blog string `json:"blog,omitempty"`
Location string `json:"location,omitempty"`
Email string `json:"email,omitempty"`
Hireable bool `json:"hireable,omitempty"`
PublicRepos int `json:"public_repos,omitempty"`
Followers int `json:"followers,omitempty"`
Following int `json:"following,omitempty"`
}
// Get fetches a user. Passing the empty string will fetch the authenticated
// user.
func (s *UsersService) Get(user string) (*User, error) {
var url_ string
if user != "" {
url_ = fmt.Sprintf("users/%v", user)
} else {
url_ = "user"
}
req, err := s.client.NewRequest("GET", url_, nil)
if err != nil {
return nil, err
}
u := new(User)
_, err = s.client.Do(req, u)
return u, err
}
// Edit the authenticated user.
func (s *UsersService) Edit(user *User) (*User, error) {
url_ := "user"
req, err := s.client.NewRequest("PATCH", url_, user)
if err != nil {
return nil, err
}
u := new(User)
_, err = s.client.Do(req, u)
return u, err
}
// UserListOptions specifies optional paramters to the UsersService.List
// method.
type UserListOptions struct {
// ID of the last user seen
Since int
}
// List all users.
func (s *UsersService) List(opt *UserListOptions) ([]User, error) {
url_ := "users"
if opt != nil {
params := url.Values{
"since": []string{strconv.Itoa(opt.Since)},
}
url_ += "?" + params.Encode()
}
req, err := s.client.NewRequest("GET", url_, nil)
if err != nil {
return nil, err
}
users := new([]User)
_, err = s.client.Do(req, users)
return *users, err
}

+ 117
- 0
users_test.go View File

@ -0,0 +1,117 @@
// Copyright 2013 Google. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package github
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestUsersService_Get_authenticatedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Request method = %v, want %v", r.Method, "GET")
}
fmt.Fprint(w, `{"id":1}`)
})
user, err := client.Users.Get("")
if err != nil {
t.Errorf("Users.Get returned error: %v", err)
}
want := &User{ID: 1}
if !reflect.DeepEqual(user, want) {
t.Errorf("Users.Get returned %+v, want %+v", user, want)
}
}
func TestUsersService_Get_specifiedUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Request method = %v, want %v", r.Method, "GET")
}
fmt.Fprint(w, `{"id":1}`)
})
user, err := client.Users.Get("u")
if err != nil {
t.Errorf("Users.Get returned error: %v", err)
}
want := &User{ID: 1}
if !reflect.DeepEqual(user, want) {
t.Errorf("Users.Get returned %+v, want %+v", user, want)
}
}
func TestUsersService_Edit(t *testing.T) {
setup()
defer teardown()
input := &User{Name: "n"}
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
v := new(User)
json.NewDecoder(r.Body).Decode(v)
if r.Method != "PATCH" {
t.Errorf("Request method = %v, want %v", r.Method, "GET")
}
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"id":1}`)
})
user, err := client.Users.Edit(input)
if err != nil {
t.Errorf("Users.Edit returned error: %v", err)
}
want := &User{ID: 1}
if !reflect.DeepEqual(user, want) {
t.Errorf("Users.Edit returned %+v, want %+v", user, want)
}
}
func TestUsersService_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
var v string
if r.Method != "GET" {
t.Errorf("Request method = %v, want %v", r.Method, "GET")
}
if v = r.FormValue("since"); v != "1" {
t.Errorf("Request since parameter = %v, want %v", v, "1")
}
fmt.Fprint(w, `[{"id":2}]`)
})
opt := &UserListOptions{1}
users, err := client.Users.List(opt)
if err != nil {
t.Errorf("Users.Get returned error: %v", err)
}
want := []User{User{ID: 2}}
if !reflect.DeepEqual(users, want) {
t.Errorf("Users.List returned %+v, want %+v", users, want)
}
}

Loading…
Cancel
Save