From 7b2f1910ab04a7dcd7bbfb1e3494383e4303b7ff Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Sun, 3 Mar 2024 16:00:12 -0500 Subject: [PATCH] Add Raids endpoints to API --- api/api.go | 3 ++ api/raids/cancel_a_raid.go | 33 +++++++++++++++++++ api/raids/raids.go | 18 +++++++++++ api/raids/start_a_raid.go | 65 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 api/raids/cancel_a_raid.go create mode 100644 api/raids/raids.go create mode 100644 api/raids/start_a_raid.go diff --git a/api/api.go b/api/api.go index df13f66..219b840 100644 --- a/api/api.go +++ b/api/api.go @@ -23,6 +23,7 @@ import ( "go.fifitido.net/twitch/api/moderation" "go.fifitido.net/twitch/api/polls" "go.fifitido.net/twitch/api/predictions" + "go.fifitido.net/twitch/api/raids" ) const HelixBaseUrl = "https://api.twitch.tv/helix" @@ -50,6 +51,7 @@ type API struct { Moderation *moderation.Moderation Polls *polls.Polls Predictions *predictions.Predictions + Raids *raids.Raids } func New() *API { @@ -79,5 +81,6 @@ func New() *API { Moderation: moderation.New(client, baseUrl), Polls: polls.New(client, baseUrl), Predictions: predictions.New(client, baseUrl), + Raids: raids.New(client, baseUrl), } } diff --git a/api/raids/cancel_a_raid.go b/api/raids/cancel_a_raid.go new file mode 100644 index 0000000..9ffff5b --- /dev/null +++ b/api/raids/cancel_a_raid.go @@ -0,0 +1,33 @@ +package raids + +import ( + "context" + "net/http" + "net/url" +) + +// Cancel a pending raid. +// +// You can cancel a raid at any point up until the broadcaster clicks Raid Now in the Twitch UX or the 90-second countdown expires. +// +// Rate Limit: The limit is 10 requests within a 10-minute window. +// +// Requires a user access token that includes the channel:manage:raids scope. +// +// The broadcaster ID must match the user ID in the user access token. +func (c *Raids) CancelARaid(ctx context.Context, broadcasterID string) error { + endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "raids", RawQuery: url.Values{"broadcaster_id": {broadcasterID}}.Encode()}) + + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, endpoint.String(), nil) + if err != nil { + return err + } + + res, err := c.client.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + + return nil +} diff --git a/api/raids/raids.go b/api/raids/raids.go new file mode 100644 index 0000000..876853f --- /dev/null +++ b/api/raids/raids.go @@ -0,0 +1,18 @@ +package raids + +import ( + "net/http" + "net/url" +) + +type Raids struct { + client *http.Client + baseUrl *url.URL +} + +func New(client *http.Client, baseUrl *url.URL) *Raids { + return &Raids{ + client: client, + baseUrl: baseUrl, + } +} diff --git a/api/raids/start_a_raid.go b/api/raids/start_a_raid.go new file mode 100644 index 0000000..88b4915 --- /dev/null +++ b/api/raids/start_a_raid.go @@ -0,0 +1,65 @@ +package raids + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + + "github.com/google/go-querystring/query" +) + +type StartARaidParams struct { + // The ID of the broadcaster that’s sending the raiding party. This ID must match the user ID in the user access token. + FromBroadcasterID string `url:"from_broadcaster_id"` + + // The ID of the broadcaster to raid. + ToBroadcasterID string `url:"to_broadcaster_id"` +} + +type StartARaidResponse struct { + // A list that contains a single object with information about the pending raid. + Data []struct { + // The UTC date and time, in RFC3339 format, of when the raid was requested. + CreatedAt string `json:"created_at"` + // A Boolean value that indicates whether the channel being raided contains mature content. + IsMature bool `json:"is_mature"` + } `json:"data"` +} + +// Raid another channel by sending the broadcaster’s viewers to the targeted channel. +// +// When you call the API from a chat bot or extension, +// the Twitch UX pops up a window at the top of the chat room that identifies the number of viewers in the raid. +// The raid occurs when the broadcaster clicks Raid Now or after the 90-second countdown expires. +// +// To determine whether the raid successfully occurred, you must subscribe to the Channel Raid event. +// For more information, see Get notified when a raid begins: https://dev.twitch.tv/docs/api/raids#get-notified-when-a-raid-begins +// +// To cancel a pending raid, use the Cancel a raid endpoint. +// +// Rate Limit: The limit is 10 requests within a 10-minute window. +// +// Requires a user access token that includes the channel:manage:raids scope. +func (c *Raids) StartARaid(ctx context.Context, params *StartARaidParams) (*StartARaidResponse, error) { + v, _ := query.Values(params) + endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "raids", RawQuery: v.Encode()}) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), nil) + if err != nil { + return nil, err + } + + res, err := c.client.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + + var data StartARaidResponse + if err := json.NewDecoder(res.Body).Decode(&data); err != nil { + return nil, err + } + + return &data, nil +}