Browse Source

pass ListOptions to Organizations.List

also minor doc cleanup
Will Norris 13 years ago
parent
commit
1329391b03
4 changed files with 29 additions and 8 deletions
  1. +4
    -4
      README.md
  2. +8
    -1
      github.go
  3. +10
    -1
      orgs.go
  4. +7
    -2
      orgs_test.go

+ 4
- 4
README.md View File

@ -17,7 +17,7 @@ client := github.NewClient(nil)
orgs, err := client.Organizations.List("willnorris", nil)
```
Some API methods have optional parameters than can be passed. For example,
Some API methods have optional parameters that can be passed. For example,
list recently updated repositories for org "github":
```go
@ -27,16 +27,16 @@ repos, err := client.Repositories.ListByOrg("github", opt)
```
The go-github library does not directly handle authentication. Instead, when
creating a new client, pass an `http.Client` than can handle authentication for
creating a new client, pass an `http.Client` that can handle authentication for
you. The easiest, and recommended, way to do this is using the [goauth2][]
library, but you can of course use any other library that provides an
`http.Client`. For example, to use the goauth2 library with an existing OAuth
`http.Client`. For example, to use the goauth2 library with an existing OAuth2
access token:
```go
t := &oauth.Transport{
Config: &oauth.Config{},
Token: &oauth.Token{AccessToken: "..."}
Token: &oauth.Token{AccessToken: "..."},
}
client := github.NewClient(t.Client())


+ 8
- 1
github.go View File

@ -30,7 +30,7 @@ capable http.Client:
// see goauth2 library for full usage
t := &oauth.Transport{
Config: &oauth.Config{},
Token: &oauth.Token{AccessToken: "..."}
Token: &oauth.Token{AccessToken: "..."},
}
client := github.NewClient(t.Client())
@ -81,6 +81,13 @@ type Client struct {
Users *UsersService
}
// ListOptions specifies the optional parameters to various List methods that
// support pagination.
type ListOptions struct {
// For paginated result sets, page of results to retrieve.
Page int
}
// NewClient returns a new GitHub API client. If a nil httpClient is
// provided, http.DefaultClient will be used. To use API methods which require
// authentication, provide an http.Client that can handle that.


+ 10
- 1
orgs.go View File

@ -8,6 +8,8 @@ package github
import (
"fmt"
"net/url"
"strconv"
)
// OrganizationsService provides access to the organization related functions
@ -38,13 +40,20 @@ type Team struct {
// List the organizations for a user. Passing the empty string will list
// organizations for the authenticated user.
func (s *OrganizationsService) List(user string) ([]Organization, error) {
func (s *OrganizationsService) List(user string, opt *ListOptions) ([]Organization, error) {
var url_ string
if user != "" {
url_ = fmt.Sprintf("users/%v/orgs", user)
} else {
url_ = "user/orgs"
}
if opt != nil {
params := url.Values{
"page": []string{strconv.Itoa(opt.Page)},
}
url_ += "?" + params.Encode()
}
req, err := s.client.NewRequest("GET", url_, nil)
if err != nil {
return nil, err


+ 7
- 2
orgs_test.go View File

@ -25,7 +25,7 @@ func TestOrganizationsService_List_authenticatedUser(t *testing.T) {
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
orgs, err := client.Organizations.List("")
orgs, err := client.Organizations.List("", nil)
if err != nil {
t.Errorf("Organizations.List returned error: %v", err)
}
@ -41,13 +41,18 @@ func TestOrganizationsService_List_specifiedUser(t *testing.T) {
defer teardown()
mux.HandleFunc("/users/u/orgs", func(w http.ResponseWriter, r *http.Request) {
var v string
if r.Method != "GET" {
t.Errorf("Request method = %v, want %v", r.Method, "GET")
}
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
if v = r.FormValue("page"); v != "2" {
t.Errorf("Request page parameter = %v, want %v", v, "2")
}
})
orgs, err := client.Organizations.List("u")
opt := &ListOptions{2}
orgs, err := client.Organizations.List("u", opt)
if err != nil {
t.Errorf("Organizations.List returned error: %v", err)
}


Loading…
Cancel
Save