Repository: nicklaw5/helix
Pull request #80 (2021-04-17 23:14:42)

Add modify channel information feature


Patch:
diff --git a/README.md b/README.md
index 0c517ac..76f4c12 100644
--- a/README.md
+++ b/README.md
@@ -55,8 +55,8 @@ looking for the Twitch API docs, see the [Twitch Developer website](https://dev.
 - [x] Get Streams
 - [x] Create Stream Marker
 - [x] Get Stream Markers
-- [ ] Get Channel Information
-- [ ] Modify Channel Information
+- [x] Get Channel Information
+- [x] Modify Channel Information
 - [ ] Get Broadcaster Subscriptions
 - [ ] Get All Stream Tags
 - [ ] Get Stream Tags
diff --git a/channels.go b/channels.go
index ac7bfdf..3077600 100644
--- a/channels.go
+++ b/channels.go
@@ -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"`
@@ -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
+}
+
+
diff --git a/docs/channels_docs.md b/docs/channels_docs.md
index 8ab2093..fcec4f0 100644
--- a/docs/channels_docs.md
+++ b/docs/channels_docs.md
@@ -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)
+```
diff --git a/helix.go b/helix.go
index 33bf1ab..2fdd85f 100644
--- a/helix.go
+++ b/helix.go
@@ -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)
 }
@@ -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


Test patch:
diff --git a/channels_test.go b/channels_test.go
index ecf0a63..3511545 100644
--- a/channels_test.go
+++ b/channels_test.go
@@ -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)
+		}
+	}
+}