go-twitch/eventsub/websocket/messages/payloads.go

44 lines
1.0 KiB
Go
Raw Normal View History

2024-03-05 12:14:18 -05:00
package messages
import (
"fmt"
"go.fifitido.net/twitch/api/eventsub"
)
type SessionWelcomePayload struct {
Session Session `json:"session"`
}
type SessionKeepAlivePayload struct{}
type NotificationPayload struct {
Subscription eventsub.Subscription `json:"subscription"`
Event any `json:"event"`
}
type SessionReconnectPayload struct {
Session Session `json:"session"`
}
type RevocationPayload struct {
Subscription eventsub.Subscription `json:"subscription"`
}
func DecodePayload(meta *Metadata, rawPayload map[string]any) (any, error) {
switch meta.MessageType {
case TypeSessionWelcome:
return decode[SessionWelcomePayload](rawPayload)
case TypeSessionKeepAlive:
return SessionKeepAlivePayload{}, nil
case TypeNotification:
return decodeNotificationPayload(rawPayload)
case TypeSessionReconnect:
return decode[SessionReconnectPayload](rawPayload)
case TypeRevocation:
return decode[RevocationPayload](rawPayload)
default:
return nil, fmt.Errorf("unknown message type: %s", meta.MessageType)
}
}