Add Raids endpoints to API

This commit is contained in:
Evan Fiordeliso 2024-03-03 16:00:12 -05:00
parent 9eed838ead
commit 7b2f1910ab
4 changed files with 119 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import (
"go.fifitido.net/twitch/api/moderation" "go.fifitido.net/twitch/api/moderation"
"go.fifitido.net/twitch/api/polls" "go.fifitido.net/twitch/api/polls"
"go.fifitido.net/twitch/api/predictions" "go.fifitido.net/twitch/api/predictions"
"go.fifitido.net/twitch/api/raids"
) )
const HelixBaseUrl = "https://api.twitch.tv/helix" const HelixBaseUrl = "https://api.twitch.tv/helix"
@ -50,6 +51,7 @@ type API struct {
Moderation *moderation.Moderation Moderation *moderation.Moderation
Polls *polls.Polls Polls *polls.Polls
Predictions *predictions.Predictions Predictions *predictions.Predictions
Raids *raids.Raids
} }
func New() *API { func New() *API {
@ -79,5 +81,6 @@ func New() *API {
Moderation: moderation.New(client, baseUrl), Moderation: moderation.New(client, baseUrl),
Polls: polls.New(client, baseUrl), Polls: polls.New(client, baseUrl),
Predictions: predictions.New(client, baseUrl), Predictions: predictions.New(client, baseUrl),
Raids: raids.New(client, baseUrl),
} }
} }

View File

@ -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
}

18
api/raids/raids.go Normal file
View File

@ -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,
}
}

65
api/raids/start_a_raid.go Normal file
View File

@ -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 thats 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 broadcasters 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
}