Skip to content

Commit

Permalink
Automatically get user_id if not provided in fpl.get_user
Browse files Browse the repository at this point in the history
  • Loading branch information
amosbastian committed Oct 15, 2019
1 parent 7bd0360 commit 3a9d076
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ dmypy.json
.pyre/

.vscode/
*.csv
18 changes: 14 additions & 4 deletions fpl/fpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
from .models.player import Player, PlayerSummary
from .models.team import Team
from .models.user import User
from .utils import (average, fetch, logged_in, position_converter, scale,
team_converter)
from .utils import (average, fetch, get_current_user, logged_in,
position_converter, scale, team_converter)


class FPL():
Expand All @@ -45,7 +45,7 @@ class FPL():
def __init__(self, session):
self.session = session

async def get_user(self, user_id, return_json=False):
async def get_user(self, user_id=None, return_json=False):
"""Returns the user with the given ``user_id``.
Information is taken from e.g.:
Expand All @@ -58,7 +58,17 @@ async def get_user(self, user_id, return_json=False):
:type return_json: bool
:rtype: :class:`User` or `dict`
"""
assert int(user_id) > 0, "User ID must be a positive number."
if user_id:
assert int(user_id) > 0, "User ID must be a positive number."
else:
# If no user ID provided get it from current session
try:
user = await get_current_user(self.session)
user_id = user["player"]["entry"]
except TypeError:
raise Exception("You must log in before using `get_user` if "
"you do not provide a user ID.")

url = API_URLS["user"].format(user_id)
user = await fetch(self.session, url)

Expand Down
1 change: 1 addition & 0 deletions fpl/models/h2h_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class H2HLeague():
>>> asyncio.run(main())
League 760869 - 760869
"""

def __init__(self, league_information, session):
self._session = session

Expand Down
7 changes: 7 additions & 0 deletions fpl/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
from functools import update_wrapper

from fpl.constants import API_URLS

headers = {"User-Agent": "https://github.com/amosbastian/fpl"}


Expand Down Expand Up @@ -169,3 +171,8 @@ def get_headers(referer):
"X-Requested-With": "XMLHttpRequest",
"Referer": referer
}


async def get_current_user(session):
user = await fetch(session, API_URLS["me"])
return user
56 changes: 30 additions & 26 deletions tests/test_h2h_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
from tests.helper import AsyncMock

h2h_league_data = {
"league": {
"id": 946125,
"name": "THE PUNDITS H2H",
"created": "2019-08-07T20:52:58.751814Z",
"closed": True,
"max_entries": None,
"league_type": "x",
"scoring": "h",
"admin_entry": 1726046,
"start_event": 1,
"code_privacy": "p",
"ko_rounds": None
},
"new_entries": {
"has_next": False,
"page": 1,
"results": [
"league": {
"id": 946125,
"name": "THE PUNDITS H2H",
"created": "2019-08-07T20:52:58.751814Z",
"closed": True,
"max_entries": None,
"league_type": "x",
"scoring": "h",
"admin_entry": 1726046,
"start_event": 1,
"code_privacy": "p",
"ko_rounds": None
},
"new_entries": {
"has_next": False,
"page": 1,
"results": [

]
},
"standings": {
"has_next": False,
"page": 1,
"results": []
}
]
},
"standings": {
"has_next": False,
"page": 1,
"results": []
}
}


Expand All @@ -51,10 +51,12 @@ async def test_get_fixtures_with_known_gameweek_unauthorized(
with pytest.raises(Exception):
await h2h_league.get_fixtures(gameweek=1)

@pytest.mark.skip(reason="Need to mock logging in properly.")
async def test_get_fixtures_with_known_gameweek_authorized(
self, loop, mocker, h2h_league):
self, loop, mocker, fpl, h2h_league):
mocked_logged_in = mocker.patch(
"fpl.models.h2h_league.logged_in", return_value=True)

fixtures = await h2h_league.get_fixtures(gameweek=1)
assert isinstance(fixtures, list)
mocked_logged_in.assert_called_once()
Expand All @@ -64,10 +66,12 @@ async def test_get_fixtures_with_unknown_gameweek_unauthorized(
with pytest.raises(Exception):
await h2h_league.get_fixtures()

@pytest.mark.skip(reason="Need to mock logging in properly.")
async def test_get_fixtures_with_unknown_gameweek_authorized(
self, loop, mocker, h2h_league):
self, loop, mocker, fpl, h2h_league):
mocked_logged_in = mocker.patch(
"fpl.models.h2h_league.logged_in", return_value=True)
await fpl.login()
fixtures = await h2h_league.get_fixtures()
assert isinstance(fixtures, list)
mocked_logged_in.assert_called_once()

0 comments on commit 3a9d076

Please sign in to comment.