Skip to content

Commit 388ebcc

Browse files
committed
Refactor typing again and minor changes
1 parent 1ce919b commit 388ebcc

File tree

5 files changed

+27
-70
lines changed

5 files changed

+27
-70
lines changed

hypixelio/_async/client.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def _fetch(
9898
data: Optional[dict]
9999
The GET Request's Key-Value Pair. Example: {"uuid": "abc"} is converted to `?uuid=abc`. Defaults to None.
100100
api_key: bool
101-
If key is needed for the endpoin
101+
If key is needed for the endpoint.
102102
103103
Returns
104104
-------
@@ -164,11 +164,8 @@ async def _filter_name_uuid(name: Optional[str] = None, uuid: Optional[str] = No
164164

165165
# Context managers
166166
async def __aenter__(self) -> "AsyncClient":
167-
# Initialize the session
168167
self._session = aiohttp.ClientSession()
169168

170-
await self._session.__aenter__()
171-
172169
return self
173170

174171
async def __aexit__(
@@ -178,7 +175,7 @@ async def __aexit__(
178175
exc_tb: Optional[TracebackType],
179176
) -> None:
180177
if self._session is not None:
181-
await self._session.__aexit__(exc_type, exc_val, exc_tb)
178+
await self._session.close()
182179

183180
# Hypixel API endpoint methods
184181
async def get_key_info(self, api_key: Optional[str] = None) -> Key:

hypixelio/_async/converters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__all__ = ("AsyncConverters",)
22

3-
import typing as t
3+
from typing import Any, Dict, Union, cast
44

55
import aiohttp
66

@@ -13,7 +13,7 @@ class AsyncConverters:
1313
url = API_PATH["MOJANG"]
1414

1515
@classmethod
16-
async def _fetch(cls, url: str) -> t.Union[dict, list]:
16+
async def _fetch(cls, url: str) -> Union[dict, list]:
1717
"""
1818
The internal function for fetching info from the Mojang API.
1919
@@ -24,7 +24,7 @@ async def _fetch(cls, url: str) -> t.Union[dict, list]:
2424
2525
Returns
2626
-------
27-
t.Union[dict, list]
27+
Union[dict, list]
2828
The JSON response from the Mojang API.
2929
"""
3030
session = aiohttp.ClientSession()
@@ -63,8 +63,8 @@ async def username_to_uuid(cls, username: str) -> str:
6363
str
6464
returns the converted UUID for the respective username.
6565
"""
66-
json = t.cast(
67-
t.Dict[str, t.Any],
66+
json = cast(
67+
Dict[str, Any],
6868
await AsyncConverters._fetch(
6969
AsyncConverters.url["username_to_uuid"].format(username)
7070
),

hypixelio/_async/utils.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__all__ = ("Utils",)
22

3-
import typing as t
3+
from typing import Optional, Union
44

55
import aiohttp
66

@@ -46,7 +46,7 @@ async def _crafatar_fetch(cls, url: str) -> str:
4646

4747
@staticmethod
4848
async def _filter_name_uuid(
49-
name: t.Optional[str] = None, uuid: t.Optional[str] = None
49+
name: Optional[str] = None, uuid: Optional[str] = None
5050
) -> str:
5151
if not name and not uuid:
5252
raise InvalidArgumentError(
@@ -78,25 +78,25 @@ def _form_crafatar_url(cls, route: str) -> str:
7878
@classmethod
7979
async def get_name_history(
8080
cls,
81-
name: t.Optional[str] = None,
82-
uuid: t.Optional[str] = None,
81+
name: Optional[str] = None,
82+
uuid: Optional[str] = None,
8383
changed_at: bool = False,
84-
) -> t.Union[list, dict]:
84+
) -> Union[list, dict]:
8585
"""
8686
Get the name history with records for a player.
8787
8888
Parameters
8989
----------
90-
name: t.Optional[str]
90+
name: Optional[str]
9191
The username of the player. Defaults to None.
92-
uuid: t.Optional[str]
92+
uuid: Optional[str]
9393
The UUID of the player. Defaults to None.
9494
changed_at: bool
9595
Toggle to true, if you need when the player changed name. Defaults to False.
9696
9797
Returns
9898
-------
99-
t.Union[list, dict]
99+
Union[list, dict]
100100
The list or dictionary with the name history and records.
101101
"""
102102
uuid = await cls._filter_name_uuid(name, uuid)
@@ -113,16 +113,16 @@ async def get_name_history(
113113

114114
@classmethod
115115
async def get_avatar(
116-
cls, name: t.Optional[str] = None, uuid: t.Optional[str] = None
116+
cls, name: Optional[str] = None, uuid: Optional[str] = None
117117
) -> str:
118118
"""
119119
Get the avatar of the specified player.
120120
121121
Parameters
122122
----------
123-
name: t.Optional[str]
123+
name: Optional[str]
124124
The username of the player. Defaults to None.
125-
uuid: t.Optional[str]
125+
uuid: Optional[str]
126126
The UUID of the player. Defaults to None.
127127
128128
Returns
@@ -137,16 +137,16 @@ async def get_avatar(
137137

138138
@classmethod
139139
async def get_head(
140-
cls, name: t.Optional[str] = None, uuid: t.Optional[str] = None
140+
cls, name: Optional[str] = None, uuid: Optional[str] = None
141141
) -> str:
142142
"""
143143
Get the head skin of the specified player.
144144
145145
Parameters
146146
----------
147-
name: t.Optional[str]
147+
name: Optional[str]
148148
The username of the player. Defaults to None.
149-
uuid: t.Optional[str]
149+
uuid: Optional[str]
150150
The UUID of the player. Defaults to None.
151151
152152
Returns
@@ -161,16 +161,16 @@ async def get_head(
161161

162162
@classmethod
163163
async def get_body(
164-
cls, name: t.Optional[str] = None, uuid: t.Optional[str] = None
164+
cls, name: Optional[str] = None, uuid: Optional[str] = None
165165
) -> str:
166166
"""
167167
Get the whole body's skin of the specified player
168168
169169
Parameters
170170
----------
171-
name: t.Optional[str]
171+
name: Optional[str]
172172
The username of the player. Defaults to None.
173-
uuid: t.Optional[str]
173+
uuid: Optional[str]
174174
The UUID of the player. Defaults to None.
175175
176176
Returns

hypixelio/exceptions.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ class InvalidArgumentError(Exception):
1919
...
2020

2121

22-
# API exceptions
2322
class APIError(Exception):
2423
"""Base class for all API exceptions."""
2524

2625
def __init__(self, service: str, reason: t.Optional[str] = None) -> None:
27-
"""
28-
Parameters
29-
----------
30-
reason: str
31-
The reason for the Error. Defaults to None.
32-
"""
3326
error = f"There was an issue with the {service} API."
3427
if reason:
3528
error += f" Reason: {reason}."
@@ -45,54 +38,30 @@ class HypixelAPIError(APIError):
4538
"""Raised when there is an issue with the Hypixel API or during fetch."""
4639

4740
def __init__(self, reason: t.Optional[str] = None) -> None:
48-
"""
49-
Parameters
50-
----------
51-
reason: str
52-
The reason for the Error. Defaults to None.
53-
"""
5441
super().__init__("Hypixel", reason)
5542

5643

5744
class CrafatarAPIError(APIError):
5845
"""Raised during issues faced by Crafatar API."""
5946

6047
def __init__(self, reason: t.Optional[str] = None) -> None:
61-
"""
62-
Parameters
63-
----------
64-
reason: str
65-
The reason for the Error. Defaults to None.
66-
"""
6748
super().__init__("Crafatar", reason)
6849

6950

7051
class MojangAPIError(APIError):
7152
"""Raised when the Mojang API is facing some problems."""
7253

7354
def __init__(self, reason: t.Optional[str] = None) -> None:
74-
"""
75-
Parameters
76-
----------
77-
reason: str
78-
The reason for the Error. Defaults to None.
79-
"""
8055
super().__init__("Mojang", reason)
8156

8257

8358
# Rate-limit exception
8459
class RateLimitError(Exception):
85-
"""Raised when the Rate-limit for the Hypixel API is hit."""
60+
"""Raised when the ratelimit for the hypixel API is hit."""
8661

8762
def __init__(self, retry_after: datetime) -> None:
88-
"""
89-
Parameters
90-
----------
91-
retry_after: datetime
92-
The time when the API will be available again for fetching.
93-
"""
9463
error = (
95-
"The rate-limit for the Hypixel API was hit. Try again after"
64+
"The ratelimit for the hypixel API was hit. Try again after"
9665
f"{retry_after.strftime('%Y-%m-%d %H:%M:%S')}."
9766
)
9867

@@ -110,14 +79,6 @@ class PlayerNotFoundError(Exception):
11079
def __init__(
11180
self, reason: t.Optional[str] = None, user: t.Optional[str] = None
11281
) -> None:
113-
"""
114-
Parameters
115-
----------
116-
reason: str
117-
The reason for the error.
118-
user: t.Optional[str]
119-
The user not found when searched for.
120-
"""
12182
error = "Player not found."
12283
if reason:
12384
error += f" {reason}."

hypixelio/lib/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ def get_key_info(self, api_key: Optional[str] = None) -> Key:
175175
Key
176176
The Key object created for the API key specified.
177177
"""
178-
if not api_key:
179-
api_key = random.choice(self._api_key)
178+
api_key = api_key or random.choice(self._api_key)
180179

181180
json = self._fetch(self.url["api_key"], {"key": api_key})
182181
return Key(json["record"])

0 commit comments

Comments
 (0)