go-twitch/api/games/get_games.go

64 lines
2.3 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
)
type GetGamesParams struct {
// The ID of the category or game to get. Include this parameter for each category or game you want to get. For example, &id=1234&id=5678.
// You may specify a maximum of 100 IDs. The endpoint ignores duplicate and invalid IDs or IDs that werent found.
IDs []string `url:"id,omitempty"`
// The name of the category or game to get. The name must exactly match the categorys or games title.
// Include this parameter for each category or game you want to get. For example, &name=foo&name=bar.
// You may specify a maximum of 100 names. The endpoint ignores duplicate names and names that werent found.
Names []string `url:"name,omitempty"`
// The IGDB ID of the game to get. Include this parameter for each game you want to get. For example, &igdb_id=1234&igdb_id=5678.
// You may specify a maximum of 100 IDs. The endpoint ignores duplicate and invalid IDs or IDs that werent found.
IGDBIDs []string `url:"igdb_id,omitempty"`
}
type GetGamesResponse struct {
// The list of categories and games. The list is empty if the specified categories and games werent found.
Data []Game `json:"data"`
}
// Gets information about specified categories or games.
//
// You may get up to 100 categories or games by specifying their ID or name. You may specify all IDs, all names, or a combination of IDs and names. If you specify a combination of IDs and names, the total number of IDs and names must not exceed 100.
//
// Requires an app access token or user access token.
func (g *Games) GetGames(ctx context.Context, params *GetGamesParams) (*GetGamesResponse, 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", 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 games (%d)", res.StatusCode)
}
2024-03-02 22:54:58 -05:00
var data GetGamesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}