Skip to content

Commit

Permalink
Merge branch 'master' into dev/scorfly/checkUserSubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Scorfly committed Apr 18, 2021
2 parents a596d38 + 655c476 commit 1bc37f4
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
29 changes: 29 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ type GetChannelInformationParams struct {
BroadcasterID string `query:"broadcaster_id"`
}

// EditChannelInformationParams ...
type EditChannelInformationParams struct {
BroadcasterID string `query:"broadcaster_id" json:"-"`
GameID string `json:"game_id"`
BroadcasterLanguage string `json:"broadcaster_language"`
Title string `json:"title"`
Delay int `json:"delay,omitempty"`
}

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

// EditChannelInformationResponse ...
type EditChannelInformationResponse struct {
ResponseCommon
}

// ManyChannelInformation ...
type ManyChannelInformation struct {
Channels []ChannelInformation `json:"data"`
Expand Down Expand Up @@ -90,3 +104,18 @@ func (c *Client) GetChannelInformation(params *GetChannelInformationParams) (*Ge

return channels, nil
}

// EditChannelInformation ...
func (c *Client) EditChannelInformation(params *EditChannelInformationParams) (*EditChannelInformationResponse, error) {
resp, err := c.patchAsJSON("/channels", &EditChannelInformationResponse{}, params)
if err != nil {
return nil, err
}

channels := &EditChannelInformationResponse{}
resp.HydrateResponseCommon(&channels.ResponseCommon)

return channels, nil
}


73 changes: 73 additions & 0 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,76 @@ func TestGetChannelInformation(t *testing.T) {
}
}
}

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

testCases := []struct {
statusCode int
options *Options
params *EditChannelInformationParams
respBody string
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "123",
GameID: "456",
BroadcasterLanguage: "en",
Title: "Test title",
},
`{"data":[{"game_id":"498566","broadcaster_language":"en","title":"Test Twitch API"}]}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "789",
GameID: "456",
BroadcasterLanguage: "en",
Title: "Test title",
Delay: 3,
},
`{"error":"Bad Request","status":400,"message":"the broadcaster is not partnered, failed to set delay"}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EditChannelInformationParams{
BroadcasterID: "789",
GameID: "-1",
BroadcasterLanguage: "en",
Title: "Test title",
},
`{"error":"Bad Request","status":400,"message":"invalid game_id"}`,
},
}

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

resp, err := c.EditChannelInformation(testCase.params)
if err != nil {
t.Error(err)
}

// Test Bad Request Responses
if resp.StatusCode == http.StatusBadRequest {
broadcasterIDErrStr := "the broadcaster is not partnered, failed to set delay"

if testCase.params.GameID == "-1" {
broadcasterIDErrStr = "invalid game_id"
}

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)
}
}
}
49 changes: 49 additions & 0 deletions docs/channels_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,52 @@ if err != nil {

fmt.Printf("%+v\n", resp)
```

## Get Channel Information

This is an example of how to get channel informations.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterID: "123456",
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```

## Modify Channel Information

This is an example of how to modify channel informations.
The `Delay` param is a Twitch Partner feature.

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
})
if err != nil {
// handle error
}

resp, err := client.GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterID : "123456",
GameID : "456789",
BroadcasterLanguage : "en",
Title : "Your stream title",
Delay : 0,
})
if err != nil {
// handle error
}

fmt.Printf("%+v\n", resp)
```
11 changes: 11 additions & 0 deletions helix.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (c *Client) delete(path string, respData, reqData interface{}) (*Response,
return c.sendRequest(http.MethodDelete, path, respData, reqData, false)
}

func (c *Client) patchAsJSON(path string, respData, reqData interface{}) (*Response, error) {
return c.sendRequest(http.MethodPatch, path, respData, reqData, true)
}

func (c *Client) postAsJSON(path string, respData, reqData interface{}) (*Response, error) {
return c.sendRequest(http.MethodPost, path, respData, reqData, true)
}
Expand Down Expand Up @@ -295,6 +299,13 @@ func (c *Client) newJSONRequest(method, url string, data interface{}) (*http.Req
return nil, err
}

query, err := buildQueryString(req, data)
if err != nil {
return nil, err
}

req.URL.RawQuery = query

req.Header.Set("Content-Type", "application/json")

return req, nil
Expand Down

0 comments on commit 1bc37f4

Please sign in to comment.