mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-30 20:48:12 +00:00
make latency an async method and fix typing
This commit is contained in:
parent
8cbc6096dc
commit
c04b69fcb7
1 changed files with 10 additions and 14 deletions
|
@ -94,7 +94,7 @@ class HTTPClient:
|
||||||
_last_ping: float
|
_last_ping: float
|
||||||
_latency: float
|
_latency: float
|
||||||
|
|
||||||
def __new__(cls, **kwargs) -> Awaitable[HTTPClient]:
|
def __new__(cls, **kwargs: Any) -> Awaitable[HTTPClient]:
|
||||||
# Basically async def __init__
|
# Basically async def __init__
|
||||||
return cls.__async_init(**kwargs)
|
return cls.__async_init(**kwargs)
|
||||||
|
|
||||||
|
@ -182,22 +182,18 @@ class HTTPClient:
|
||||||
remaining = self._rates.remaining
|
remaining = self._rates.remaining
|
||||||
return remaining is not None and remaining < 2
|
return remaining is not None and remaining < 2
|
||||||
|
|
||||||
@property
|
async def latency(self) -> float:
|
||||||
def latency(self) -> Awaitable[float]:
|
last_ping = self._last_ping
|
||||||
async def inner() -> float:
|
|
||||||
last_ping = self._last_ping
|
|
||||||
|
|
||||||
# If there was no ping or the last ping was more than 5 seconds ago.
|
# If there was no ping or the last ping was more than 5 seconds ago.
|
||||||
if not last_ping or time.monotonic() > last_ping + 5 or self.is_ratelimited:
|
if not last_ping or time.monotonic() > last_ping + 5 or self.is_ratelimited:
|
||||||
self._last_ping = time.monotonic()
|
self._last_ping = time.monotonic()
|
||||||
|
|
||||||
start = time.monotonic()
|
start = time.monotonic()
|
||||||
await self.request("GET", "/")
|
await self.request("GET", "/")
|
||||||
self._latency = time.monotonic() - start
|
self._latency = time.monotonic() - start
|
||||||
|
|
||||||
return self._latency
|
return self._latency
|
||||||
|
|
||||||
return inner()
|
|
||||||
|
|
||||||
async def request(
|
async def request(
|
||||||
self, method: Literal["GET", "POST", "PUT", "DELETE", "PATCH"], path: str, /, **kwargs: Any
|
self, method: Literal["GET", "POST", "PUT", "DELETE", "PATCH"], path: str, /, **kwargs: Any
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue