Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Support eager loading on broadcast endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa committed May 2, 2019
1 parent 4c4e25f commit a7d307a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions broadcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ type Broadcast struct {
type BroadcastsOption struct {
// Filters broadcasts by whether or not the broadcast should still be displayed
Active bool `url:"active"`
// List of attributes to eager load
Include []string `url:"include,omitempty,comma"`
}

// getBroadcastsResponse represents a response
// broadcastsResponse represents a response
// from broadcast endpoints
type getBroadcastsResponse struct {
type broadcastsResponse struct {
Broadcasts []*Broadcast `json:"broadcasts,omitempty"`
}

Expand All @@ -61,11 +63,11 @@ func (bs *BroadcastsService) List(ctx context.Context, opt *BroadcastsOption) ([
return nil, nil, err
}

var getBroadcastsResponse getBroadcastsResponse
resp, err := bs.client.Do(ctx, req, &getBroadcastsResponse)
var br broadcastsResponse
resp, err := bs.client.Do(ctx, req, &br)
if err != nil {
return nil, resp, err
}

return getBroadcastsResponse.Broadcasts, resp, err
return br.Broadcasts, resp, err
}
2 changes: 1 addition & 1 deletion broadcasts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestBroadcastsService_Integration_List(t *testing.T) {
cases := []*BroadcastsOption{
nil,
{Active: true},
{Active: false},
{Active: false, Include: []string{"broadcast.recipient"}},
}

for i, opt := range cases {
Expand Down
4 changes: 2 additions & 2 deletions broadcasts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestBroadcastsService_List(t *testing.T) {

mux.HandleFunc("/broadcasts", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testFormValues(t, r, values{"active": "true"})
testFormValues(t, r, values{"active": "true", "include": "broadcast.recipient"})
fmt.Fprint(w, `{"broadcasts": [{"id":125,"message":"We just switched the default image for","active":true,"created_at":"2014-11-19T14:39:51Z"}]}`)
})

broadcasts, _, err := client.Broadcasts.List(context.Background(), &BroadcastsOption{Active: true})
broadcasts, _, err := client.Broadcasts.List(context.Background(), &BroadcastsOption{Active: true, Include: []string{"broadcast.recipient"}})

if err != nil {
t.Errorf("Broadcasts.List returned error: %v", err)
Expand Down

0 comments on commit a7d307a

Please sign in to comment.