90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package subscriptions
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
type CheckUserSubscriptionParams struct {
|
||
// The ID of a partner or affiliate broadcaster.
|
||
BroadcasterID string `url:"broadcaster_id"`
|
||
|
||
// The ID of the user that you’re checking to see whether they subscribe to the broadcaster in broadcaster_id.
|
||
// This ID must match the user ID in the access Token.
|
||
UserID string `url:"user_id"`
|
||
}
|
||
|
||
type CheckUserSubscriptionResponse struct {
|
||
// A list that contains a single object with information about the user’s subscription.
|
||
Data []UserSubscription `json:"data"`
|
||
}
|
||
|
||
type UserSubscription struct {
|
||
// An ID that identifies the broadcaster.
|
||
BroadcasterID string `json:"broadcaster_id"`
|
||
|
||
// The broadcaster’s login name.
|
||
BroadcasterLogin string `json:"broadcaster_login"`
|
||
|
||
// The broadcaster’s display name.
|
||
BroadcasterName string `json:"broadcaster_name"`
|
||
|
||
// The ID of the user that gifted the subscription to the user. The object includes this field only if is_gift is true.
|
||
GifterID *string `json:"gifter_id"`
|
||
|
||
// The gifter’s login name. The object includes this field only if is_gift is true.
|
||
GifterLogin *string `json:"gifter_login"`
|
||
|
||
// The gifter’s display name. The object includes this field only if is_gift is true.
|
||
GifterName *string `json:"gifter_name"`
|
||
|
||
// A Boolean value that determines whether the subscription is a gift subscription. Is true if the subscription was gifted.
|
||
IsGift bool `json:"is_gift"`
|
||
|
||
// The type of subscription. Possible values are:
|
||
//
|
||
// 1000 — Tier 1
|
||
//
|
||
// 2000 — Tier 2
|
||
//
|
||
// 3000 — Tier 3
|
||
Tier string `json:"tier"`
|
||
}
|
||
|
||
// Checks whether the user subscribes to the broadcaster’s channel.
|
||
//
|
||
// Requires a user access token that includes the user:read:subscriptions scope.
|
||
//
|
||
// A Twitch extensions may use an app access token if the broadcaster has granted the user:read:subscriptions scope from within the Twitch Extensions manager.
|
||
func (s *Subscriptions) CheckUserSubscription(ctx context.Context, params *CheckUserSubscriptionParams) (*CheckUserSubscriptionResponse, error) {
|
||
v, _ := query.Values(params)
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(s.baseUrl, "subscriptions/user", v), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp, err := s.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
|
||
if !statusOK {
|
||
return nil, fmt.Errorf("failed to check user subscription (%d)", resp.StatusCode)
|
||
}
|
||
|
||
var data CheckUserSubscriptionResponse
|
||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|