Skip to content

Commit

Permalink
Add GetChannelInformation endpoint (#75)
Browse files Browse the repository at this point in the history
* Added get channel information

* Fixed formatting

* Cleaned up formatting and spellchecks

* Update channels.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

* Update channels_test.go

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>

Co-authored-by: Nick Law <nick@nicholaslaw.com.au>
  • Loading branch information
codedninja and nicklaw5 committed Feb 26, 2021
1 parent 54d4eee commit 34b336c
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 29 deletions.
6 changes: 5 additions & 1 deletion ads.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package helix

// AdLengthEnum ...
type AdLengthEnum int

// AdLen30 ...
const (
_ AdLengthEnum = iota * 30
AdLen30
Expand All @@ -18,17 +20,19 @@ type StartCommercialParams struct {
Length AdLengthEnum `query:"length"`
}

// AdDetails ...
type AdDetails struct {
Length AdLengthEnum `json:"length"`
Message string `json:"message"`
RetryAfter int `json:"retry_after"`
}

// ManyAdDetails ...
type ManyAdDetails struct {
AdDetails []AdDetails `json:"data"`
}

// StartCommercialResponse
// StartCommercialResponse ...
type StartCommercialResponse struct {
ResponseCommon
Data ManyAdDetails
Expand Down
4 changes: 2 additions & 2 deletions ads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestClient_StartCommercial(t *testing.T) {
statusCode int
options *Options
count int
broadcasterId string
broadcasterID string
adLength AdLengthEnum
respBody string
expectedErrMsg string
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestClient_StartCommercial(t *testing.T) {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))

params := &StartCommercialParams{
BroadcasterID: testCase.broadcasterId,
BroadcasterID: testCase.broadcasterID,
Length: testCase.adLength,
}

Expand Down
9 changes: 9 additions & 0 deletions bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func (c *Client) GetBitsLeaderboard(params *BitsLeaderboardParams) (*BitsLeaderb
return bits, nil
}

// CheermotesParams ...
type CheermotesParams struct {
BroadcasterID string `query:"broadcaster_id"` // optional
}

// TierImages ...
type TierImages struct {
Image1 string `json:"1"`
Image1_5 string `json:"1.5"`
Expand All @@ -63,16 +65,19 @@ type TierImages struct {
Image4 string `json:"4"`
}

// TierImageTypes ...
type TierImageTypes struct {
Animated TierImages `json:"animated"`
Static TierImages `json:"static"`
}

// CheermoteTierImages ...
type CheermoteTierImages struct {
Dark TierImageTypes `json:"dark"`
Light TierImageTypes `json:"light"`
}

// CheermoteTiers ...
type CheermoteTiers struct {
MinBits uint `json:"min_bits"`
ID string `json:"id"`
Expand All @@ -82,6 +87,7 @@ type CheermoteTiers struct {
ShowInBitsCard bool `json:"show_in_bits_card"`
}

// Cheermotes ...
type Cheermotes struct {
Prefix string `json:"prefix"`
Tiers []CheermoteTiers `json:"tiers"`
Expand All @@ -91,15 +97,18 @@ type Cheermotes struct {
IsCharitable bool `json:"is_charitable"`
}

// ManyCheermotes ...
type ManyCheermotes struct {
Cheermotes []Cheermotes `json:"data"`
}

// CheermotesResponse ...
type CheermotesResponse struct {
ResponseCommon
Data ManyCheermotes
}

// GetCheermotes ...
func (c *Client) GetCheermotes(params *CheermotesParams) (*CheermotesResponse, error) {
resp, err := c.get("/bits/cheermotes", &ManyCheermotes{}, params)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,43 @@ func (c *Client) SearchChannels(params *SearchChannelsParams) (*SearchChannelsRe

return channels, nil
}

// GetChannelInformationParams ...
type GetChannelInformationParams struct {
BroadcasterID string `query:"broadcaster_id"`
}

// GetChannelInformationResponse ...
type GetChannelInformationResponse struct {
ResponseCommon
Data ManyChannelInformation
}

// ManyChannelInformation ...
type ManyChannelInformation struct {
Channels []ChannelInformation `json:"data"`
}

// ChannelInformation ...
type ChannelInformation struct {
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
BroadcasterLanguage string `json:"broadcaster_language"`
GameID string `json:"game_id"`
GameName string `json:"game_name"`
Title string `json:"title"`
}

// GetChannelInformation ...
func (c *Client) GetChannelInformation(params *GetChannelInformationParams) (*GetChannelInformationResponse, error) {
resp, err := c.get("/channels", &ManyChannelInformation{}, params)
if err != nil {
return nil, err
}

channels := &GetChannelInformationResponse{}
resp.HydrateResponseCommon(&channels.ResponseCommon)
channels.Data.Channels = resp.Data.(*ManyChannelInformation).Channels

return channels, nil
}
90 changes: 88 additions & 2 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSearchChannels(t *testing.T) {
2,
`{"data":[{"broadcaster_language":"en","display_name":"Ninja","game_id":"33214","id":"27833742640","is_live":false,"tag_ids":[],"thumbnail_url":"https://static-cdn.jtvnw.net/previews-ttv/live_user_ninja-{width}x{height}.jpg","title":"I have lost my voice D: | twitter.com/Ninja","started_at":"2018-03-06T15:07:45Z"},{"broadcaster_language":"en","display_name":"DrDisrespect","game_id":"33214","id":"27834185424","is_live":false,"tag_ids":[],"thumbnail_url":"https://static-cdn.jtvnw.net/previews-ttv/live_user_drdisrespectlive-{width}x{height}.jpg","title":"Turbo Treehouses || @DrDisRespect","started_at":"2018-03-06T16:05:00Z"}],"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6Mn19"}}`,
[]Channel{
Channel{
{
ID: "27833742640",
GameID: "33214",
BroadcasterLogin: "ninja",
Expand All @@ -32,7 +32,7 @@ func TestSearchChannels(t *testing.T) {
IsLive: false,
TagIDs: []string{},
},
Channel{
{
ID: "27834185424",
GameID: "33214",
BroadcasterLogin: "drdisrespect",
Expand Down Expand Up @@ -109,3 +109,89 @@ func TestSearchChannels(t *testing.T) {
}
}
}

func TestGetChannelInformation(t *testing.T) {
t.Parallel()

testCases := []struct {
statusCode int
options *Options
BroadcasterID string
respBody string
parsed []ChannelInformation
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"44445592",
`{"data":[{"broadcaster_id":"44445592","broadcaster_login":"pokimane","broadcaster_name":"pokimane","broadcaster_language":"en","game_id":"509658","game_name":"Just Chatting","title":"See you Wednesday 8am for Among Us ^_^"}]}`,
[]ChannelInformation{
{
BroadcasterID: "44445592",
BroadcasterName: "pokimane",
BroadcasterLanguage: "en",
GameID: "509658",
GameName: "Just Chatting",
Title: "See you Wednesday 8am for Among Us ^_^",
},
},
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
"9999999999999999999",
`{"error":"Bad Request","status":400,"message":"Invalid broadcasterID"}`,
[]ChannelInformation{},
},
}

for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))

resp, err := c.GetChannelInformation(&GetChannelInformationParams{
BroadcasterID: testCase.BroadcasterID,
})
if err != nil {
t.Error(err)
}

// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
broadcasterIDErrStr := "Invalid broadcasterID"
if resp.ErrorMessage != broadcasterIDErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", broadcasterIDErrStr, resp.ErrorMessage)
continue
}
}

if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}

for i, channel := range resp.Data.Channels {
if channel.BroadcasterID != testCase.parsed[i].BroadcasterID {
t.Errorf("Expected struct field BroadcasterID = %s, was %s", testCase.parsed[i].BroadcasterID, channel.BroadcasterID)
}

if channel.BroadcasterName != testCase.parsed[i].BroadcasterName {
t.Errorf("Expected struct field BroadcasterName = %s, was %s", testCase.parsed[i].BroadcasterName, channel.BroadcasterName)
}

if channel.BroadcasterLanguage != testCase.parsed[i].BroadcasterLanguage {
t.Errorf("Expected struct field BroadcasterLanguage = %s, was %s", testCase.parsed[i].BroadcasterLanguage, channel.BroadcasterLanguage)
}

if channel.GameID != testCase.parsed[i].GameID {
t.Errorf("Expected struct field GameID = %s, was %s", testCase.parsed[i].GameID, channel.GameID)
}

if channel.GameName != testCase.parsed[i].GameName {
t.Errorf("Expected struct field GameName = %s, was %s", testCase.parsed[i].GameName, channel.GameName)
}

if channel.Title != testCase.parsed[i].Title {
t.Errorf("Expected struct field Title = %s, was %s", testCase.parsed[i].Title, channel.Title)
}
}
}
}
20 changes: 10 additions & 10 deletions drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package helix

// GetDropEntitlementsParams ...
type GetDropEntitlementsParams struct {
ID string `query:"id"`
UserID string `query:"user_id"`
GameID string `query:"game_id"`
After string `query:"after"`
First int `query:"first,20"` // Limit 100
ID string `query:"id"`
UserID string `query:"user_id"`
GameID string `query:"game_id"`
After string `query:"after"`
First int `query:"first,20"` // Limit 100
}

// Entitlement ...
type Entitlement struct {
ID string `json:"id"`
BenefitID string `json:"benefit_id"`
Timestamp Time `json:"timestamp"`
UserID string `json:"user_id"`
GameID string `json:"game_id"`
ID string `json:"id"`
BenefitID string `json:"benefit_id"`
Timestamp Time `json:"timestamp"`
UserID string `json:"user_id"`
GameID string `json:"game_id"`
}

// ManyEntitlements ...
Expand Down
10 changes: 5 additions & 5 deletions drops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func TestGetDropsEntitlements(t *testing.T) {
t.Parallel()

testCases := []struct {
statusCode int
options *Options
gameID string
respBody string
expectedErrMsg string
statusCode int
options *Options
gameID string
respBody string
expectedErrMsg string
}{
{
http.StatusUnauthorized,
Expand Down
6 changes: 4 additions & 2 deletions entitlement_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type CodeResponse struct {
Data ManyCodes
}

/**
/*
GetEntitlementCodeStatus ...
Per https://dev.twitch.tv/docs/api/reference#get-code-status
Access is controlled via an app access token on the calling service. The client ID associated with the app access token must be approved by Twitch as part of a contracted arrangement.
Callers with an app access token are authorized to redeem codes on behalf of any Twitch user account.
Expand All @@ -56,7 +57,8 @@ func (c *Client) GetEntitlementCodeStatus(params *CodesParams) (*CodeResponse, e
return codes, nil
}

/**
/*
RedeemEntitlementCode ...
Per https://dev.twitch.tv/docs/api/reference/#redeem-code
Access is controlled via an app access token on the calling service. The client ID associated with the app access token must be approved by Twitch.
Callers with an app access token are authorized to redeem codes on behalf of any Twitch user account.
Expand Down
8 changes: 4 additions & 4 deletions entitlement_codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestClient_GetEntitlementCodeStatus(t *testing.T) {
statusCode int
options *Options
count int
userId string
userID string
codes []string
respBody string
}{
Expand All @@ -31,7 +31,7 @@ func TestClient_GetEntitlementCodeStatus(t *testing.T) {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))

params := &CodesParams{
UserID: testCase.userId,
UserID: testCase.userID,
Codes: testCase.codes,
}

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestClient_RedeemEntitlementCode(t *testing.T) {
statusCode int
options *Options
count int
userId string
userID string
codes []string
respBody string
}{
Expand All @@ -87,7 +87,7 @@ func TestClient_RedeemEntitlementCode(t *testing.T) {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))

params := &CodesParams{
UserID: testCase.userId,
UserID: testCase.userID,
Codes: testCase.codes,
}

Expand Down
2 changes: 1 addition & 1 deletion moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BannedUsersResponse struct {
Data ManyBans
}

// BannedUsersResponse
// BannedUsersParams ...
// BroadcasterID must match the auth tokens user_id
type BannedUsersParams struct {
BroadcasterID string `query:"broadcaster_id"`
Expand Down
2 changes: 1 addition & 1 deletion user_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestUpdateUserExtensions(t *testing.T) {
&Options{ClientID: "my-client-id", UserAccessToken: "my-access-token"},
&UpdateUserExtensionsPayload{
Panel: map[string]UserActiveExtensionInfo{
"1": UserActiveExtensionInfo{
"1": {
Active: false,
},
},
Expand Down
1 change: 1 addition & 0 deletions user_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (c *Client) GetUserActiveExtensions(params *UserActiveExtensionsParams) (*U
return userActiveExtensions, nil
}

// UpdateUserExtensionsPayload ...
type UpdateUserExtensionsPayload struct {
Component map[string]UserActiveExtensionInfo `json:"component,omitempty"`
Overlay map[string]UserActiveExtensionInfo `json:"overlay,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestPostWebhookSubscriptions(t *testing.T) {
}

func newMockWebhookRequest(header string) *http.Request {
return &http.Request{Header: map[string][]string{"Link": []string{header}}}
return &http.Request{Header: map[string][]string{"Link": {header}}}
}

func TestGetWebhookTopicFromRequest(t *testing.T) {
Expand Down

0 comments on commit 34b336c

Please sign in to comment.