Browse Source

EventsService with one API method, ListPerformedByUser, and Event struct

Quinn Slack 13 years ago
committed by Will Norris
parent
commit
f1e62dd5d3
3 changed files with 122 additions and 0 deletions
  1. +63
    -0
      github/events.go
  2. +57
    -0
      github/events_test.go
  3. +2
    -0
      github/github.go

+ 63
- 0
github/events.go View File

@ -0,0 +1,63 @@
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
// EventsService provides access to the event related functions
// in the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/
type EventsService struct {
client *Client
}
// Event represents a GitHub event.
type Event struct {
Type string `json:"type,omitempty"`
Public bool `json:"public"`
Payload json.RawMessage `json:"payload,omitempty"`
Repo *Repository `json:"repo,omitempty"`
Actor *User `json:"actor,omitempty"`
Org *Organization `json:"org,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
ID string `json:"id,omitempty"`
}
// ListPerformedByUser lists the events performed by a user. If publicOnly is
// true, only public events will be returned.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
func (s *EventsService) ListPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]Event, error) {
var u string
if publicOnly {
u = fmt.Sprintf("users/%v/events/public", user)
} else {
u = fmt.Sprintf("users/%v/events", user)
}
if opt != nil {
params := url.Values{
"page": []string{strconv.Itoa(opt.Page)},
}
u += "?" + params.Encode()
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
events := new([]Event)
_, err = s.client.Do(req, events)
return *events, err
}

+ 57
- 0
github/events_test.go View File

@ -0,0 +1,57 @@
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestEventsService_ListPerformedByUser_all(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u/events", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`)
})
events, err := client.Events.ListPerformedByUser("u", false, nil)
if err != nil {
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
}
want := []Event{Event{ID: "1"}, Event{ID: "2"}}
if !reflect.DeepEqual(events, want) {
t.Errorf("Events.ListPerformedByUser returned %+v, want %+v", events, want)
}
}
func TestEventsService_ListPerformedByUser_publicOnly(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/users/u/events/public", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`)
})
events, err := client.Events.ListPerformedByUser("u", true, nil)
if err != nil {
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
}
want := []Event{Event{ID: "1"}, Event{ID: "2"}}
if !reflect.DeepEqual(events, want) {
t.Errorf("Events.ListPerformedByUser returned %+v, want %+v", events, want)
}
}

+ 2
- 0
github/github.go View File

@ -87,6 +87,7 @@ type Client struct {
// Services used for talking to different parts of the API // Services used for talking to different parts of the API
Events *EventsService
Issues *IssuesService Issues *IssuesService
Organizations *OrganizationsService Organizations *OrganizationsService
PullRequests *PullRequestsService PullRequests *PullRequestsService
@ -114,6 +115,7 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL) baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent} c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.Events = &EventsService{client: c}
c.Issues = &IssuesService{client: c} c.Issues = &IssuesService{client: c}
c.Organizations = &OrganizationsService{client: c} c.Organizations = &OrganizationsService{client: c}
c.PullRequests = &PullRequestsService{client: c} c.PullRequests = &PullRequestsService{client: c}


Loading…
Cancel
Save