go-twitch/eventsub/eventsub.go

49 lines
920 B
Go
Raw Normal View History

2024-02-27 22:13:57 -05:00
package eventsub
import (
"go.fifitido.net/twitch/api"
"go.fifitido.net/twitch/api/eventsub"
)
type EventSub struct {
api *api.API
transport *eventsub.Transport
subscriptions map[string]*eventsub.Subscription
}
func New(api *api.API, trans *eventsub.Transport) *EventSub {
return &EventSub{
api: api,
transport: trans,
}
}
func (e *EventSub) Subscribe(subType eventsub.SubscriptionType, cond eventsub.Condition) error {
res, err := e.api.EventSub.CreateSubscription(&eventsub.CreateSubscriptionRequest{
SubscriptionType: subType,
Condition: cond,
Transport: e.transport,
})
if err != nil {
return err
}
for _, sub := range res.Data {
e.subscriptions[sub.ID] = sub
}
return nil
}
func (e *EventSub) Close() error {
for _, sub := range e.subscriptions {
if err := e.api.EventSub.DeleteSubscription(sub.ID); err != nil {
return err
}
}
return nil
}