diff --git a/README.md b/README.md index 90c80eb..72a72bc 100644 --- a/README.md +++ b/README.md @@ -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()) diff --git a/github.go b/github.go index 29f64f2..f731f61 100644 --- a/github.go +++ b/github.go @@ -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. diff --git a/orgs.go b/orgs.go index cb27e8f..03547cb 100644 --- a/orgs.go +++ b/orgs.go @@ -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 diff --git a/orgs_test.go b/orgs_test.go index eadaa10..7406d84 100644 --- a/orgs_test.go +++ b/orgs_test.go @@ -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) }