65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package games
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
)
|
||
|
||
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 weren’t found.
|
||
IDs []string `url:"id,omitempty"`
|
||
|
||
// The name of the category or game to get. The name must exactly match the category’s or game’s 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 weren’t 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 weren’t 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 weren’t 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 (c *Games) GetGames(ctx context.Context, params *GetGamesParams) (*GetGamesResponse, error) {
|
||
v, _ := query.Values(params)
|
||
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "games", 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()
|
||
|
||
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
||
if !statusOK {
|
||
return nil, fmt.Errorf("failed to get games (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data GetGamesResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|