Skip to content

Commit

Permalink
Merge pull request #8 from MxHonesty/cloud-eval-endpoint
Browse files Browse the repository at this point in the history
Add wrapper for cloud-eval endpoint issue #7
  • Loading branch information
cyanfish committed Oct 25, 2020
2 parents b833d2f + 395ad88 commit 035f5c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
21 changes: 13 additions & 8 deletions lichess/api.py
Expand Up @@ -35,13 +35,13 @@ class DefaultApiClient(object):

max_retries = -1
"""The maximum number of retries after rate-limiting before an exception is raised. -1 for infinite retries."""

def __init__(self, base_url=None, max_retries=None):
if base_url is not None:
self.base_url = base_url
if max_retries is not None:
self.max_retries = max_retries

def call(self, path, params=None, post_data=None, auth=None, format=lichess.format.JSON, object_type=lichess.format.PUBLIC_API_OBJECT):
"""Makes an API call, prepending :data:`~lichess.api.DefaultApiClient.base_url` to the provided path. HTTP GET is used unless :data:`post_data` is provided.
Expand All @@ -52,7 +52,7 @@ def call(self, path, params=None, post_data=None, auth=None, format=lichess.form
DefaultApiClient._first_call = False
else:
time.sleep(1)

if auth is None:
auth = lichess.auth.EMPTY
elif isinstance(auth, str):
Expand All @@ -77,12 +77,12 @@ def call(self, path, params=None, post_data=None, auth=None, format=lichess.form
retry_count += 1
else:
break

if resp.status_code != 200:
raise ApiHttpError(resp.status_code, url, resp.text)

return format.parse(object_type, resp)

def on_rate_limit(self, url, retry_count):
"""A handler called when HTTP 429 is received.
Expand Down Expand Up @@ -145,7 +145,7 @@ def _batch(fn, args, kwargs, batch_size):

def user(username, **kwargs):
"""Wrapper for the `GET /api/user/<username> <https://github.com/ornicar/lila#get-apiuserusername-fetch-one-user>`_ endpoint.
>>> user = lichess.api.user('thibault')
>>> print(user.get('perfs', {}).get('blitz', {}).get('rating'))
1617
Expand Down Expand Up @@ -261,7 +261,7 @@ def user_games(username, **kwargs):
>>> print(next(pgns))
[Event "Casual rapid game"]
...
>>> pgn = lichess.api.user_games('cyanfish', max=50, format=SINGLE_PGN)
>>> print(pgn)
[Event "Casual rapid game"]
Expand Down Expand Up @@ -316,6 +316,11 @@ def tv_channels(**kwargs):
"""Wrapper for the `GET /tv/channels <https://github.com/ornicar/lila#get-tvchannels-fetch-current-tournaments>`_ endpoint."""
return _api_get('/tv/channels', kwargs)

def cloud_eval(fen, **kwargs):
"""Wrapper for the `GET api/cloud-eval <https://lichess.org/api#operation/apiCloudEval>`_ endpoint. """
kwargs['fen'] = fen
return _api_get('/api/cloud-eval', kwargs)

def login(username, password):
cookie_jar = _api_post('/login', {'format': lichess.format.COOKIES}, {'username': username, 'password': password})
return lichess.auth.Cookie(cookie_jar)
22 changes: 14 additions & 8 deletions test.py
Expand Up @@ -17,7 +17,7 @@ def test_users_by_team(self):
lst = list(itertools.islice(users, 2))
self.assertEqual(type(lst[0]['id']), str)
self.assertEqual(len(lst), 2)

def test_users_by_ids(self):
users = lichess.api.users_by_ids(['thibault', 'cyanfish'])
lst = list(users)
Expand All @@ -26,16 +26,16 @@ def test_users_by_ids(self):
self.assertEqual(type(rating1), int)
self.assertEqual(type(rating2), int)
self.assertNotEqual(rating1, rating2)

def test_users_status(self):
users = lichess.api.users_status(['thibault', 'cyanfish'])
online_count = len([u for u in users if u.get('online')])
self.assertEqual(type(online_count), int)

def test_user_activity(self):
activity = lichess.api.user_activity('thibault')
self.assertEqual(type(activity), list)

def test_current_game(self):
game = lichess.api.current_game('cyanfish')
self.assertEqual(type(game['moves']), type(u''))
Expand All @@ -51,7 +51,7 @@ def test_game_pgn(self):
def test_game_pychess(self):
game = lichess.api.game('Qa7FJNk2', format=lichess.format.PYCHESS)
self.assertTrue('Event' in game.headers)

def test_games_by_ids(self):
games = lichess.api.games_by_ids(['Qa7FJNk2', '4M973EVR'], with_moves=1)
lst = list(games)
Expand Down Expand Up @@ -87,15 +87,15 @@ def test_user_games_pychess(self):
self.assertTrue('Event' in lst[0].headers)
self.assertTrue('Event' in lst[1].headers)
self.assertNotEqual(lst[0], lst[1])

def test_tournaments(self):
tourns = lichess.api.tournaments()
self.assertGreater(len(tourns), 0)

def test_tournament(self):
tourn = lichess.api.tournament('winter17')
self.assertEqual(tourn['id'], 'winter17')

def test_tournament_standings(self):
stands = lichess.api.tournament_standings('winter17')
first_20 = list(itertools.islice(stands, 20))
Expand All @@ -106,6 +106,12 @@ def test_tv_channels(self):
channels = lichess.api.tv_channels()
self.assertEqual(type(channels['Blitz']), dict)

def test_cloud_eval(self):
evaluation = lichess.api.cloud_eval(fen="rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2", multiPv=5, variant="standard")
pvs = evaluation['pvs']
self.assertEqual(evaluation['fen'], "rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2")
self.assertEqual(len(pvs), 5)

class PgnIntegrationTestCase(unittest.TestCase):

def test_pychess(self):
Expand Down

0 comments on commit 035f5c3

Please sign in to comment.