This commit adds the core library funcationality and establishes the general calling style and testing structure. Only a few GitHub API methods related to organizations and repositories are included in this first commit, mainly to cement the calling style.
| @ -0,0 +1,28 @@ | |||||
| package main | |||||
| import ( | |||||
| "fmt" | |||||
| "github.com/google/go-github" | |||||
| ) | |||||
| func main() { | |||||
| client := github.NewClient(nil) | |||||
| fmt.Println("Recently updated repositories owned by user willnorris:") | |||||
| opt := &github.RepositoryListOptions{Type: "owner", Sort: "updated", Direction: "desc"} | |||||
| repos, err := client.Repositories.List("willnorris", opt) | |||||
| if err != nil { | |||||
| fmt.Printf("error: %v\n\n", err) | |||||
| } else { | |||||
| fmt.Printf("%#v\n\n", repos) | |||||
| } | |||||
| rate, err := client.RateLimit() | |||||
| if err != nil { | |||||
| fmt.Printf("Error fetching rate limit: %#v\n\n", err) | |||||
| return | |||||
| } | |||||
| fmt.Printf("API Rate Limit: %#v\n\n", rate) | |||||
| } | |||||
| @ -0,0 +1,236 @@ | |||||
| // 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 provides a client for using the GitHub API. | |||||
| Access different parts of the GitHub API using the various services on a GitHub | |||||
| Client: | |||||
| client := github.NewClient(nil) | |||||
| // list all organizations for user "willnorris" | |||||
| orgs, err := client.Organizations.List("willnorris", nil) | |||||
| Set optional parameters for an API method by passing an Options object. | |||||
| // list recently updated repositories for org "github" | |||||
| opt := &github.RepositoryListByOrgOptions{Sort: "updated"} | |||||
| repos, err := client.Repositories.ListByOrg("github", opt) | |||||
| Make authenticated API calls by constructing a GitHub client using an OAuth | |||||
| capable http.Client: | |||||
| import "code.google.com/p/goauth2/oauth" | |||||
| // simple OAuth transport if you already have an access token; | |||||
| // see goauth2 library for full usage | |||||
| t := &oauth.Transport{ | |||||
| Config: &oauth.Config{}, | |||||
| Token: &oauth.Token{AccessToken: "..."} | |||||
| } | |||||
| client := github.NewClient(t.Client()) | |||||
| // list all repositories for the authenticated user | |||||
| repos, err := client.Repositories.List(nil) | |||||
| The full GitHub API is documented at http://developer.github.com/v3/. | |||||
| */ | |||||
| package github | |||||
| import ( | |||||
| "bytes" | |||||
| "encoding/json" | |||||
| "fmt" | |||||
| "io/ioutil" | |||||
| "net/http" | |||||
| "net/url" | |||||
| ) | |||||
| const ( | |||||
| libraryVersion = "0.1" | |||||
| defaultBaseURL = "https://api.github.com/" | |||||
| userAgent = "go-github/" + libraryVersion | |||||
| ) | |||||
| // A Client manages communication with the GitHub API. | |||||
| type Client struct { | |||||
| // HTTP client used to communicate with the API. | |||||
| client *http.Client | |||||
| // Base URL for API requests. Defaults to the public GitHub API, but can be | |||||
| // set to a domain endpoint to use with GitHub Enterprise. BaseURL should | |||||
| // always be specified with a trailing slash. | |||||
| BaseURL *url.URL | |||||
| // User agent used when communicating with the GitHub API. | |||||
| UserAgent string | |||||
| // Services used for talking to different parts of the API | |||||
| Organizations *OrganizationsService | |||||
| Repositories *RepositoriesService | |||||
| Users *UsersService | |||||
| } | |||||
| // 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. | |||||
| func NewClient(httpClient *http.Client) *Client { | |||||
| if httpClient == nil { | |||||
| httpClient = http.DefaultClient | |||||
| } | |||||
| baseURL, _ := url.Parse(defaultBaseURL) | |||||
| c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent} | |||||
| c.Organizations = &OrganizationsService{client: c} | |||||
| c.Repositories = &RepositoriesService{client: c} | |||||
| c.Users = &UsersService{client: c} | |||||
| return c | |||||
| } | |||||
| // NewRequest creates an API request. A relative URL can be provided in urls, | |||||
| // in which case it is resolved relative to the BaseURL of the Client. | |||||
| // Relative URLs should always be specified without a preceding slash. If | |||||
| // specified, the value pointed to by body is JSON encoded and included as the | |||||
| // request body. | |||||
| func (c *Client) NewRequest(method, urls string, body interface{}) (*http.Request, error) { | |||||
| rel, err := url.Parse(urls) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| url_ := c.BaseURL.ResolveReference(rel) | |||||
| buf := new(bytes.Buffer) | |||||
| if body != nil { | |||||
| err := json.NewEncoder(buf).Encode(body) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| } | |||||
| req, err := http.NewRequest(method, url_.String(), buf) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| req.Header.Add("User-Agent", c.UserAgent) | |||||
| return req, nil | |||||
| } | |||||
| // Do sends an API request and returns the API response. The API response is | |||||
| // decoded and stored in the value pointed to by v, or returned as an error if | |||||
| // an API error has occurred. | |||||
| func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) { | |||||
| resp, err := c.client.Do(req) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| defer resp.Body.Close() | |||||
| err = CheckResponse(resp) | |||||
| if err != nil { | |||||
| return resp, err | |||||
| } | |||||
| if v != nil { | |||||
| err = json.NewDecoder(resp.Body).Decode(v) | |||||
| } | |||||
| return resp, err | |||||
| } | |||||
| /* | |||||
| An ErrorResponse reports one or more errors caused by an API request. | |||||
| GitHub API docs: http://developer.github.com/v3/#client-errors | |||||
| */ | |||||
| type ErrorResponse struct { | |||||
| Response *http.Response // HTTP response that caused this error | |||||
| Message string `json:message` // error message | |||||
| Errors []Error `json:errors` // more detail on individual errors | |||||
| } | |||||
| func (r *ErrorResponse) Error() string { | |||||
| return fmt.Sprintf("%v %v: %d %v", | |||||
| r.Response.Request.Method, r.Response.Request.URL, | |||||
| r.Response.StatusCode, r.Message) | |||||
| } | |||||
| /* | |||||
| An Error reports more details on an individual error in an ErrorResponse. | |||||
| These are the possible validation error codes: | |||||
| missing: | |||||
| resource does not exist | |||||
| missing_field: | |||||
| a required field on a resource has not been set | |||||
| invalid: | |||||
| the formatting of a field is invalid | |||||
| already_exists: | |||||
| another resource has the same valid as this field | |||||
| GitHub API docs: http://developer.github.com/v3/#client-errors | |||||
| */ | |||||
| type Error struct { | |||||
| Resource string `json:resource` // resource on which the error occurred | |||||
| Field string `json:field` // field on which the error occurred | |||||
| Code string `json:code` // validation error code | |||||
| } | |||||
| func (e *Error) Error() string { | |||||
| return fmt.Sprintf("%v error caused by %v field on %v resource", | |||||
| e.Code, e.Field, e.Resource) | |||||
| } | |||||
| // CheckResponse checks the API response for errors, and returns them if | |||||
| // present. | |||||
| func CheckResponse(r *http.Response) error { | |||||
| if c := r.StatusCode; 200 <= c && c <= 299 { | |||||
| return nil | |||||
| } | |||||
| data, err := ioutil.ReadAll(r.Body) | |||||
| if err == nil { | |||||
| errorResponse := new(ErrorResponse) | |||||
| err = json.Unmarshal(data, errorResponse) | |||||
| if err == nil && errorResponse != nil { | |||||
| errorResponse.Response = r | |||||
| return errorResponse | |||||
| } | |||||
| } | |||||
| return fmt.Errorf("github: got HTTP response code %d and error reading body: %v", | |||||
| r.StatusCode, err) | |||||
| } | |||||
| // API response wrapper to a rate limit request. | |||||
| type rateResponse struct { | |||||
| Rate *Rate `json:rate` | |||||
| } | |||||
| // Rate represents the rate limit for the current client. Unauthenticated | |||||
| // requests are limited to 60 per hour. Authenticated requests are limited to | |||||
| // 5,000 per hour. | |||||
| type Rate struct { | |||||
| // The number of requests per hour the client is currently limited to. | |||||
| Limit int `json:limit` | |||||
| // The number of remaining requests the client can make this hour. | |||||
| Remaining int `json:remaining` | |||||
| } | |||||
| // RateLimit returns the rate limit for the current client. | |||||
| func (c *Client) RateLimit() (*Rate, error) { | |||||
| req, err := c.NewRequest("GET", "rate_limit", nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| response := new(rateResponse) | |||||
| _, err = c.Do(req, response) | |||||
| return response.Rate, err | |||||
| } | |||||
| @ -0,0 +1,197 @@ | |||||
| // 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 ( | |||||
| "fmt" | |||||
| "io/ioutil" | |||||
| "net/http" | |||||
| "net/http/httptest" | |||||
| "net/url" | |||||
| "reflect" | |||||
| "strings" | |||||
| "testing" | |||||
| ) | |||||
| var ( | |||||
| // mux is the HTTP request multiplexer used with the test server. | |||||
| mux *http.ServeMux | |||||
| // client is the GitHub client being tested. | |||||
| client *Client | |||||
| // server is a test HTTP server used to provide mock API responses. | |||||
| server *httptest.Server | |||||
| ) | |||||
| // setup sets up a test HTTP server along with a github.Client that is | |||||
| // configured to talk to that test server. Tests should register handlers on | |||||
| // mux which provide mock responses for the API method being tested. | |||||
| func setup() { | |||||
| // test server | |||||
| mux = http.NewServeMux() | |||||
| server = httptest.NewServer(mux) | |||||
| // github client configured to use test server | |||||
| client = NewClient(nil) | |||||
| client.BaseURL, _ = url.Parse(server.URL) | |||||
| } | |||||
| // teardown closes the test HTTP server. | |||||
| func teardown() { | |||||
| server.Close() | |||||
| } | |||||
| func TestNewClient(t *testing.T) { | |||||
| c := NewClient(nil) | |||||
| if c.BaseURL.String() != defaultBaseURL { | |||||
| t.Errorf("NewClient BaseURL = %v, want %v", c.BaseURL.String(), defaultBaseURL) | |||||
| } | |||||
| if c.UserAgent != userAgent { | |||||
| t.Errorf("NewClient UserAgent = %v, want %v", c.UserAgent, userAgent) | |||||
| } | |||||
| } | |||||
| func TestNewRequest(t *testing.T) { | |||||
| c := NewClient(nil) | |||||
| inURL, outURL := "/foo", defaultBaseURL+"foo" | |||||
| inBody, outBody := &User{Login: "l"}, `{"login":"l"}`+"\n" | |||||
| req, _ := c.NewRequest("GET", inURL, inBody) | |||||
| // test that relative URL was expanded | |||||
| if req.URL.String() != outURL { | |||||
| t.Errorf("NewRequest(%v) URL = %v, want %v", inURL, req.URL, outURL) | |||||
| } | |||||
| // test that body was JSON encoded | |||||
| body, _ := ioutil.ReadAll(req.Body) | |||||
| if string(body) != outBody { | |||||
| t.Errorf("NewRequest(%v) Body = %v, want %v", inBody, string(body), outBody) | |||||
| } | |||||
| // test that default user-agent is attached to the request | |||||
| userAgent := req.Header.Get("User-Agent") | |||||
| if c.UserAgent != userAgent { | |||||
| t.Errorf("NewRequest() User-Agent = %v, want %v", userAgent, c.UserAgent) | |||||
| } | |||||
| } | |||||
| func TestNewRequest_invalidJSON(t *testing.T) { | |||||
| c := NewClient(nil) | |||||
| type T struct { | |||||
| A map[int]interface{} | |||||
| } | |||||
| _, err := c.NewRequest("GET", "/", &T{}) | |||||
| if err == nil { | |||||
| t.Error("Expected JSON marshalling error.") | |||||
| } | |||||
| } | |||||
| func TestDo(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| type foo struct { | |||||
| A string | |||||
| } | |||||
| mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "GET" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| fmt.Fprint(w, `{"A":"a"}`) | |||||
| }) | |||||
| req, _ := client.NewRequest("GET", "/", nil) | |||||
| body := new(foo) | |||||
| client.Do(req, body) | |||||
| want := &foo{"a"} | |||||
| if !reflect.DeepEqual(body, want) { | |||||
| t.Errorf("Response body = %v, want %v", body, want) | |||||
| } | |||||
| } | |||||
| func TestDo_httpError(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| http.Error(w, "Bad Request", 400) | |||||
| }) | |||||
| req, _ := client.NewRequest("GET", "/", nil) | |||||
| _, err := client.Do(req, nil) | |||||
| if err == nil { | |||||
| t.Error("Expected HTTP 400 error.") | |||||
| } | |||||
| } | |||||
| func TestCheckResponse(t *testing.T) { | |||||
| res := &http.Response{ | |||||
| Request: &http.Request{}, | |||||
| StatusCode: 400, | |||||
| Body: ioutil.NopCloser(strings.NewReader(`{"message":"m", | |||||
| "errors": [{"resource": "r", "field": "f", "code": "c"}]}`)), | |||||
| } | |||||
| err := CheckResponse(res).(*ErrorResponse) | |||||
| if err == nil { | |||||
| t.Errorf("Expected error response.") | |||||
| } | |||||
| want := &ErrorResponse{ | |||||
| Response: res, | |||||
| Message: "m", | |||||
| Errors: []Error{Error{Resource: "r", Field: "f", Code: "c"}}, | |||||
| } | |||||
| if !reflect.DeepEqual(err, want) { | |||||
| t.Errorf("Error = %#v, want %#v", err, want) | |||||
| } | |||||
| } | |||||
| func TestErrorResponse_Error(t *testing.T) { | |||||
| res := &http.Response{Request: &http.Request{}} | |||||
| err := ErrorResponse{Message: "m", Response: res} | |||||
| if err.Error() == "" { | |||||
| t.Errorf("Expected non-empty ErrorResponse.Error()") | |||||
| } | |||||
| } | |||||
| func TestError_Error(t *testing.T) { | |||||
| err := Error{} | |||||
| if err.Error() == "" { | |||||
| t.Errorf("Expected non-empty Error.Error()") | |||||
| } | |||||
| } | |||||
| func TestRateLimit(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/rate_limit", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "GET" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| fmt.Fprint(w, `{"rate":{"limit":2,"remaining":1}}`) | |||||
| }) | |||||
| rate, err := client.RateLimit() | |||||
| if err != nil { | |||||
| t.Errorf("Rate limit returned error: %v", err) | |||||
| } | |||||
| want := &Rate{Limit: 2, Remaining: 1} | |||||
| if !reflect.DeepEqual(rate, want) { | |||||
| t.Errorf("RateLimit returned %+v, want %+v", rate, want) | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,171 @@ | |||||
| // 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 ( | |||||
| "fmt" | |||||
| ) | |||||
| // OrganizationsService provides access to the organization related functions | |||||
| // in the GitHub API. | |||||
| // | |||||
| // GitHub API docs: http://developer.github.com/v3/orgs/ | |||||
| type OrganizationsService struct { | |||||
| client *Client | |||||
| } | |||||
| type Organization struct { | |||||
| Login string `json:"login,omitempty"` | |||||
| ID int `json:"id,omitempty"` | |||||
| URL string `json:"url,omitempty"` | |||||
| AvatarURL string `json:"avatar_url,omitempty"` | |||||
| Location string `json:"location,omitempty"` | |||||
| } | |||||
| type Team struct { | |||||
| ID int `json:"id,omitempty"` | |||||
| Name string `json:"name,omitempty"` | |||||
| URL string `json:"url,omitempty"` | |||||
| Slug string `json:"slug,omitempty"` | |||||
| Permission string `json:"permission,omitempty"` | |||||
| MembersCount int `json:"members_count,omitempty"` | |||||
| ReposCount int `json:"repos_count,omitempty"` | |||||
| } | |||||
| // 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) { | |||||
| var url string | |||||
| if user != "" { | |||||
| url = fmt.Sprintf("users/%v/orgs", user) | |||||
| } else { | |||||
| url = "user/orgs" | |||||
| } | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| orgs := new([]Organization) | |||||
| _, err = s.client.Do(req, orgs) | |||||
| return *orgs, err | |||||
| } | |||||
| // Get an organization. | |||||
| func (s *OrganizationsService) Get(org string) (*Organization, error) { | |||||
| url := fmt.Sprintf("orgs/%v", org) | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| organization := new(Organization) | |||||
| _, err = s.client.Do(req, organization) | |||||
| return organization, err | |||||
| } | |||||
| // Edit an organization. | |||||
| func (s *OrganizationsService) Edit(name string, org *Organization) (*Organization, error) { | |||||
| url := fmt.Sprintf("orgs/%v", name) | |||||
| req, err := s.client.NewRequest("PATCH", url, org) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| updatedOrg := new(Organization) | |||||
| _, err = s.client.Do(req, updatedOrg) | |||||
| return updatedOrg, err | |||||
| } | |||||
| // List the members for an organization. If the authenticated user is an owner | |||||
| // of the organization, this will return concealed and public members, | |||||
| // otherwise it will only return public members. | |||||
| func (s *OrganizationsService) ListMembers(org string) ([]User, error) { | |||||
| url := fmt.Sprintf("orgs/%v/members", org) | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| members := new([]User) | |||||
| _, err = s.client.Do(req, members) | |||||
| return *members, err | |||||
| } | |||||
| // List the public members for an organization. | |||||
| func (s *OrganizationsService) ListPublicMembers(org string) ([]User, error) { | |||||
| url := fmt.Sprintf("orgs/%v/public_members", org) | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| members := new([]User) | |||||
| _, err = s.client.Do(req, members) | |||||
| return *members, err | |||||
| } | |||||
| // List the teams for an organization. | |||||
| func (s *OrganizationsService) ListTeams(org string) ([]Team, error) { | |||||
| url := fmt.Sprintf("orgs/%v/teams", org) | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| teams := new([]Team) | |||||
| _, err = s.client.Do(req, teams) | |||||
| return *teams, err | |||||
| } | |||||
| // Add a user to a team. | |||||
| func (s *OrganizationsService) AddTeamMember(team int, user string) error { | |||||
| url := fmt.Sprintf("teams/%v/members/%v", team, user) | |||||
| req, err := s.client.NewRequest("PUT", url, nil) | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| _, err = s.client.Do(req, nil) | |||||
| return err | |||||
| } | |||||
| // Remove a user from a team. | |||||
| func (s *OrganizationsService) RemoveTeamMember(team int, user string) error { | |||||
| url := fmt.Sprintf("teams/%v/members/%v", team, user) | |||||
| req, err := s.client.NewRequest("DELETE", url, nil) | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| _, err = s.client.Do(req, nil) | |||||
| return err | |||||
| } | |||||
| // Publicize a user's membership in an organization. | |||||
| func (s *OrganizationsService) PublicizeMembership(org, user string) error { | |||||
| url := fmt.Sprintf("orgs/%v/public_members/%v", org, user) | |||||
| req, err := s.client.NewRequest("PUT", url, nil) | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| _, err = s.client.Do(req, nil) | |||||
| return err | |||||
| } | |||||
| // Conceal a user's membership in an organization. | |||||
| func (s *OrganizationsService) ConcealMembership(org, user string) error { | |||||
| url := fmt.Sprintf("orgs/%v/public_members/%v", org, user) | |||||
| req, err := s.client.NewRequest("DELETE", url, nil) | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| _, err = s.client.Do(req, nil) | |||||
| return err | |||||
| } | |||||
| @ -0,0 +1,242 @@ | |||||
| // 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 TestOrganizationsService_List_authenticatedUser(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/user/orgs", 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},{"id":2}]`) | |||||
| }) | |||||
| orgs, err := client.Organizations.List("") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.List returned error: %v", err) | |||||
| } | |||||
| want := []Organization{Organization{ID: 1}, Organization{ID: 2}} | |||||
| if !reflect.DeepEqual(orgs, want) { | |||||
| t.Errorf("Organizations.List returned %+v, want %+v", orgs, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_List_specifiedUser(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/users/u/orgs", 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},{"id":2}]`) | |||||
| }) | |||||
| orgs, err := client.Organizations.List("u") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.List returned error: %v", err) | |||||
| } | |||||
| want := []Organization{Organization{ID: 1}, Organization{ID: 2}} | |||||
| if !reflect.DeepEqual(orgs, want) { | |||||
| t.Errorf("Organizations.List returned %+v, want %+v", orgs, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_Get(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o", 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, "login":"l", "url":"u", "avatar_url": "a", "location":"l"}`) | |||||
| }) | |||||
| org, err := client.Organizations.Get("o") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.Get returned error: %v", err) | |||||
| } | |||||
| want := &Organization{ID: 1, Login: "l", URL: "u", AvatarURL: "a", Location: "l"} | |||||
| if !reflect.DeepEqual(org, want) { | |||||
| t.Errorf("Organizations.Get returned %+v, want %+v", org, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_Edit(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| input := &Organization{Login: "l"} | |||||
| mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) { | |||||
| v := new(Organization) | |||||
| 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}`) | |||||
| }) | |||||
| org, err := client.Organizations.Edit("o", input) | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.Edit returned error: %v", err) | |||||
| } | |||||
| want := &Organization{ID: 1} | |||||
| if !reflect.DeepEqual(org, want) { | |||||
| t.Errorf("Organizations.Edit returned %+v, want %+v", org, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_ListMembers(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o/members", 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}]`) | |||||
| }) | |||||
| members, err := client.Organizations.ListMembers("o") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.ListMembers returned error: %v", err) | |||||
| } | |||||
| want := []User{User{ID: 1}} | |||||
| if !reflect.DeepEqual(members, want) { | |||||
| t.Errorf("Organizations.ListMembers returned %+v, want %+v", members, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_ListPublicMembers(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o/public_members", 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}]`) | |||||
| }) | |||||
| members, err := client.Organizations.ListPublicMembers("o") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.ListPublicMembers returned error: %v", err) | |||||
| } | |||||
| want := []User{User{ID: 1}} | |||||
| if !reflect.DeepEqual(members, want) { | |||||
| t.Errorf("Organizations.ListPublicMembers returned %+v, want %+v", members, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_ListTeams(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o/teams", 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}]`) | |||||
| }) | |||||
| teams, err := client.Organizations.ListTeams("o") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.ListTeams returned error: %v", err) | |||||
| } | |||||
| want := []Team{Team{ID: 1}} | |||||
| if !reflect.DeepEqual(teams, want) { | |||||
| t.Errorf("Organizations.ListTeams returned %+v, want %+v", teams, want) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_AddTeamMember(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/teams/1/members/u", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "PUT" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| }) | |||||
| err := client.Organizations.AddTeamMember(1, "u") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.AddTeamMember returned error: %v", err) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_RemoveTeamMember(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/teams/1/members/u", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "DELETE" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| }) | |||||
| err := client.Organizations.RemoveTeamMember(1, "u") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.RemoveTeamMember returned error: %v", err) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_PublicizeMembership(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o/public_members/u", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "PUT" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| }) | |||||
| err := client.Organizations.PublicizeMembership("o", "u") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.PublicizeMembership returned error: %v", err) | |||||
| } | |||||
| } | |||||
| func TestOrganizationsService_ConcealMembership(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/orgs/o/public_members/u", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "DELETE" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| }) | |||||
| err := client.Organizations.ConcealMembership("o", "u") | |||||
| if err != nil { | |||||
| t.Errorf("Organizations.ConcealMembership returned error: %v", err) | |||||
| } | |||||
| } | |||||
| @ -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 ( | |||||
| "fmt" | |||||
| "net/url" | |||||
| "strconv" | |||||
| ) | |||||
| // RepositoriesService handles communication with the repository related | |||||
| // methods of the GitHub API. | |||||
| // | |||||
| // GitHub API docs: http://developer.github.com/v3/repos/ | |||||
| type RepositoriesService struct { | |||||
| client *Client | |||||
| } | |||||
| type Repository struct { | |||||
| ID int `json:"id,omitempty"` | |||||
| Owner *User `json:"owner,omitempty"` | |||||
| Name string `json:"name,omitempty"` | |||||
| Description string `json:"description,omitempty"` | |||||
| } | |||||
| // RepositoryListOptions specifies the optional parameters to the | |||||
| // RepositoriesService.List method. | |||||
| type RepositoryListOptions struct { | |||||
| // Type of repositories to list. Possible values are: all, owner, public, | |||||
| // private, member. Default is "all". | |||||
| Type string | |||||
| // How to sort the repository list. Possible values are: created, updated, | |||||
| // pushed, full_name. Default is "full_name". | |||||
| Sort string | |||||
| // Direction in which to sort repositories. Possible values are: asc, desc. | |||||
| // Default is "asc" when sort is "full_name", otherwise default is "desc". | |||||
| Direction string | |||||
| // For paginated result sets, page of results to retrieve. | |||||
| Page int | |||||
| } | |||||
| // List the repositories for a user. Passing the empty string will list | |||||
| // repositories for the authenticated user. | |||||
| func (s *RepositoriesService) List(user string, opt *RepositoryListOptions) ([]Repository, error) { | |||||
| var urls string | |||||
| if user != "" { | |||||
| urls = fmt.Sprintf("users/%v/repos", user) | |||||
| } else { | |||||
| urls = "user/repos" | |||||
| } | |||||
| if opt != nil { | |||||
| params := url.Values{ | |||||
| "type": []string{opt.Type}, | |||||
| "sort": []string{opt.Sort}, | |||||
| "direction": []string{opt.Direction}, | |||||
| "page": []string{strconv.Itoa(opt.Page)}, | |||||
| } | |||||
| urls += "?" + params.Encode() | |||||
| } | |||||
| req, err := s.client.NewRequest("GET", urls, nil) | |||||
| repos := new([]Repository) | |||||
| _, err = s.client.Do(req, repos) | |||||
| return *repos, err | |||||
| } | |||||
| // RepositoryListByOrgOptions specifies the optional parameters to the | |||||
| // RepositoriesService.ListByOrg method. | |||||
| type RepositoryListByOrgOptions struct { | |||||
| // Type of repositories to list. Possible values are: all, public, private, | |||||
| // forks, sources, member. Default is "all". | |||||
| Type string | |||||
| // For paginated result sets, page of results to retrieve. | |||||
| Page int | |||||
| } | |||||
| // List the repositories for an organization. | |||||
| func (s *RepositoriesService) ListByOrg(org string, opt *RepositoryListByOrgOptions) ([]Repository, error) { | |||||
| urls := fmt.Sprintf("orgs/%v/repos", org) | |||||
| if opt != nil { | |||||
| params := url.Values{ | |||||
| "type": []string{opt.Type}, | |||||
| "page": []string{strconv.Itoa(opt.Page)}, | |||||
| } | |||||
| urls += "?" + params.Encode() | |||||
| } | |||||
| req, err := s.client.NewRequest("GET", urls, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| repos := new([]Repository) | |||||
| _, err = s.client.Do(req, repos) | |||||
| return *repos, err | |||||
| } | |||||
| // Get fetches a repository. | |||||
| func (s *RepositoriesService) Get(owner, repo string) (*Repository, error) { | |||||
| url := fmt.Sprintf("repos/%v/%v", owner, repo) | |||||
| req, err := s.client.NewRequest("GET", url, nil) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| repository := new(Repository) | |||||
| _, err = s.client.Do(req, repository) | |||||
| return repository, err | |||||
| } | |||||
| @ -0,0 +1,126 @@ | |||||
| // 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 ( | |||||
| "fmt" | |||||
| "net/http" | |||||
| "reflect" | |||||
| "testing" | |||||
| ) | |||||
| func TestRepositoriesService_List_authenticatedUser(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/user/repos", 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},{"id":2}]`) | |||||
| }) | |||||
| repos, err := client.Repositories.List("", nil) | |||||
| if err != nil { | |||||
| t.Errorf("Repositories.List returned error: %v", err) | |||||
| } | |||||
| want := []Repository{Repository{ID: 1}, Repository{ID: 2}} | |||||
| if !reflect.DeepEqual(repos, want) { | |||||
| t.Errorf("Repositories.List returned %+v, want %+v", repos, want) | |||||
| } | |||||
| } | |||||
| func TestRepositoriesService_List_specifiedUser(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| opt := &RepositoryListOptions{"owner", "created", "asc", 2} | |||||
| mux.HandleFunc("/users/u/repos", 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("type"); v != "owner" { | |||||
| t.Errorf("Request type parameter = %v, want %v", v, "owner") | |||||
| } | |||||
| if v = r.FormValue("sort"); v != "created" { | |||||
| t.Errorf("Request sort parameter = %v, want %v", v, "created") | |||||
| } | |||||
| if v = r.FormValue("direction"); v != "asc" { | |||||
| t.Errorf("Request direction parameter = %v, want %v", v, "created") | |||||
| } | |||||
| if v = r.FormValue("page"); v != "2" { | |||||
| t.Errorf("Request page parameter = %v, want %v", v, "2") | |||||
| } | |||||
| fmt.Fprint(w, `[{"id":1}]`) | |||||
| }) | |||||
| repos, err := client.Repositories.List("u", opt) | |||||
| if err != nil { | |||||
| t.Errorf("Repositories.List returned error: %v", err) | |||||
| } | |||||
| want := []Repository{Repository{ID: 1}} | |||||
| if !reflect.DeepEqual(repos, want) { | |||||
| t.Errorf("Repositories.List returned %+v, want %+v", repos, want) | |||||
| } | |||||
| } | |||||
| func TestRepositoriesService_ListByOrg(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| opt := &RepositoryListByOrgOptions{"forks", 2} | |||||
| mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { | |||||
| if r.Method != "GET" { | |||||
| t.Errorf("Request method = %v, want %v", r.Method, "GET") | |||||
| } | |||||
| v := r.FormValue("type") | |||||
| if v != "forks" { | |||||
| t.Errorf("Request type parameter = %v, want %v", v, "forks") | |||||
| } | |||||
| v = r.FormValue("page") | |||||
| if v != "2" { | |||||
| t.Errorf("Request page parameter = %v, want %v", v, "2") | |||||
| } | |||||
| fmt.Fprint(w, `[{"id":1}]`) | |||||
| }) | |||||
| repos, err := client.Repositories.ListByOrg("o", opt) | |||||
| if err != nil { | |||||
| t.Errorf("Repositories.ListByOrg returned error: %v", err) | |||||
| } | |||||
| want := []Repository{Repository{ID: 1}} | |||||
| if !reflect.DeepEqual(repos, want) { | |||||
| t.Errorf("Repositories.ListByOrg returned %+v, want %+v", repos, want) | |||||
| } | |||||
| } | |||||
| func TestRepositoriesService_Get(t *testing.T) { | |||||
| setup() | |||||
| defer teardown() | |||||
| mux.HandleFunc("/repos/o/r", 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,"name":"n","description":"d","owner":{"login":"l"}}`) | |||||
| }) | |||||
| repo, err := client.Repositories.Get("o", "r") | |||||
| if err != nil { | |||||
| t.Errorf("Repositories.Get returned error: %v", err) | |||||
| } | |||||
| want := &Repository{ID: 1, Name: "n", Description: "d", Owner: &User{Login: "l"}} | |||||
| if !reflect.DeepEqual(repo, want) { | |||||
| t.Errorf("Repositories.Get returned %+v, want %+v", repo, want) | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,22 @@ | |||||
| // 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 | |||||
| // UsersService handles communication with the user related | |||||
| // methods of the GitHub API. | |||||
| // | |||||
| // GitHub API docs: http://developer.github.com/v3/users/ | |||||
| type UsersService struct { | |||||
| client *Client | |||||
| } | |||||
| type User struct { | |||||
| Login string `json:"login,omitempty"` | |||||
| ID int `json:"id,omitempty"` | |||||
| URL string `json:"url,omitempty"` | |||||
| AvatarURL string `json:"avatar_url,omitempty"` | |||||
| } | |||||