go-twitch/api/streams/get_followed_streams.go

72 lines
2.4 KiB
Go
Raw Normal View History

2024-03-03 17:34:18 -05:00
package streams
import (
"context"
"encoding/json"
"fmt"
2024-03-03 17:34:18 -05:00
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetFollowedStreamsParams struct {
// The ID of the user whose list of followed streams you want to get. This ID must match the user ID in the access token.
UserID string `url:"user_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 items per page.
// The default is 100.
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 GetFollowedStreamsResponse struct {
// The list of live streams of broadcasters that the specified user follows.
// The list is in descending order by the number of viewers watching the stream.
// Because viewers come and go during a stream, its possible to find duplicate or missing streams in the list as you page through the results.
// The list is empty if none of the followed broadcasters are streaming live.
Data []Stream `json:"data"`
// 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"`
}
// Gets the list of broadcasters that the user follows and that are streaming live.
//
// Requires a user access token that includes the user:read:follows scope.
func (s *Streams) GetFollowedStreams(ctx context.Context, params *GetFollowedStreamsParams) (*GetFollowedStreamsResponse, error) {
v, _ := query.Values(params)
endpoint := s.baseUrl.ResolveReference(&url.URL{Path: "streams/followed", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := s.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 followed streams (%d)", res.StatusCode)
}
2024-03-03 17:34:18 -05:00
var data GetFollowedStreamsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}