go-twitch/api/games/get_top_games.go

67 lines
2.1 KiB
Go
Raw Permalink Normal View History

2024-03-02 22:54:58 -05:00
package games
import (
"context"
"encoding/json"
"fmt"
2024-03-02 22:54:58 -05:00
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:54:58 -05:00
"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 (g *Games) GetTopGames(ctx context.Context, params *GetTopGamesParams) (*GetTopGamesResponse, error) {
2024-03-02 22:54:58 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(g.baseUrl, "games/top", v), nil)
2024-03-02 22:54:58 -05:00
if err != nil {
return nil, err
}
res, err := g.client.Do(req)
2024-03-02 22:54:58 -05:00
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 top games (%d)", res.StatusCode)
}
2024-03-02 22:54:58 -05:00
var data GetTopGamesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}