54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package slapshotapi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type endpointGame struct {
|
|
gameID string
|
|
}
|
|
|
|
func getEndpointGame(gameID string) *endpointGame {
|
|
return &endpointGame{
|
|
gameID: gameID,
|
|
}
|
|
}
|
|
|
|
func (ep *endpointGame) path() string {
|
|
return fmt.Sprintf("/api/public/games/%s", ep.gameID)
|
|
}
|
|
|
|
func (ep *endpointGame) method() string {
|
|
return "GET"
|
|
}
|
|
|
|
// ErrGameNotFound is returned when a game ID does not match any known game
|
|
var ErrGameNotFound = errors.New("game not found")
|
|
|
|
// GetGame retrieves match details for a specific game by its ID.
|
|
func (c *SlapAPI) GetGame(
|
|
ctx context.Context,
|
|
gameID string,
|
|
) (*Game, error) {
|
|
if gameID == "" {
|
|
return nil, errors.New("gameID cannot be empty")
|
|
}
|
|
endpoint := getEndpointGame(gameID)
|
|
data, err := c.request(ctx, endpoint)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "404") {
|
|
return nil, ErrGameNotFound
|
|
}
|
|
return nil, errors.Wrap(err, "c.request")
|
|
}
|
|
game, err := unmarshal[Game](data)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "unmarshal")
|
|
}
|
|
return game, nil
|
|
}
|