Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions src/pychasing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
def _print_error(response: requests.Response) -> None:
"""Print out an error code from a `requests.Response` if an HTTP error is
encountered.

"""
error_side = ("Client" if 400 <= response.status_code < 500 else "Server"
if 500 <= response.status_code < 600 else None)
Expand All @@ -55,21 +55,21 @@ def _print_error(response: requests.Response) -> None:
error_description = ""
if response_json and "error" in response_json:
error_description = "(" + response_json["error"] + ") "

print(f"\033[93m{response.status_code} {error_side} Error: {reason} "
f"{error_description}for url: {response.url}\033[0m")


def p(v):
"""Return `v` if `v` is `...` or a `str`, else return `v.value`.

"""
return v if v == ... or isinstance(v, str) else v.value


class Client:
"""The main class used to interact with the Ballchasing API.

"""
def __init__(self, token: str, auto_rate_limit: bool = True,
patreon_tier: Union[str, enums.PatreonTier] = enums.PatreonTier.none,
Expand All @@ -95,12 +95,12 @@ def __init__(self, token: str, auto_rate_limit: bool = True,
patreon_tier = enums.PatreonTier[patreon_tier]
except KeyError as exc:
raise ValueError(f"{patreon_tier!r} is not a valid PatreonTier") from exc

if auto_rate_limit:
for k, v in patreon_tier.value.items():
rlim.set_rate_limiter(getattr(self, k.name),
rlim.RateLimiter(*v, safestart=rate_limit_safe_start))

def ping(self, *, print_error: bool = True) -> requests.Response:
"""Ping the https://ballchasing.com servers.

Expand All @@ -109,12 +109,12 @@ def ping(self, *, print_error: bool = True) -> requests.Response:
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare URL
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -123,7 +123,7 @@ def ping(self, *, print_error: bool = True) -> requests.Response:
# prepare headers
prepped_headers = httpprep.Headers()
prepped_headers.Authorization = self._token

# make request, print error, and return response
response = requests.get(prepped_url.build(), headers=prepped_headers.format_dict())
if print_error:
Expand All @@ -146,12 +146,12 @@ def upload_replay(self, file: io.BufferedReader,
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare URL
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -161,7 +161,7 @@ def upload_replay(self, file: io.BufferedReader,
# prepare headers
prepped_headers = httpprep.Headers()
prepped_headers.Authorization = self._token

# make request, print error, and return response
response = requests.post(prepped_url.build(query_check=...),
headers=prepped_headers.format_dict(), files={"file":file})
Expand Down Expand Up @@ -239,7 +239,7 @@ def list_replays(self, *, next: str = ..., title: str = ..., player_names: Itera
Only include replays played after a given date, formatted as an
RFC3339 datetime string.
count : int, optional, default=150
The number of replays returned. Must be between 1 and 200
The number of replays returned. Must be between 1 and 200
(inclusive) if defined.
sort_by : str or ReplaySortBy, optional, default=
ReplaySortBy.upload_date
Expand All @@ -249,16 +249,16 @@ def list_replays(self, *, next: str = ..., title: str = ..., player_names: Itera
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

Raises:
ValueError: `count` is defined and is less than 0 or greater than
200.

"""
if count != ... and 1 > count > 200:
raise ValueError("\"count\" must be between 1 and 200")
Expand Down Expand Up @@ -329,7 +329,7 @@ def list_replays(self, *, next: str = ..., title: str = ..., player_names: Itera
if print_error:
_print_error(response)
return response

@rlim.placeholder
def get_replay(self, replay_id: str, *, print_error: bool = True) -> requests.Response:
"""Get more in-depth information for a specific replay.
Expand All @@ -341,12 +341,12 @@ def get_replay(self, replay_id: str, *, print_error: bool = True) -> requests.Re
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -361,7 +361,7 @@ def get_replay(self, replay_id: str, *, print_error: bool = True) -> requests.Re
if print_error:
_print_error(response)
return response

@rlim.placeholder
def delete_replay(self, replay_id: str, *, print_error: bool = True) -> requests.Response:
"""Delete the given replay from https://ballchasing.com, so long as the
Expand All @@ -374,12 +374,12 @@ def delete_replay(self, replay_id: str, *, print_error: bool = True) -> requests
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -394,7 +394,7 @@ def delete_replay(self, replay_id: str, *, print_error: bool = True) -> requests
if print_error:
_print_error(response)
return response

@rlim.placeholder
def patch_replay(self, replay_id: str, *, title: str = ...,
visibility: Union[str, enums.Visibility] = ..., group: str = ...,
Expand All @@ -417,12 +417,12 @@ def patch_replay(self, replay_id: str, *, title: str = ...,
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand Down Expand Up @@ -454,7 +454,7 @@ def download_replay(self, replay_id: str, *, print_error: bool = True) -> reques
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Warnings
--------
Replay files can be rather large (up to around 1.5mb). The HTTP request
Expand All @@ -465,7 +465,7 @@ def download_replay(self, replay_id: str, *, print_error: bool = True) -> reques
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand Down Expand Up @@ -509,7 +509,7 @@ def create_group(self, name: str, player_identification: Union[str, enums.Player
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -532,7 +532,7 @@ def create_group(self, name: str, player_identification: Union[str, enums.Player
if print_error:
_print_error(response)
return response

@rlim.placeholder
def list_groups(self, *, next: str = ..., name: str = ..., creator: Union[str, int] = ...,
group: str = ..., created_before: Union[models.Date, str] = ...,
Expand Down Expand Up @@ -580,15 +580,15 @@ def list_groups(self, *, next: str = ..., name: str = ..., creator: Union[str, i
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

Raises:
ValueError: `count` is defined and is less than 0 or greater than
200.

"""
if count != ... and 1 > count > 200:
raise ValueError("\"count\" must be between 1 and 200")

# prepare headers
prepped_headers = httpprep.Headers()
prepped_headers.Authorization = self._token
Expand Down Expand Up @@ -620,7 +620,7 @@ def list_groups(self, *, next: str = ..., name: str = ..., creator: Union[str, i
group,
created_before,
created_after,
count,
count,
p(sort_by),
p(sort_dir)
]
Expand All @@ -644,12 +644,12 @@ def get_group(self, group_id: str, *, print_error: bool = True) -> requests.Resp
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -664,7 +664,7 @@ def get_group(self, group_id: str, *, print_error: bool = True) -> requests.Resp
if print_error:
_print_error(response)
return response

def delete_group(self, group_id: str, *, print_error: bool = True) -> requests.Response:
"""Delete a specific group (and all children groups) from
https://ballchasing.com, so long as it is owned by the token holder.
Expand All @@ -676,12 +676,12 @@ def delete_group(self, group_id: str, *, print_error: bool = True) -> requests.R
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -696,7 +696,7 @@ def delete_group(self, group_id: str, *, print_error: bool = True) -> requests.R
if print_error:
_print_error(response)
return response

@rlim.placeholder
def patch_group(self, group_id: str, *,
player_identification: Union[str, enums.PlayerIdentification] = ...,
Expand Down Expand Up @@ -726,12 +726,12 @@ def patch_group(self, group_id: str, *,
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
The `requests.Response` object returned from the HTTP request.

"""
# prepare url
prepped_url = httpprep.URL(protocol="https", domain="ballchasing", top_level_domain="com",
Expand All @@ -754,16 +754,16 @@ def patch_group(self, group_id: str, *,
if print_error:
_print_error(response)
return response

def maps(self, *, print_error: bool = True) -> requests.Response:
"""Get a list of current maps.

Parameters
----------
print_error : bool, optional, default=True
Prints an error message (that contains information about the error) if the request
resulted in an HTTP error (i.e. status codes 400 through 599).

Returns
-------
requests.Response
Expand Down
14 changes: 7 additions & 7 deletions src/pychasing/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ class Map(enum.Enum):
arc_p="arc_p"
Starbase_ARC="arc_p"
arc_standard_p="arc_standard_p"
Starbase_ARC_Standard="arc_standard_p"
Starbase_ARC_Standard="arc_standard_p"
bb_p="bb_p"
Champions_Field_NFL="bb_p"
beach_night_p="beach_night_p"
Salty_Shores_Night="beach_night_p"
Salty_Shores_Night="beach_night_p"
beach_p="beach_p"
Salty_Shores="beach_p"
beachvolley="beachvolley"
Salty_Shores_Volley="beachvolley"
chn_stadium_day_p="chn_stadium_day_p"
Forbidden_Temple_Day="chn_stadium_day_p"
Salty_Shores_Volley="beachvolley"
chn_stadium_day_p="chn_stadium_day_p"
Forbidden_Temple_Day="chn_stadium_day_p"
chn_stadium_p="chn_stadium_p"
Forbidden_Temple="chn_stadium_p"
cs_day_p="cs_day_p"
Expand All @@ -174,7 +174,7 @@ class Map(enum.Enum):
cs_p="cs_p"
Champions_Field="cs_p"
eurostadium_night_p="eurostadium_night_p"
Mannfield_Night="eurostadium_night_p"
Mannfield_Night="eurostadium_night_p"
eurostadium_p="eurostadium_p"
Mannfield="eurostadium_p"
eurostadium_rainy_p="eurostadium_rainy_p"
Expand Down Expand Up @@ -310,7 +310,7 @@ class Season(enum.Enum):
f2p_28="f28"
f2p_29="f29"
f2p_30="f30"

class PlayerIdentification(enum.Enum):
by_id="by-id"
by_name="by-name"
Expand Down
Loading