go-twitch/api/channels/get_channel_followers.go

99 lines
4.0 KiB
Go
Raw Permalink Normal View History

2024-02-27 22:13:57 -05:00
package channels
import (
"context"
2024-02-27 22:13:57 -05:00
"encoding/json"
"fmt"
"net/http"
2024-02-27 22:13:57 -05:00
"time"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-02-27 22:13:57 -05:00
"go.fifitido.net/twitch/api/types"
)
type GetChannelFollowersParams struct {
// A users ID. Use this parameter to see whether the user follows this broadcaster.
// If specified, the response contains this user if they follow the broadcaster.
// If not specified, the response contains all users that follow the broadcaster.
//
// Using this parameter requires both a user access token with the moderator:read:followers scope and the user ID
// in the access token match the broadcaster_id or be the user ID for a moderator of the specified broadcaster.
UserID *string `url:"user_id,omitempty"`
2024-02-27 22:13:57 -05:00
// The broadcasters ID. Returns the list of users that follow this broadcaster.
BroadcasterID string `url:"broadcaster_id"`
// The maximum number of items to return per page in the response. The minimum page size is 1 item per page and the maximum is 100. The default is 20.
First *int `url:"first,omitempty"`
// The cursor used to get the next page of results. The Pagination object in the response contains the cursors value.
// Read more: https://dev.twitch.tv/docs/api/guide#pagination
After *types.Cursor `url:"after,omitempty"`
}
type GetChannelFollowersResponse struct {
// The list of users that follow the specified broadcaster. The list is in descending order by followed_at (with the most recent follower first).
// The list is empty if nobody follows the broadcaster, the specified user_id isnt in the follower list,
// the user access token is missing the moderator:read:followers scope, or the user isnt the broadcaster or moderator for the channel.
Data []ChannelFollower `json:"data"`
// Contains the information used to page through the list of results. The object is empty if there are no more pages left to page through.
// Read more: https://dev.twitch.tv/docs/api/guide#pagination
Pagination types.Pagination `json:"pagination"`
// The total number of users that follow this broadcaster.
// As someone pages through the list, the number of users may change as users follow or unfollow the broadcaster.
Total int `json:"total"`
}
type ChannelFollower struct {
// An ID that uniquely identifies the user thats following the broadcaster.
UserID string `json:"user_id"`
// The users login name.
UserLogin string `json:"user_login"`
// The users display name.
UserName string `json:"user_name"`
// The UTC timestamp when the user started following the broadcaster.
FollowedAt time.Time `json:"followed_at"`
}
// Gets a list of users that follow the specified broadcaster. You can also use this endpoint to see whether a specific user follows the broadcaster.
//
// - Requires a user access token that includes the moderator:read:followers scope.
//
// - The ID in the broadcaster_id query parameter must match the user ID in the access token or the user ID in the access token must be a moderator for the specified broadcaster.
//
// This endpoint will return specific follower information only if both of the above are true.
// If a scope is not provided or the user isnt the broadcaster or a moderator for the specified channel,
// only the total follower count will be included in the response.
func (c *Channels) GetChannelFollowers(ctx context.Context, params *GetChannelFollowersParams) (*GetChannelFollowersResponse, error) {
2024-02-27 22:13:57 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "channels/followers", v), nil)
2024-02-27 22:13:57 -05:00
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
2024-02-27 22:13:57 -05:00
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get channel followers (%d)", res.StatusCode)
}
2024-02-27 22:13:57 -05:00
var data GetChannelFollowersResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
2024-02-27 22:13:57 -05:00
return nil, err
}
return &data, nil
}