go-twitch/api/games/get_top_games.go

62 lines
1.9 KiB
Go
Raw Normal View History

2024-03-02 22:54:58 -05:00
package games
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetTopGamesParams struct {
// 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 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"`
// 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"`
}
type GetTopGamesResponse struct {
// The list of broadcasts. The broadcasts are sorted by the number of viewers, with the most popular first.
Data []Game `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"`
}
// Gets information about all broadcasts on Twitch.
//
// Requires an app access token or user access token.
func (c *Games) GetTopGames(ctx context.Context, params *GetTopGamesParams) (*GetTopGamesResponse, error) {
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "games/top", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data GetTopGamesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}