Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client.CreateStreamMarker(params) #17

Merged
merged 3 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ All documentation and usage examples for this package can be found in the [docs
- [x] Get Game Analytics
- [x] Get Streams
- [x] Get Streams Metadata
- [x] Create Stream Marker
- [x] Get Stream Markers
- [x] Get Users
- [x] Get Users Follows
Expand Down
30 changes: 29 additions & 1 deletion docs/stream_markers_docs.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Stream Markers Documentation

## Create Stream Marker

This is an example of how to create a stream marker for a livestream at
the current time. The user needs to be authenticated and requires the
`user:edit:broadcast` scope. Markers cannot be created in [some cases](https://dev.twitch.tv/docs/api/reference/#create-stream-marker).

```go
client, err := helix.NewClient(&helix.Options{
ClientID: "your-client-id",
UserAccessToken: "your-user-access-token",
})

if err != nil {
// handle error
}

resp, err := client.CreateStreamMarker(&helix.CreateStreamMarkerParams{
UserId: "123",
Description: "a notable moment",
})
if err != nil {
// handle error
}

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

## Get Stream Markers

This is an example of how to get stream markers. You can request the stream markers of
Expand All @@ -26,4 +53,5 @@ if err != nil {
}

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

48 changes: 48 additions & 0 deletions stream_markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,51 @@ func (c *Client) GetStreamMarkers(params *StreamMarkersParams) (*StreamMarkersRe

return markers, nil
}

// CreateStreamMarker ...
type CreateStreamMarker struct {
ID string `json:"id"`
CreatedAt Time `json:"created_at"`
Description string `json:"description"`
PositionSeconds int `json:"position_seconds"`
}

// ManyCreateStreamMarkers ...
type ManyCreateStreamMarkers struct {
CreateStreamMarkers []CreateStreamMarker `json:"data"`
}

// CreateStreamMarkerResponse ...
type CreateStreamMarkerResponse struct {
ResponseCommon
Data ManyCreateStreamMarkers
}

// CreateStreamMarkerParams ...
type CreateStreamMarkerParams struct {
UserID string `query:"user_id"`
Description string `query:"description"`
}

// CreateStreamMarker creates a stream marker for a live stream at the current time.
// The user has to be the stream owner or an editor. Stream markers cannot be created
// in some cases, see:
// https://dev.twitch.tv/docs/api/reference/#create-stream-marker
//
// Required Scope: user:edit:broadcast
func (c *Client) CreateStreamMarker(params *CreateStreamMarkerParams) (*CreateStreamMarkerResponse, error) {
resp, err := c.post("/streams/markers", &ManyCreateStreamMarkers{}, params)
if err != nil {
return nil, err
}

markers := &CreateStreamMarkerResponse{}
markers.StatusCode = resp.StatusCode
markers.Header = resp.Header
markers.Error = resp.Error
markers.ErrorStatus = resp.ErrorStatus
markers.ErrorMessage = resp.ErrorMessage
markers.Data.CreateStreamMarkers = resp.Data.(*ManyCreateStreamMarkers).CreateStreamMarkers

return markers, nil
}
58 changes: 58 additions & 0 deletions stream_markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,61 @@ func TestGetStreamMarkers(t *testing.T) {
}
}
}

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

testCases := []struct {
statusCode int
options *Options
userID string
description string
markerCount int
respBody string
}{
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"123",
"a notable moment",
1,
`{"data":[{"id":"123","created_at":"2018-08-20T20:10:03Z","description":"hello, this is a marker!","position_seconds":244}]}`},
{
http.StatusForbidden,
&Options{ClientID: "my-client-id"},
"124",
"another notable moment",
0,
`{"error":"Forbidden","status":403,"message":"Not authorized to create a stream marker for channel test."}`,
},
}

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

resp, err := c.CreateStreamMarker(&CreateStreamMarkerParams{
UserID: testCase.userID,
Description: testCase.description,
})
if err != nil {
t.Error(err)
}

// Test Forbidden Request Responses
if resp.StatusCode == http.StatusForbidden {
firstErrStr := "Not authorized to create a stream marker for channel test."
if resp.ErrorMessage != firstErrStr {
t.Errorf("expected error message to be \"%s\", got \"%s\"", firstErrStr, resp.ErrorMessage)
}
continue
}

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

if len(resp.Data.CreateStreamMarkers) != testCase.markerCount {
t.Errorf("expected \"%d\" stream markers, got \"%d\"", testCase.markerCount, len(resp.Data.CreateStreamMarkers))
}
}
}