2024-02-27 22:13:57 -05:00
|
|
|
package eventsub
|
|
|
|
|
|
|
|
import (
|
2024-02-27 23:03:40 -05:00
|
|
|
"context"
|
|
|
|
|
2024-02-27 22:13:57 -05:00
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 23:03:40 -05:00
|
|
|
func (e *EventSub) Subscribe(ctx context.Context, subType eventsub.SubscriptionType, cond eventsub.Condition) error {
|
|
|
|
res, err := e.api.EventSub.CreateEventSubSubscription(ctx, &eventsub.CreateEventSubSubscriptionRequest{
|
2024-02-27 22:13:57 -05:00
|
|
|
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 {
|
2024-02-27 23:03:40 -05:00
|
|
|
if err := e.api.EventSub.DeleteEventSubSubscription(context.Background(), sub.ID); err != nil {
|
2024-02-27 22:13:57 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|