Skip to content

Commit a51d6b4

Browse files
committed
return Response object from all API functions
The truth is, this change makes me sad. It's a breaking change for all clients and it adds more complexity to the library surface. In most cases, clients will simply drop the Response object on the floor (which is actually all the library itself was doing before this change... now we're just pushing that off to the client). Initially, the Response object will be primarily of interest for functions that return paginated result sets, since the Response.NextPage field is the only way to know for sure if there are more pages that should be fetched. And this is really the cleanest way to get at that data, so in that respect this change isn't so bad. It's also worth noting that returning the raw Response object makes a lot more since in a GitHub library than it may in others, given how GitHub makes liberal (read: proper) use of HTTP request and response headers. Other APIs, like Google's various APIs for example, tend to push things like pagination links into the response body. While this is certainly less of a purist view in terms of REST, it does make the lives of client developers a lot easier, since then the response body contains everything you need to know. But whatever; this is how GitHub rolls, so we'll roll right along with them. (Somewhat ironically we are ignoring the RESTful links in the GitHub response bodies, since we're actually calling the API in an RPC style and don't do anything with those links.) We still don't have an easy way to set arbitrary request headers, but that's a problem for another day. Fixes #22
1 parent 18ed217 commit a51d6b4

40 files changed

+494
-512
lines changed

github/activity_events.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type PushEventCommit struct {
6262
// true, only public events will be returned.
6363
//
6464
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
65-
func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]Event, error) {
65+
func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]Event, *Response, error) {
6666
var u string
6767
if publicOnly {
6868
u = fmt.Sprintf("users/%v/events/public", user)
@@ -79,10 +79,10 @@ func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool
7979

8080
req, err := s.client.NewRequest("GET", u, nil)
8181
if err != nil {
82-
return nil, err
82+
return nil, nil, err
8383
}
8484

8585
events := new([]Event)
86-
_, err = s.client.Do(req, events)
87-
return *events, err
86+
resp, err := s.client.Do(req, events)
87+
return *events, resp, err
8888
}

github/activity_events_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestActivityService_ListEventsPerformedByUser_all(t *testing.T) {
2626
})
2727

2828
opt := &ListOptions{Page: 2}
29-
events, err := client.Activity.ListEventsPerformedByUser("u", false, opt)
29+
events, _, err := client.Activity.ListEventsPerformedByUser("u", false, opt)
3030
if err != nil {
3131
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
3232
}
@@ -46,7 +46,7 @@ func TestActivityService_ListEventsPerformedByUser_publicOnly(t *testing.T) {
4646
fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`)
4747
})
4848

49-
events, err := client.Activity.ListEventsPerformedByUser("u", true, nil)
49+
events, _, err := client.Activity.ListEventsPerformedByUser("u", true, nil)
5050
if err != nil {
5151
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
5252
}

github/activity_star.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type ActivityListStarredOptions struct {
3030
// will list the starred repositories for the authenticated user.
3131
//
3232
// GitHub API docs: http://developer.github.com/v3/activity/starring/#list-repositories-being-starred
33-
func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]Repository, error) {
33+
func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]Repository, *Response, error) {
3434
var u string
3535
if user != "" {
3636
u = fmt.Sprintf("users/%v/starred", user)
@@ -47,10 +47,10 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
4747
}
4848
req, err := s.client.NewRequest("GET", u, nil)
4949
if err != nil {
50-
return nil, err
50+
return nil, nil, err
5151
}
5252

5353
repos := new([]Repository)
54-
_, err = s.client.Do(req, repos)
55-
return *repos, err
54+
resp, err := s.client.Do(req, repos)
55+
return *repos, resp, err
5656
}

github/activity_star_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
2121
fmt.Fprint(w, `[{"id":1}]`)
2222
})
2323

24-
repos, err := client.Activity.ListStarred("", nil)
24+
repos, _, err := client.Activity.ListStarred("", nil)
2525
if err != nil {
2626
t.Errorf("Activity.ListStarred returned error: %v", err)
2727
}
@@ -47,7 +47,7 @@ func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
4747
})
4848

4949
opt := &ActivityListStarredOptions{"created", "asc", 2}
50-
repos, err := client.Activity.ListStarred("u", opt)
50+
repos, _, err := client.Activity.ListStarred("u", opt)
5151
if err != nil {
5252
t.Errorf("Activity.ListStarred returned error: %v", err)
5353
}

github/gists.go

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type GistListOptions struct {
5757
// user.
5858
//
5959
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
60-
func (s *GistsService) List(user string, opt *GistListOptions) ([]Gist, error) {
60+
func (s *GistsService) List(user string, opt *GistListOptions) ([]Gist, *Response, error) {
6161
var u string
6262
if user != "" {
6363
u = fmt.Sprintf("users/%v/gists", user)
@@ -74,18 +74,18 @@ func (s *GistsService) List(user string, opt *GistListOptions) ([]Gist, error) {
7474

7575
req, err := s.client.NewRequest("GET", u, nil)
7676
if err != nil {
77-
return nil, err
77+
return nil, nil, err
7878
}
7979

8080
gists := new([]Gist)
81-
_, err = s.client.Do(req, gists)
82-
return *gists, err
81+
resp, err := s.client.Do(req, gists)
82+
return *gists, resp, err
8383
}
8484

8585
// ListAll lists all public gists.
8686
//
8787
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
88-
func (s *GistsService) ListAll(opt *GistListOptions) ([]Gist, error) {
88+
func (s *GistsService) ListAll(opt *GistListOptions) ([]Gist, *Response, error) {
8989
u := "gists/public"
9090
if opt != nil {
9191
params := url.Values{}
@@ -97,18 +97,18 @@ func (s *GistsService) ListAll(opt *GistListOptions) ([]Gist, error) {
9797

9898
req, err := s.client.NewRequest("GET", u, nil)
9999
if err != nil {
100-
return nil, err
100+
return nil, nil, err
101101
}
102102

103103
gists := new([]Gist)
104-
_, err = s.client.Do(req, gists)
105-
return *gists, err
104+
resp, err := s.client.Do(req, gists)
105+
return *gists, resp, err
106106
}
107107

108108
// ListStarred lists starred gists of authenticated user.
109109
//
110110
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
111-
func (s *GistsService) ListStarred(opt *GistListOptions) ([]Gist, error) {
111+
func (s *GistsService) ListStarred(opt *GistListOptions) ([]Gist, *Response, error) {
112112
u := "gists/starred"
113113
if opt != nil {
114114
params := url.Values{}
@@ -120,118 +120,116 @@ func (s *GistsService) ListStarred(opt *GistListOptions) ([]Gist, error) {
120120

121121
req, err := s.client.NewRequest("GET", u, nil)
122122
if err != nil {
123-
return nil, err
123+
return nil, nil, err
124124
}
125125

126126
gists := new([]Gist)
127-
_, err = s.client.Do(req, gists)
128-
return *gists, err
127+
resp, err := s.client.Do(req, gists)
128+
return *gists, resp, err
129129
}
130130

131131
// Get a single gist.
132132
//
133133
// GitHub API docs: http://developer.github.com/v3/gists/#get-a-single-gist
134-
func (s *GistsService) Get(id string) (*Gist, error) {
134+
func (s *GistsService) Get(id string) (*Gist, *Response, error) {
135135
u := fmt.Sprintf("gists/%v", id)
136136
req, err := s.client.NewRequest("GET", u, nil)
137137
if err != nil {
138-
return nil, err
138+
return nil, nil, err
139139
}
140140
gist := new(Gist)
141-
_, err = s.client.Do(req, gist)
142-
return gist, err
141+
resp, err := s.client.Do(req, gist)
142+
return gist, resp, err
143143
}
144144

145145
// Create a gist for authenticated user.
146146
//
147147
// GitHub API docs: http://developer.github.com/v3/gists/#create-a-gist
148-
func (s *GistsService) Create(gist *Gist) (*Gist, error) {
148+
func (s *GistsService) Create(gist *Gist) (*Gist, *Response, error) {
149149
u := "gists"
150150
req, err := s.client.NewRequest("POST", u, gist)
151151
if err != nil {
152-
return nil, err
152+
return nil, nil, err
153153
}
154154
g := new(Gist)
155-
_, err = s.client.Do(req, g)
156-
return g, err
155+
resp, err := s.client.Do(req, g)
156+
return g, resp, err
157157
}
158158

159159
// Edit a gist.
160160
//
161161
// GitHub API docs: http://developer.github.com/v3/gists/#edit-a-gist
162-
func (s *GistsService) Edit(id string, gist *Gist) (*Gist, error) {
162+
func (s *GistsService) Edit(id string, gist *Gist) (*Gist, *Response, error) {
163163
u := fmt.Sprintf("gists/%v", id)
164164
req, err := s.client.NewRequest("PATCH", u, gist)
165165
if err != nil {
166-
return nil, err
166+
return nil, nil, err
167167
}
168168
g := new(Gist)
169-
_, err = s.client.Do(req, g)
170-
return g, err
169+
resp, err := s.client.Do(req, g)
170+
return g, resp, err
171171
}
172172

173173
// Delete a gist.
174174
//
175175
// GitHub API docs: http://developer.github.com/v3/gists/#delete-a-gist
176-
func (s *GistsService) Delete(id string) error {
176+
func (s *GistsService) Delete(id string) (*Response, error) {
177177
u := fmt.Sprintf("gists/%v", id)
178178
req, err := s.client.NewRequest("DELETE", u, nil)
179179
if err != nil {
180-
return err
180+
return nil, err
181181
}
182-
_, err = s.client.Do(req, nil)
183-
return err
182+
return s.client.Do(req, nil)
184183
}
185184

186185
// Star a gist on behalf of authenticated user.
187186
//
188187
// GitHub API docs: http://developer.github.com/v3/gists/#star-a-gist
189-
func (s *GistsService) Star(id string) error {
188+
func (s *GistsService) Star(id string) (*Response, error) {
190189
u := fmt.Sprintf("gists/%v/star", id)
191190
req, err := s.client.NewRequest("PUT", u, nil)
192191
if err != nil {
193-
return err
192+
return nil, err
194193
}
195-
_, err = s.client.Do(req, nil)
196-
return err
194+
return s.client.Do(req, nil)
197195
}
198196

199197
// Unstar a gist on a behalf of authenticated user.
200198
//
201199
// Github API docs: http://developer.github.com/v3/gists/#unstar-a-gist
202-
func (s *GistsService) Unstar(id string) error {
200+
func (s *GistsService) Unstar(id string) (*Response, error) {
203201
u := fmt.Sprintf("gists/%v/star", id)
204202
req, err := s.client.NewRequest("DELETE", u, nil)
205203
if err != nil {
206-
return err
204+
return nil, err
207205
}
208-
_, err = s.client.Do(req, nil)
209-
return err
206+
return s.client.Do(req, nil)
210207
}
211208

212209
// Starred checks if a gist is starred by authenticated user.
213210
//
214211
// GitHub API docs: http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
215-
func (s *GistsService) Starred(id string) (bool, error) {
212+
func (s *GistsService) Starred(id string) (bool, *Response, error) {
216213
u := fmt.Sprintf("gists/%v/star", id)
217214
req, err := s.client.NewRequest("GET", u, nil)
218215
if err != nil {
219-
return false, err
216+
return false, nil, err
220217
}
221-
_, err = s.client.Do(req, nil)
222-
return parseBoolResponse(err)
218+
resp, err := s.client.Do(req, nil)
219+
starred, err := parseBoolResponse(err)
220+
return starred, resp, err
223221
}
224222

225223
// Fork a gist.
226224
//
227225
// GitHub API docs: http://developer.github.com/v3/gists/#fork-a-gist
228-
func (s *GistsService) Fork(id string) (*Gist, error) {
226+
func (s *GistsService) Fork(id string) (*Gist, *Response, error) {
229227
u := fmt.Sprintf("gists/%v/forks", id)
230228
req, err := s.client.NewRequest("POST", u, nil)
231229
if err != nil {
232-
return nil, err
230+
return nil, nil, err
233231
}
234232
g := new(Gist)
235-
_, err = s.client.Do(req, g)
236-
return g, err
233+
resp, err := s.client.Do(req, g)
234+
return g, resp, err
237235
}

0 commit comments

Comments
 (0)