go-twitch/api/streams/get_streams.go

105 lines
3.8 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"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 17:34:18 -05:00
"go.fifitido.net/twitch/api/types"
)
type GetStreamsParams struct {
// A user ID used to filter the list of streams.
// Returns only the streams of those users that are broadcasting.
// To specify multiple IDs, include the user_id parameter for each user. For example, &user_id=1234&user_id=5678.
// You may specify a maximum of 100 IDs.
UserIDs []string `url:"user_id,omitempty"`
// A user login name used to filter the list of streams.
// Returns only the streams of those users that are broadcasting.
// To specify multiple names, include the user_login parameter for each user. For example, &user_login=foo&user_login=bar.
// You may specify a maximum of 100 login names.
UserLogins []string `url:"user_login,omitempty"`
// A game (category) ID used to filter the list of streams.
// Returns only the streams that are broadcasting the game (category).
// You may specify a maximum of 100 IDs.
// To specify multiple IDs, include the game_id parameter for each game. For example, &game_id=9876&game_id=5432.
GameIDs []string `url:"game_id,omitempty"`
// The type of stream to filter the list of streams by.
// Possible values are:
//
// all, live
//
// The default is all.
Type *string `url:"type,omitempty"`
// A language code used to filter the list of streams.
// Returns only streams that broadcast in the specified language.
// Specify the language using an ISO 639-1 two-letter language code or other if the broadcast uses a language not in the list of supported stream languages.
//
// You may specify a maximum of 100 language codes.
// To specify multiple languages, include the language parameter for each language. For example, &language=de&language=fr.
Languages []string `url:"language,omitempty"`
// 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 20.
First *int `url:"first,omitempty"`
// The cursor used to get the previous page of results.
// The Pagination object in the response contains the cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
Before *types.Cursor `url:"before,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 GetStreamsResponse struct {
// The list of streams.
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 a list of all streams. 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.
//
// Requires an app access token or user access token.
func (s *Streams) GetStreams(ctx context.Context, params *GetStreamsParams) (*GetStreamsResponse, error) {
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(s.baseUrl, "streams", v), nil)
2024-03-03 17:34:18 -05:00
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 streams (%d)", res.StatusCode)
}
2024-03-03 17:34:18 -05:00
var data GetStreamsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}