go-twitch/api/eventsub/create_eventsub_subscriptio...

90 lines
3.1 KiB
Go
Raw Permalink Normal View History

package eventsub
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
)
type CreateEventSubSubscriptionRequest struct {
// The type of subscription to create.
SubscriptionType
// A JSON object that contains the parameter values that are specific to the specified subscription type.
//For the objects required and optional fields, see the subscription types documentation.
Condition Condition `json:"condition"`
// The transport details that you want Twitch to use when sending you notifications.
Transport *Transport `json:"transport"`
}
type CreateEventSubSubscriptionResponse struct {
// A list that contains the single subscription that you created.
Data []*Subscription `json:"data"`
// The total number of subscriptions youve created.
Total int `json:"total"`
// The sum of all of your subscription costs. Learn More: https://dev.twitch.tv/docs/eventsub/manage-subscriptions/#subscription-limits
TotalCost int `json:"total_cost"`
// The maximum total cost that youre allowed to incur for all subscriptions you create.
MaxTotalCost int `json:"max_total_cost"`
}
// Creates an EventSub subscription.
//
// If you use webhooks to receive events, the request must specify an app access token.
// The request will fail if you use a user access token. If the subscription type requires user authorization,
// the user must have granted your app (client ID) permissions to receive those events before you subscribe to them.
// For example, to subscribe to channel.subscribe events, your app must get a user access token that includes the
// channel:read:subscriptions scope, which adds the required permission to your app access tokens client ID.
//
// If you use WebSockets to receive events, the request must specify a user access token.
// The request will fail if you use an app access token. If the subscription type requires user authorization,
// the token must include the required scope. However, if the subscription type doesnt include user authorization,
// the token may include any scopes or no scopes.
//
// If you use Conduits to receive events, the request must specify an app access token.
// The request will fail if you use a user access token.
func (e *EventSub) CreateEventSubSubscription(ctx context.Context, body *CreateEventSubSubscriptionRequest) (*CreateEventSubSubscriptionResponse, error) {
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Make(e.baseUrl, "eventsub/subscriptions"), r)
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to create EventSub subscription (%d)", res.StatusCode)
}
var data CreateEventSubSubscriptionResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}