go-twitch/api/eventsub/get_eventsub_subscriptions.go

87 lines
3.1 KiB
Go
Raw Normal View History

package eventsub
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetEventSubSubscriptionsParams struct {
// Filter subscriptions by its status.
Status *Status `url:"status,omitempty"`
// Filter subscriptions by subscription type. For a list of subscription types.
// See Subscription Types: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types#subscription-types
Type *SubscriptionType `url:"type,omitempty"`
// Filter subscriptions by user ID.
// The response contains subscriptions where this ID matches a user ID that you specified in the Condition object when you created the subscription.
UserID *string `url:"user_id,omitempty"`
// The cursor used to get the next page of results.
// The pagination object in the response contains the cursor's value.
After *types.Cursor `url:"after,omitempty"`
}
type GetEventSubSubscriptionsResponse struct {
// The list of subscriptions. The list is ordered by the oldest subscription first.
// The list is empty if the client hasn't created subscriptions or there are no subscriptions that match the specified filter criteria.
Data []Subscription `json:"data"`
// The total number of subscriptions that 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 that you create.
MaxTotalCost int `json:"max_total_cost"`
// An object that contains the cursor used to get the next page of subscriptions.
// The object is empty if there are no more pages to get.
// The number of subscriptions returned per page is undertermined.
Pagination types.Pagination `json:"pagination"`
}
// Gets a list of EventSub subscriptions that the client in the access token created.
//
// 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 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. The token may include any scopes.
func (e *EventSub) GetEventSubSubscriptions(ctx context.Context, params *GetEventSubSubscriptionsParams) (*GetEventSubSubscriptionsResponse, error) {
v, _ := query.Values(params)
endpoint := e.baseUrl.ResolveReference(&url.URL{Path: "eventsub/subscriptions", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get EventSub subscriptions (%d)", res.StatusCode)
}
var data GetEventSubSubscriptionsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}