-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
twitch/eventsub: add stream.online and stream.offline payload
For #58.
- Loading branch information
1 parent
06f344e
commit 586a585
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package eventsub | ||
|
||
// StreamOnline is the payload for a stream.online or stream.offline notification. | ||
type Stream struct { | ||
// ID is the stream ID. | ||
// Only present in stream.online messages. | ||
ID string `json:"id"` | ||
// Broadcaster is the broadcaster's user ID. | ||
Broadcaster string `json:"broadcaster_user_id"` | ||
// BroadcasterLogin is the broadcaster's user login. | ||
BroadcasterLogin string `json:"broadcaster_user_login"` | ||
// BoradcasterName is the broadcaster's display name. | ||
BroadcasterName string `json:"broadcaster_user_name"` | ||
// Type is the stream type, usually "live". | ||
Type string `json:"type"` | ||
// Started is the time at which the stream started in RFC3339Nano format. | ||
Started string `json:"started_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package eventsub_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-json-experiment/json" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/zephyrtronium/robot/twitch/eventsub" | ||
) | ||
|
||
func TestStream(t *testing.T) { | ||
t.Run("online", func(t *testing.T) { | ||
evt := Testdata("stream.online.json") | ||
var got eventsub.Stream | ||
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil { | ||
t.Errorf("couldn't unmarshal payload as stream: %v", err) | ||
} | ||
want := eventsub.Stream{ | ||
ID: "9001", | ||
Broadcaster: "1337", | ||
BroadcasterLogin: "cool_user", | ||
BroadcasterName: "Cool_User", | ||
Type: "live", | ||
Started: "2020-10-11T10:11:12.123Z", | ||
} | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("wrong stream (+got/-want):\n%s", diff) | ||
} | ||
}) | ||
t.Run("offline", func(t *testing.T) { | ||
evt := Testdata("stream.offline.json") | ||
var got eventsub.Stream | ||
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil { | ||
t.Errorf("couldn't unmarshal payload as stream: %v", err) | ||
} | ||
want := eventsub.Stream{ | ||
Broadcaster: "1337", | ||
BroadcasterLogin: "cool_user", | ||
BroadcasterName: "Cool_User", | ||
} | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("wrong stream (+got/-want):\n%s", diff) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"subscription": { | ||
"id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4", | ||
"type": "stream.offline", | ||
"version": "1", | ||
"status": "enabled", | ||
"cost": 0, | ||
"condition": { | ||
"broadcaster_user_id": "1337" | ||
}, | ||
"created_at": "2019-11-16T10:11:12.634234626Z", | ||
"transport": { | ||
"method": "webhook", | ||
"callback": "https://example.com/webhooks/callback" | ||
} | ||
}, | ||
"event": { | ||
"broadcaster_user_id": "1337", | ||
"broadcaster_user_login": "cool_user", | ||
"broadcaster_user_name": "Cool_User" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"subscription": { | ||
"id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4", | ||
"type": "stream.online", | ||
"version": "1", | ||
"status": "enabled", | ||
"cost": 0, | ||
"condition": { | ||
"broadcaster_user_id": "1337" | ||
}, | ||
"transport": { | ||
"method": "webhook", | ||
"callback": "https://example.com/webhooks/callback" | ||
}, | ||
"created_at": "2019-11-16T10:11:12.634234626Z" | ||
}, | ||
"event": { | ||
"id": "9001", | ||
"broadcaster_user_id": "1337", | ||
"broadcaster_user_login": "cool_user", | ||
"broadcaster_user_name": "Cool_User", | ||
"type": "live", | ||
"started_at": "2020-10-11T10:11:12.123Z" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package eventsub_test | ||
|
||
import ( | ||
"embed" | ||
|
||
"github.com/go-json-experiment/json" | ||
|
||
"github.com/zephyrtronium/robot/twitch/eventsub" | ||
) | ||
|
||
//go:embed testdata | ||
var testdata embed.FS | ||
|
||
func Testdata(name string) *eventsub.Event { | ||
b, err := testdata.ReadFile("testdata/" + name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var r eventsub.Event | ||
if err := json.Unmarshal(b, &r); err != nil { | ||
panic(err) | ||
} | ||
return &r | ||
} |