Skip to content

Commit

Permalink
Ensure error is raised from _ensure_user when the user doesn't exist
Browse files Browse the repository at this point in the history
Refactors _ensure_user to not rely on ensure_flattened, and handle
the response directly, as we know exactly what to expect here.
  • Loading branch information
SamHames committed Apr 20, 2022
1 parent 7f62427 commit b62c47e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions test_twarc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,12 @@ def test_list_tweets():
assert len(tweets) >= 90


def test_user_lookup_non_existent():
with pytest.raises(ValueError):
# This user does not exist, and a value error should be raised
T._ensure_user("noasdfasdf")


def test_twarc_metadata():

# With metadata (default)
Expand Down
10 changes: 5 additions & 5 deletions twarc/client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
POLL_FIELDS,
PLACE_FIELDS,
LIST_FIELDS,
ensure_flattened,
)
from twarc.decorators2 import *
from twarc.version import version, user_agent
Expand Down Expand Up @@ -1883,11 +1882,12 @@ def _ensure_user(self, user):

lookup = []
if len(user) > 15 or (is_numeric and self._id_exists(user)):
lookup = ensure_flattened(list(self.user_lookup([user])))
lookup = list(self.user_lookup([user]))[0]
else:
lookup = ensure_flattened(list(self.user_lookup([user], usernames=True)))
if lookup:
return lookup[-1]
lookup = list(self.user_lookup([user], usernames=True))[0]

if "data" in lookup:
return lookup["data"][0]
else:
raise ValueError(f"No such user {user}")

Expand Down

0 comments on commit b62c47e

Please sign in to comment.