84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package eventsub
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/url"
|
||
)
|
||
|
||
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 object’s required and optional fields, see the subscription type’s 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 you’ve 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 you’re 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 token’s 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 doesn’t 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) {
|
||
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "eventsub/subscriptions"})
|
||
|
||
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.String(), r)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := e.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
defer res.Body.Close()
|
||
|
||
var data CreateEventSubSubscriptionResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|