lichess

package
v0.0.0-...-b66454a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// Base URL for API requests. Defaults to the public GitHub API, but can be
	// set to a domain endpoint. BaseURL should always be specified with a trailing slash.
	BaseURL *url.URL

	// Services used for talking to different parts of the Lichess API.
	Games   *GamesService
	Puzzles *PuzzlesService
	// contains filtered or unexported fields
}

A Client manages communication with the Lichess API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient returns a new Lichess API client. If a nil httpClient is provided, a new http.Client will be used.

func (*Client) BareDo

func (c *Client) BareDo(req *http.Request) (*Response, error)

BareDo sends an API request and lets you handle the api response. If an error or API Error occurs, the error will contain more information. Otherwise, you are supposed to read and close the response's Body. If rate limit is exceeded and reset time is in the future, BareDo returns *RateLimitError immediately without making a network API call.

The provided ctx must be non-nil, if it is nil an error is returned. If it is canceled or times out, ctx.Err() will be returned.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it. If v is nil, and no error happens, the response is returned as is. If rate limit is exceeded and reset time is in the future, Do returns *RateLimitError immediately without making a network API call.

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, urlStr string) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash.

func (*Client) NewRequestWithBody

func (c *Client) NewRequestWithBody(
	ctx context.Context,
	method, urlStr string,
	body RequestBody,
) (*http.Request, error)

NewRequestWithBody creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.

func (*Client) WithAuthToken

func (c *Client) WithAuthToken(token string) *Client

WithAuthToken returns a copy of the client configured to use the provided token for the Authorization header.

type ExportByUsernameOptions

type ExportByUsernameOptions struct {
	ExportOptions
	Since    *int    `url:"since,omitempty"`
	Until    *int    `url:"until,omitempty"`
	Max      *int    `url:"max,omitempty"`
	VS       *string `url:"vs,omitempty"`
	Rated    *bool   `url:"rated,omitempty"`
	PerfType *string `url:"perfType,omitempty"`
	Color    *string `url:"color,omitempty"`
	Analysed *string `url:"analysed,omitempty"`
	Ongoing  *bool   `url:"ongoing,omitempty"`
	Finished *bool   `url:"finished,omitempty"`
	LastFen  *bool   `url:"lastFen,omitempty"`
	Sort     *string `url:"sort,omitempty"` // Either "dateAsc" or "dateDesc"
}

ExportByUsernameOptions specifies parameters for GamesService.ExportByUsername method.

type ExportOptions

type ExportOptions struct {
	Moves     *bool   `url:"moves,omitempty"`
	PgnInJson *bool   `url:"pgnInJson,omitempty"`
	Tags      *bool   `url:"tags,omitempty"`
	Clocks    *bool   `url:"clocks,omitempty"`
	Evals     *bool   `url:"evals,omitempty"`
	Accuracy  *bool   `url:"accuracy,omitempty"`
	Opening   *bool   `url:"opening,omitempty"`
	Literate  *bool   `url:"literate,omitempty"`
	Players   *string `url:"players,omitempty"`
}

ExportOptions specifies parameters for GamesService.ExportById GamesService.ExportCurrent, GamesService.ExportByUsername methods.

type Game

type Game struct {
	Id         string      `json:"id,omitempty"`
	Rated      bool        `json:"rated,omitempty"`
	Variant    GameVariant `json:"variant,omitempty"`
	Speed      GameSpeed   `json:"speed,omitempty"`
	Perf       string      `json:"perf,omitempty"`
	CreatedAt  int64       `json:"createdAt,omitempty"`
	LastMoveAt int64       `json:"lastMoveAt,omitempty"`
	Status     GameStatus  `json:"status,omitempty"`
	Players    struct {
		White GameUser `json:"white,omitempty"`
		Black GameUser `json:"black,omitempty"`
	} `json:"players,omitempty"`
	InitialFen  *string         `json:"initialFen,omitempty"`
	Winner      *string         `json:"winner,omitempty"`
	Opening     *GameOpening    `json:"opening,omitempty"`
	Moves       *string         `json:"moves,omitempty"`
	Pgn         *string         `json:"pgn,omitempty"`
	DaysPerTurn *int            `json:"daysPerTurn,omitempty"`
	Analysis    []*GameAnalysis `json:"analysis,omitempty"`
	Tournament  *string         `json:"tournament,omitempty"`
	Swiss       *string         `json:"swiss,omitempty"`
	Clock       *GameClock      `json:"clock,omitempty"`
}

Game represents a Lichess game.

type GameAnalysis

type GameAnalysis struct {
	Eval      int                   `json:"eval,omitempty"`
	Best      *string               `json:"best,omitempty"`
	Variation *string               `json:"variation,omitempty"`
	Judgement *GameAnalysisJudgment `json:"judgement,omitempty"`
}

GameAnalysis represents a Lichess game user analysis.

type GameAnalysisJudgment

type GameAnalysisJudgment struct {
	Name    *string `json:"name,omitempty"`
	Comment *string `json:"comment,omitempty"`
}

GameAnalysisJudgment represents a Lichess game user analysis judgment.

type GameClock

type GameClock struct {
	Initial   *int `json:"initial,omitempty"`
	Increment *int `json:"increment,omitempty"`
	TotalTime *int `json:"totalTime,omitempty"`
	Limit     *int `json:"limit,omitempty"`
}

GameClock represents a Lichess game clock.

type GameDescription

type GameDescription struct {
	Id      string `json:"id"`
	Variant struct {
		Key GameVariant `json:"key"`
	} `json:"variant"`
	Speed         GameSpeed `json:"speed"`
	Perf          string    `json:"perf"`
	Rated         bool      `json:"rated"`
	InitialFen    string    `json:"initialFen"`
	Fen           string    `json:"fen"`
	Player        string    `json:"player"`
	Turns         int       `json:"turns"`
	StartedAtTurn int       `json:"startedAtTurn"`
	Source        string    `json:"source"`
	Status        struct {
		Name GameStatus `json:"name"`
	} `json:"status"`
	CreatedAt int64  `json:"createdAt"`
	LastMove  string `json:"lastMove"`
	Players   struct {
		White GameUser `json:"white"`
		Black GameUser `json:"black"`
	} `json:"players"`
}

GameDescription the description of a Lichess game sent at the beginning and end of stream.

func (GameDescription) GameStreamEventType

func (e GameDescription) GameStreamEventType() GameStreamEventType

type GameMove

type GameMove struct {
	Fen string `json:"fen"`
	LM  string `json:"lm"`
	WC  int    `json:"wc"`
	BC  int    `json:"bc"`
}

func (GameMove) GameStreamEventType

func (e GameMove) GameStreamEventType() GameStreamEventType

type GameOpening

type GameOpening struct {
	Eco  *string `json:"eco,omitempty"`
	Name *string `json:"name,omitempty"`
	Ply  *int    `json:"ply,omitempty"`
}

GameOpening represents a Lichess opening analysis.

type GameSpeed

type GameSpeed string

GameSpeed represents a Lichess game speed.

const (
	UltraBullet    GameSpeed = "ultraBullet"
	Bullet         GameSpeed = "bullet"
	Blitz          GameSpeed = "blitz"
	Rapid          GameSpeed = "rapid"
	Classical      GameSpeed = "classical"
	Correspondence GameSpeed = "correspondence"
)

type GameStatus

type GameStatus string

GameStatus represents a Lichess game status.

const (
	Created       GameStatus = "created"
	Started       GameStatus = "started"
	Aborted       GameStatus = "aborted"
	Mate          GameStatus = "mate"
	Resign        GameStatus = "resign"
	Stalemate     GameStatus = "stalemate"
	Timeout       GameStatus = "timeout"
	Draw          GameStatus = "draw"
	OutOfTime     GameStatus = "outoftime"
	Cheat         GameStatus = "cheat"
	NoStart       GameStatus = "noStart"
	UnknownFinish GameStatus = "unknownFinish"
	VariantEnd    GameStatus = "variantEnd"
)

type GameStream

type GameStream struct {
	Id         string      `json:"id"`
	Rated      bool        `json:"rated"`
	Variant    GameVariant `json:"variant"`
	Speed      GameSpeed   `json:"speed"`
	Perf       string      `json:"perf"`
	CreatedAt  int64       `json:"createdAt"`
	Status     int64       `json:"status"`
	StatusName GameStatus  `json:"statusName"`
	Clock      GameClock   `json:"clock"`
	Players    struct {
		White GameUser `json:"white"`
		Black GameUser `json:"black"`
	} `json:"players"`
}

GameStream is a Lichess Game representation for streaming methods. For instance: GamesService.StreamGamesOfUsers.

type GameStreamEvent

type GameStreamEvent interface {
	GameStreamEventType() GameStreamEventType
}

GameStreamEvent represents a Lichess game stream event.

type GameStreamEventError

type GameStreamEventError struct {
	// contains filtered or unexported fields
}

GameStreamEventError represents a Lichess game stream event error.

func (GameStreamEventError) GameStreamEventType

func (e GameStreamEventError) GameStreamEventType() GameStreamEventType

type GameStreamEventType

type GameStreamEventType string

GameStreamEventType represents a Lichess game stream event type.

const (
	GameDescriptionEventType GameStreamEventType = "description"
	GameMoveEventType        GameStreamEventType = "move"
	GameStreamErrorEventType GameStreamEventType = "error"
)

type GameUser

type GameUser struct {
	User        *LightUser        `json:"user,omitempty"`
	Rating      *int              `json:"rating,omitempty"`
	RatingDiff  *int              `json:"ratingDiff,omitempty"`
	Name        *string           `json:"name,omitempty"`
	Provisional *bool             `json:"provisional,omitempty"`
	AILevel     *int              `json:"aiLevel,omitempty"`
	Analysis    *GameUserAnalysis `json:"analysis,omitempty"`
	Team        *string           `json:"team,omitempty"`
}

GameUser represents a Lichess game user.

type GameUserAnalysis

type GameUserAnalysis struct {
	Inaccuracy int `json:"inaccuracy,omitempty"`
	Mistake    int `json:"mistake,omitempty"`
	Blunder    int `json:"blunder,omitempty"`
	Acpl       int `json:"acpl,omitempty"`
}

GameUserAnalysis represents a Lichess game user analysis.

type GameVariant

type GameVariant string

GameVariant represents a Lichess game variant.

const (
	Standard      GameVariant = "standard"
	Chess960      GameVariant = "chess960"
	Crazyhouse    GameVariant = "crazyhouse"
	Antichess     GameVariant = "antichess"
	Atomic        GameVariant = "atomic"
	Horde         GameVariant = "horde"
	KingOfTheHill GameVariant = "kingOfTheHill"
	RacingKings   GameVariant = "racingKings"
	ThreeCheck    GameVariant = "threeCheck"
	FromPosition  GameVariant = "fromPosition"
)

type GamesService

type GamesService service

GamesService handles communication with the game related methods of the Lichess API.

Lichess API docs: https://lichess.org/api#tag/Games

func (*GamesService) ExportById

func (s *GamesService) ExportById(ctx context.Context, id string, opts *ExportOptions) (*Game, *Response, error)

ExportById exports a Game by its identifier. Find more details at https://lichess.org/api#tag/Games/operation/gamePgn.

func (*GamesService) ExportByUsername

func (s *GamesService) ExportByUsername(
	ctx context.Context,
	username string,
	opts *ExportByUsernameOptions,
) ([]*Game, *Response, error)

ExportByUsername exports a list of Game played by the given username. Find more details at https://lichess.org/api#tag/Games/operation/apiGamesUser.

func (*GamesService) ExportCurrent

func (s *GamesService) ExportCurrent(
	ctx context.Context,
	username string,
	opts *ExportOptions,
) (*Game, *Response, error)

ExportCurrent exports the current Game being played by the given username. Find more details at https://lichess.org/api#tag/Games/operation/apiUserCurrentGame.

func (*GamesService) StreamGameMoves

func (s *GamesService) StreamGameMoves(ctx context.Context, id string) (chan GameStreamEvent, *Response, error)

StreamGameMoves streams GameStreamEvent happening at Game identified by id. It closes the channel of GameStreamEvent and the Response body when the context is done. So, please use the context.Context argument to control the lifetime of the stream. Find more details at https://lichess.org/api#tag/Games/operation/streamGame.

func (*GamesService) StreamGamesOfUsers

func (s *GamesService) StreamGamesOfUsers(
	ctx context.Context,
	usernames []string,
	opts *StreamGamesOfUsersOptions,
) (chan *GameStream, *Response, error)

StreamGamesOfUsers streams Game played among the given usernames. It closes the channel of Game and the Response body when the context is done. So, please use the context.Context argument to control the lifetime of the stream. Find more details at https://lichess.org/api#tag/Games/operation/gamesByUsers.

func (*GamesService) StreamUserGames

func (s *GamesService) StreamUserGames(
	ctx context.Context,
	username string,
	opts *ExportByUsernameOptions,
) (chan *Game, *Response, error)

StreamUserGames streams Game played by the given username. It closes the channel of Game and the Response body when the context is done. So, please use the context.Context argument to control the lifetime of the stream. Equivalent to GamesService.ExportByUsername but streams the response. Find more details at https://lichess.org/api#tag/Games/operation/apiGamesUser.

type GetPuzzleActivityOptions

type GetPuzzleActivityOptions struct {
	// Max specifies how many entries to download.
	// Leave empty to download all activity.
	Max *int `url:"max,omitempty"` // >= 1
	// Before is used to download entries before this timestamp.
	// Defaults to now. Use before and max for pagination.
	Before *int `url:"before,omitempty"` // >= 1356998400070
}

GetPuzzleActivityOptions specifies parameters for PuzzlesService.GetPuzzleActivity method.

type LightUser

type LightUser struct {
	Id     string  `json:"id,omitempty"`
	Name   string  `json:"name,omitempty"`
	Title  *string `json:"title,omitempty"`
	Patron bool    `json:"patron,omitempty"`
}

type Puzzle

type Puzzle struct {
	Id       string   `json:"id,omitempty"`
	Fen      string   `json:"fen,omitempty"`
	Plays    int      `json:"plays,omitempty"`
	Rating   int      `json:"rating,omitempty"`
	Solution []string `json:"solution,omitempty"`
	Themes   []string `json:"themes,omitempty"`
}

Puzzle represents a Lichess puzzle.

type PuzzleRound

type PuzzleRound struct {
	Date   int    `json:"date,omitempty"`
	Win    bool   `json:"win,omitempty"`
	Puzzle Puzzle `json:"puzzle,omitempty"`
}

PuzzleRound represents a Lichess puzzle round.

type PuzzlesService

type PuzzlesService service

PuzzlesService handles communication with the puzzle related methods of the Lichess API.

Lichess API docs: https://lichess.org/api#tag/Puzzles

func (*PuzzlesService) GetPuzzleActivity

func (s *PuzzlesService) GetPuzzleActivity(
	ctx context.Context,
	opts *GetPuzzleActivityOptions,
) ([]*PuzzleRound, *Response, error)

type RequestBody

type RequestBody struct {
	Bytes io.Reader
	Type  string
}

RequestBody is a data structure that holds the request body as well as the type of the content, which will be used to set the Content-Type header.

type Response

type Response struct {
	*http.Response
}

Response is a Lichess API response. This wraps the standard http.Response returned from Lichess and provides convenient access to things like pagination links.

type StreamGamesOfUsersOptions

type StreamGamesOfUsersOptions struct {
	CurrentGames *bool `url:"withCurrentGames,omitempty"`
}

StreamGamesOfUsersOptions specifies parameters for GamesService.StreamGamesOfUsers method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL