go-twitch/api/eventsub/create_subscription.go

49 lines
1.0 KiB
Go
Raw Normal View History

2024-02-27 22:13:57 -05:00
package eventsub
import (
"encoding/json"
"io"
"net/url"
)
type CreateSubscriptionRequest struct {
SubscriptionType
Condition Condition `json:"condition"`
Transport *Transport `json:"transport"`
}
type CreateSubscriptionResponse struct {
Data []*Subscription `json:"data"`
Total int `json:"total"`
TotalCost int `json:"total_cost"`
MaxTotalCost int `json:"max_total_cost"`
}
func (e *EventSub) CreateSubscription(req *CreateSubscriptionRequest) (*CreateSubscriptionResponse, error) {
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "eventsub/subscriptions"})
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(req); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
resp, err := e.client.Post(endpoint.String(), "application/json", r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data CreateSubscriptionResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}