From 88516d83b9591623da74bee644b6b04cb2159918 Mon Sep 17 00:00:00 2001 From: janaSunrise Date: Wed, 24 Mar 2021 11:05:15 +0530 Subject: [PATCH 1/3] Remove caching for async. The library is buggy. --- Pipfile | 3 +- hypixelio/__init__.py | 2 +- hypixelio/ext/asyncio/client.py | 110 ++++++++++++-------------------- hypixelio/lib/client.py | 1 - hypixelio/utils/constants.py | 1 + setup.py | 1 - 6 files changed, 43 insertions(+), 75 deletions(-) diff --git a/Pipfile b/Pipfile index 3af1c83..677d069 100644 --- a/Pipfile +++ b/Pipfile @@ -20,8 +20,7 @@ hypixelio = {editable = true, extras = ["all"], path = "."} [packages] requests = "==2.25.1" requests-cache = "==0.5.2" -aiohttp = {extras = ["speedups"], version = "*"} -aiohttp-client-cache = "*" +aiohttp = "==3.7.4post0" [scripts] lint = "pre-commit run --all-files" diff --git a/hypixelio/__init__.py b/hypixelio/__init__.py index 384391e..fe1c0b7 100644 --- a/hypixelio/__init__.py +++ b/hypixelio/__init__.py @@ -6,7 +6,7 @@ __author__ = "Sunrit Jana" __email__ = "warriordefenderz@gmail.com" -__version__ = "1.2.4" +__version__ = "1.2.6" __license__ = "GPL-3.0 License" __copyright__ = "Copyright 2021 Sunrit Jana" diff --git a/hypixelio/ext/asyncio/client.py b/hypixelio/ext/asyncio/client.py index 8b664cc..afb06d7 100644 --- a/hypixelio/ext/asyncio/client.py +++ b/hypixelio/ext/asyncio/client.py @@ -5,7 +5,7 @@ import typing as t from datetime import datetime, timedelta -import aiohttp_client_cache +import aiohttp from hypixelio.endpoints import API_PATH from hypixelio.exceptions import ( @@ -18,7 +18,6 @@ from hypixelio.lib.converters import Converters from hypixelio.models import ( boosters, - caching, find_guild, friends, games, @@ -33,7 +32,7 @@ ) from hypixelio.utils.constants import ( HYPIXEL_API, - TIMEOUT, + TIMEOUT ) from hypixelio.utils.helpers import ( form_url @@ -57,20 +56,11 @@ class AsyncClient: >>> client = AsyncClient(api_key=["123-456", "789-000", "568-908"]) - f you want to enable caching, Here's how to do it - >>> client = AsyncClient(cache=True) - - And configuring cache - >>> from hypixelio.models.caching import Caching, CacheBackend - >>> cache_cfg = Caching(cache_name="my-cache", backend=CacheBackend.sqlite, expire_after=10) - >>> client = AsyncClient(cache=True, cache_config=cache_cfg) - - Notes ----- Keep in mind that, your keys wouldn't work if you're banned from hypixel, or if they're expired. """ - def __init__(self, api_key: t.Union[str, list], cache: bool = False, cache_config: caching.Caching = None) -> None: + def __init__(self, api_key: t.Union[str, list]) -> None: """ Parameters ---------- @@ -79,39 +69,20 @@ def __init__(self, api_key: t.Union[str, list], cache: bool = False, cache_confi """ self.url = API_PATH["HYPIXEL"] + self.__session = aiohttp.ClientSession() self.__lock = asyncio.Lock() self.requests_remaining = -1 self.total_requests = 0 - self._ratelimit_reset = datetime(1998, 1, 1) self.retry_after = datetime(1998, 1, 1) if not isinstance(api_key, list): self.__api_key = [api_key] - self.cache = cache - - if cache: - if cache_config is None: - cache_config = caching.Caching(expire_after=30, old_data_on_error=True) - - if cache_config.backend == "sqlite": - self.cache = aiohttp_client_cache.backends.SQLiteBackend( - cache_name=cache_config.cache_name, expire_after=cache_config.expire_after - ) - elif cache_config.backend == "redis": - self.cache = aiohttp_client_cache.backends.RedisBackend( - cache_name=cache_config.cache_name, expire_after=cache_config.expire_after - ) - elif cache_config.backend == "mongodb": - self.cache = aiohttp_client_cache.backends.MongoDBBackend( - cache_name=cache_config.cache_name, expire_after=cache_config.expire_after - ) - else: - self.cache = aiohttp_client_cache.backends.CacheBackend( - cache_name=cache_config.cache_name, expire_after=cache_config.expire_after - ) + async def close(self) -> None: + """Close the AIOHTTP sessions to prevent memory leaks.""" + await self.__session.close() def add_key(self, key: t.Union[str, list]) -> None: if isinstance(key, str): @@ -166,40 +137,39 @@ async def _fetch(self, url: str, data: dict = None, key: bool = True) -> t.Tuple url = form_url(HYPIXEL_API, url, data) async with self.__lock: - async with aiohttp_client_cache.CachedSession(cache=self.cache) as session: - async with session.get(url, timeout=TIMEOUT, headers=headers) as response: - if response.status == 429: - self.requests_remaining = 0 - self.retry_after = datetime.now() + timedelta(seconds=int(response.headers["Retry-After"])) - raise RateLimitError( - f"Out of Requests! " - f"{datetime.now() + timedelta(seconds=int(response.headers['Retry-After']))}" - ) - - if response.status == 400: - raise HypixelAPIError(reason="Invalid key specified!") - - if key and "RateLimit-Limit" in response.headers: - if self.total_requests == 0: - self.total_requests = int(response.headers["RateLimit-Limit"]) - - self.requests_remaining = int(response.headers["RateLimit-Remaining"]) - self._ratelimit_reset = datetime.now() + timedelta( - seconds=int(response.headers["RateLimit-Reset"])) - - try: - json = await response.json() - except Exception as exception: - raise HypixelAPIError(f"{exception}") - else: - if not json["success"]: - reason = "Something in the API has problem." - if json["cause"] is not None: - reason += f" Reason given: {json['cause']}" - - raise HypixelAPIError(reason=reason) - - return json + async with self.__session.get(url, timeout=TIMEOUT, headers=headers) as response: + if response.status == 429: + self.requests_remaining = 0 + self.retry_after = datetime.now() + timedelta(seconds=int(response.headers["Retry-After"])) + raise RateLimitError( + f"Out of Requests! " + f"{datetime.now() + timedelta(seconds=int(response.headers['Retry-After']))}" + ) + + if response.status == 400: + raise HypixelAPIError(reason="Invalid key specified!") + + if key and "RateLimit-Limit" in response.headers: + if self.total_requests == 0: + self.total_requests = int(response.headers["RateLimit-Limit"]) + + self.requests_remaining = int(response.headers["RateLimit-Remaining"]) + self._ratelimit_reset = datetime.now() + timedelta( + seconds=int(response.headers["RateLimit-Reset"])) + + try: + json = await response.json() + except Exception as exception: + raise HypixelAPIError(f"{exception}") + else: + if not json["success"]: + reason = "Something in the API has problem." + if json["cause"] is not None: + reason += f" Reason given: {json['cause']}" + + raise HypixelAPIError(reason=reason) + + return json @staticmethod def _filter_name_uuid(name: t.Optional[str] = None, uuid: t.Optional[str] = None) -> str: diff --git a/hypixelio/lib/client.py b/hypixelio/lib/client.py index 0b675ac..f6d143b 100644 --- a/hypixelio/lib/client.py +++ b/hypixelio/lib/client.py @@ -85,7 +85,6 @@ def __init__(self, api_key: t.Union[str, list], cache: bool = False, cache_confi self.requests_remaining = -1 self.total_requests = 0 - self._ratelimit_reset = datetime(1998, 1, 1) self.retry_after = datetime(1998, 1, 1) diff --git a/hypixelio/utils/constants.py b/hypixelio/utils/constants.py index 3d5cb5f..798d0ce 100644 --- a/hypixelio/utils/constants.py +++ b/hypixelio/utils/constants.py @@ -13,6 +13,7 @@ "GUILD_COLORS__TAG", "SKYWARS_PRESTIGES_RANKS", "SKYWARS_PRESTIGE_COLOR", + "TIMEOUT" ) HYPIXEL_API = "https://api.hypixel.net" diff --git a/setup.py b/setup.py index cc70648..08c4a9c 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,6 @@ "cchardet", ], "cache": ["requests-cache==0.5.2", 'boto3', 'pymongo', 'redis'], - "async-cache": ["aiohttp-client-cache==0.2.2", 'aiosqlite', 'boto3', 'motor', 'aioredis'] } extras_require['all'] = list(chain.from_iterable(extras_require.values())) From 2aee1a81d3e30ef63e276c902a74897030f1ee60 Mon Sep 17 00:00:00 2001 From: janaSunrise Date: Wed, 24 Mar 2021 11:10:18 +0530 Subject: [PATCH 2/3] Update README. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a3f644d..bd36b03 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,12 @@ python3 -m pip install -U git+https://github.com/janaSunrise/HypixelIO # Or use [speedups] to speed up only for async features python3 -m pip install -U "HypixelIO[speedups]" + +# Take advantage of caching +python3 -m pip install -U "HypixelIO[cache]" + +# Or get all the features! +python3 -m pip install -U "HypixelIO[all]" ``` ## Usage From 5065eabd178f6452a752c65ad5f90070749c6367 Mon Sep 17 00:00:00 2001 From: janaSunrise Date: Wed, 24 Mar 2021 11:35:27 +0530 Subject: [PATCH 3/3] Fix the Timeout issue in aiohttp. --- CHANGELOG.md | 22 ++++ Pipfile.lock | 193 +------------------------------- hypixelio/ext/asyncio/client.py | 15 ++- 3 files changed, 37 insertions(+), 193 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d94d023..68936a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.2.6](https://github.com/janaSunrise/HypixelIO/releases/tag/v1.2.6) - 24-03-2021 + +## Added + +None + +## Fixed + +- Async caching removed due to bugs. +- Variables exposed and bugs not allowing fetching in async. +- Cleaned async client. + ## [1.2.5](https://github.com/janaSunrise/HypixelIO/releases/tag/v1.2.5) - 23-03-2021 ## Added @@ -26,6 +38,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Made the documentation better, and changed the theme. - Tweaked and fixed things here and there. +## Added + +None + +## Fixed + +- Async caching removed due to bugginess +- Variables exposed and bugs not allowing fetching in async. +- Cleaned async client. + ## [1.2.3](https://github.com/janaSunrise/HypixelIO/releases/tag/v1.2.3) - 11-03-2021 ## Added diff --git a/Pipfile.lock b/Pipfile.lock index 324f887..18084d3 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "083c7ae9ab1fbb9ead1023f179d04040b0256f143a9fcb962ca282fa7af269c8" + "sha256": "a616a9ebf9c4deec62ad0f4b8a21d2a5f25de2a9b37b6c22a0856f1009ef4a5e" }, "pipfile-spec": 6, "requires": {}, @@ -14,17 +14,7 @@ ] }, "default": { - "aiodns": { - "hashes": [ - "sha256:815fdef4607474295d68da46978a54481dd1e7be153c7d60f9e72773cd38d77d", - "sha256:aaa5ac584f40fe778013df0aa6544bf157799bd3f608364b451840ed2c8688de" - ], - "version": "==2.0.0" - }, "aiohttp": { - "extras": [ - "speedups" - ], "hashes": [ "sha256:02f46fc0e3c5ac58b80d4d56eb0a7c7d97fcef69ace9326289fb9f1955e65cfe", "sha256:0563c1b3826945eecd62186f3f5c7d31abb7391fedc893b7e2b26303b5a9f3fe", @@ -65,15 +55,7 @@ "sha256:fe60131d21b31fd1a14bd43e6bb88256f69dfc3188b3a89d736d6c71ed43ec95" ], "index": "pypi", - "version": "==3.7.4.post0" - }, - "aiohttp-client-cache": { - "hashes": [ - "sha256:2bc22ccc1bfd82be21c3b774a526d912c87c69bdaee99bceb12d3333b19bb0f3", - "sha256:a280ae6092adf5b1eba3974d4e87f5749bd5b0a113b97fbfde5e2dd617c41a42" - ], - "index": "pypi", - "version": "==0.2.2" + "version": "==3.7.4post0" }, "async-timeout": { "hashes": [ @@ -91,82 +73,6 @@ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==20.3.0" }, - "brotlipy": { - "hashes": [ - "sha256:07194f4768eb62a4f4ea76b6d0df6ade185e24ebd85877c351daa0a069f1111a", - "sha256:08a16ebe2ffc52f645c076f96b138f185e74e5d59b4a65e84af17d5997d82890", - "sha256:091b299bf36dd6ef7a06570dbc98c0f80a504a56c5b797f31934d2ad01ae7d17", - "sha256:09ec3e125d16749b31c74f021aba809541b3564e5359f8c265cbae442810b41a", - "sha256:0be698678a114addcf87a4b9496c552c68a2c99bf93cf8e08f5738b392e82057", - "sha256:0fa6088a9a87645d43d7e21e32b4a6bf8f7c3939015a50158c10972aa7f425b7", - "sha256:1379347337dc3d20b2d61456d44ccce13e0625db2611c368023b4194d5e2477f", - "sha256:1ea4e578241504b58f2456a6c69952c88866c794648bdc74baee74839da61d44", - "sha256:22a53ccebcce2425e19f99682c12be510bf27bd75c9b77a1720db63047a77554", - "sha256:2699945a0a992c04fc7dc7fa2f1d0575a2c8b4b769f2874a08e8eae46bef36ae", - "sha256:2a80319ae13ea8dd60ecdc4f5ccf6da3ae64787765923256b62c598c5bba4121", - "sha256:2e5c64522364a9ebcdf47c5744a5ddeb3f934742d31e61ebfbbc095460b47162", - "sha256:36def0b859beaf21910157b4c33eb3b06d8ce459c942102f16988cca6ea164df", - "sha256:3a3e56ced8b15fbbd363380344f70f3b438e0fd1fcf27b7526b6172ea950e867", - "sha256:3c1d5e2cf945a46975bdb11a19257fa057b67591eb232f393d260e7246d9e571", - "sha256:4864ac52c116ea3e3a844248a9c9fbebb8797891cbca55484ecb6eed3ebeba24", - "sha256:4bac11c1ffba9eaa2894ec958a44e7f17778b3303c2ee9f99c39fcc511c26668", - "sha256:4e4638b49835d567d447a2cfacec109f9a777f219f071312268b351b6839436d", - "sha256:50ca336374131cfad20612f26cc43c637ac0bfd2be3361495e99270883b52962", - "sha256:5de6f7d010b7558f72f4b061a07395c5c3fd57f0285c5af7f126a677b976a868", - "sha256:637847560d671657f993313ecc6c6c6666a936b7a925779fd044065c7bc035b9", - "sha256:653faef61241bf8bf99d73ca7ec4baa63401ba7b2a2aa88958394869379d67c7", - "sha256:786afc8c9bd67de8d31f46e408a3386331e126829114e4db034f91eacb05396d", - "sha256:79aaf217072840f3e9a3b641cccc51f7fc23037496bd71e26211856b93f4b4cb", - "sha256:7b21341eab7c939214e457e24b265594067a6ad268305289148ebaf2dacef325", - "sha256:7e31f7adcc5851ca06134705fcf3478210da45d35ad75ec181e1ce9ce345bb38", - "sha256:890b973039ba26c3ad2e86e8908ab527ed64f9b1357f81a676604da8088e4bf9", - "sha256:8b39abc3256c978f575df5cd7893153277216474f303e26f0e43ba3d3969ef96", - "sha256:9448227b0df082e574c45c983fa5cd4bda7bfb11ea6b59def0940c1647be0c3c", - "sha256:96bc59ff9b5b5552843dc67999486a220e07a0522dddd3935da05dc194fa485c", - "sha256:a07647886e24e2fb2d68ca8bf3ada398eb56fd8eac46c733d4d95c64d17f743b", - "sha256:af65d2699cb9f13b26ec3ba09e75e80d31ff422c03675fcb36ee4dabe588fdc2", - "sha256:b4c98b0d2c9c7020a524ca5bbff42027db1004c6571f8bc7b747f2b843128e7a", - "sha256:c6cc0036b1304dd0073eec416cb2f6b9e37ac8296afd9e481cac3b1f07f9db25", - "sha256:d2c1c724c4ac375feb2110f1af98ecdc0e5a8ea79d068efb5891f621a5b235cb", - "sha256:dc6c5ee0df9732a44d08edab32f8a616b769cc5a4155a12d2d010d248eb3fb07", - "sha256:fd1d1c64214af5d90014d82cee5d8141b13d44c92ada7a0c0ec0679c6f15a471" - ], - "version": "==0.7.0" - }, - "cchardet": { - "hashes": [ - "sha256:0b859069bbb9d27c78a2c9eb997e6f4b738db2d7039a03f8792b4058d61d1109", - "sha256:228d2533987c450f39acf7548f474dd6814c446e9d6bd228e8f1d9a2d210f10b", - "sha256:2309ff8fc652b0fc3c0cff5dbb172530c7abb92fe9ba2417c9c0bcf688463c1c", - "sha256:24974b3e40fee9e7557bb352be625c39ec6f50bc2053f44a3d1191db70b51675", - "sha256:273699c4e5cd75377776501b72a7b291a988c6eec259c29505094553ee505597", - "sha256:27a9ba87c9f99e0618e1d3081189b1217a7d110e5c5597b0b7b7c3fedd1c340a", - "sha256:302aa443ae2526755d412c9631136bdcd1374acd08e34f527447f06f3c2ddb98", - "sha256:45456c59ec349b29628a3c6bfb86d818ec3a6fbb7eb72de4ff3bd4713681c0e3", - "sha256:48ba829badef61441e08805cfa474ccd2774be2ff44b34898f5854168c596d4d", - "sha256:50ad671e8d6c886496db62c3bd68b8d55060688c655873aa4ce25ca6105409a1", - "sha256:54341e7e1ba9dc0add4c9d23b48d3a94e2733065c13920e85895f944596f6150", - "sha256:54d0b26fd0cd4099f08fb9c167600f3e83619abefeaa68ad823cc8ac1f7bcc0c", - "sha256:5a25f9577e9bebe1a085eec2d6fdd72b7a9dd680811bba652ea6090fb2ff472f", - "sha256:6b6397d8a32b976a333bdae060febd39ad5479817fabf489e5596a588ad05133", - "sha256:70eeae8aaf61192e9b247cf28969faef00578becd2602526ecd8ae7600d25e0e", - "sha256:80e6faae75ecb9be04a7b258dc4750d459529debb6b8dee024745b7b5a949a34", - "sha256:90086e5645f8a1801350f4cc6cb5d5bf12d3fa943811bb08667744ec1ecc9ccd", - "sha256:a39526c1c526843965cec589a6f6b7c2ab07e3e56dc09a7f77a2be6a6afa4636", - "sha256:b154effa12886e9c18555dfc41a110f601f08d69a71809c8d908be4b1ab7314f", - "sha256:b59ddc615883835e03c26f81d5fc3671fab2d32035c87f50862de0da7d7db535", - "sha256:bd7f262f41fd9caf5a5f09207a55861a67af6ad5c66612043ed0f81c58cdf376", - "sha256:c428b6336545053c2589f6caf24ea32276c6664cb86db817e03a94c60afa0eaf", - "sha256:c6f70139aaf47ffb94d89db603af849b82efdf756f187cdd3e566e30976c519f", - "sha256:c96aee9ebd1147400e608a3eff97c44f49811f8904e5a43069d55603ac4d8c97", - "sha256:ec3eb5a9c475208cf52423524dcaf713c394393e18902e861f983c38eeb77f18", - "sha256:eee4f5403dc3a37a1ca9ab87db32b48dc7e190ef84601068f45397144427cc5e", - "sha256:f16517f3697569822c6d09671217fdeab61dfebc7acb5068634d6b0728b86c0b", - "sha256:f86e0566cb61dc4397297696a4a1b30f6391b50bc52b4f073507a48466b6255a", - "sha256:fdac1e4366d0579fff056d1280b8dc6348be964fda8ebb627c0269e097ab37fa" - ], - "version": "==2.1.7" - }, "certifi": { "hashes": [ "sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c", @@ -174,48 +80,6 @@ ], "version": "==2020.12.5" }, - "cffi": { - "hashes": [ - "sha256:005a36f41773e148deac64b08f233873a4d0c18b053d37da83f6af4d9087b813", - "sha256:0857f0ae312d855239a55c81ef453ee8fd24136eaba8e87a2eceba644c0d4c06", - "sha256:1071534bbbf8cbb31b498d5d9db0f274f2f7a865adca4ae429e147ba40f73dea", - "sha256:158d0d15119b4b7ff6b926536763dc0714313aa59e320ddf787502c70c4d4bee", - "sha256:1f436816fc868b098b0d63b8920de7d208c90a67212546d02f84fe78a9c26396", - "sha256:2894f2df484ff56d717bead0a5c2abb6b9d2bf26d6960c4604d5c48bbc30ee73", - "sha256:29314480e958fd8aab22e4a58b355b629c59bf5f2ac2492b61e3dc06d8c7a315", - "sha256:34eff4b97f3d982fb93e2831e6750127d1355a923ebaeeb565407b3d2f8d41a1", - "sha256:35f27e6eb43380fa080dccf676dece30bef72e4a67617ffda586641cd4508d49", - "sha256:3d3dd4c9e559eb172ecf00a2a7517e97d1e96de2a5e610bd9b68cea3925b4892", - "sha256:43e0b9d9e2c9e5d152946b9c5fe062c151614b262fda2e7b201204de0b99e482", - "sha256:48e1c69bbacfc3d932221851b39d49e81567a4d4aac3b21258d9c24578280058", - "sha256:51182f8927c5af975fece87b1b369f722c570fe169f9880764b1ee3bca8347b5", - "sha256:58e3f59d583d413809d60779492342801d6e82fefb89c86a38e040c16883be53", - "sha256:5de7970188bb46b7bf9858eb6890aad302577a5f6f75091fd7cdd3ef13ef3045", - "sha256:65fa59693c62cf06e45ddbb822165394a288edce9e276647f0046e1ec26920f3", - "sha256:69e395c24fc60aad6bb4fa7e583698ea6cc684648e1ffb7fe85e3c1ca131a7d5", - "sha256:6c97d7350133666fbb5cf4abdc1178c812cb205dc6f41d174a7b0f18fb93337e", - "sha256:6e4714cc64f474e4d6e37cfff31a814b509a35cb17de4fb1999907575684479c", - "sha256:72d8d3ef52c208ee1c7b2e341f7d71c6fd3157138abf1a95166e6165dd5d4369", - "sha256:8ae6299f6c68de06f136f1f9e69458eae58f1dacf10af5c17353eae03aa0d827", - "sha256:8b198cec6c72df5289c05b05b8b0969819783f9418e0409865dac47288d2a053", - "sha256:99cd03ae7988a93dd00bcd9d0b75e1f6c426063d6f03d2f90b89e29b25b82dfa", - "sha256:9cf8022fb8d07a97c178b02327b284521c7708d7c71a9c9c355c178ac4bbd3d4", - "sha256:9de2e279153a443c656f2defd67769e6d1e4163952b3c622dcea5b08a6405322", - "sha256:9e93e79c2551ff263400e1e4be085a1210e12073a31c2011dbbda14bda0c6132", - "sha256:9ff227395193126d82e60319a673a037d5de84633f11279e336f9c0f189ecc62", - "sha256:a465da611f6fa124963b91bf432d960a555563efe4ed1cc403ba5077b15370aa", - "sha256:ad17025d226ee5beec591b52800c11680fca3df50b8b29fe51d882576e039ee0", - "sha256:afb29c1ba2e5a3736f1c301d9d0abe3ec8b86957d04ddfa9d7a6a42b9367e396", - "sha256:b85eb46a81787c50650f2392b9b4ef23e1f126313b9e0e9013b35c15e4288e2e", - "sha256:bb89f306e5da99f4d922728ddcd6f7fcebb3241fc40edebcb7284d7514741991", - "sha256:cbde590d4faaa07c72bf979734738f328d239913ba3e043b1e98fe9a39f8b2b6", - "sha256:cd2868886d547469123fadc46eac7ea5253ea7fcb139f12e1dfc2bbd406427d1", - "sha256:d42b11d692e11b6634f7613ad8df5d6d5f8875f5d48939520d351007b3c13406", - "sha256:f2d45f97ab6bb54753eab54fffe75aaf3de4ff2341c9daee1987ee1837636f1d", - "sha256:fd78e5fee591709f32ef6edb9a015b4aa1a5022598e36227500c8f4e02328d9c" - ], - "version": "==1.14.5" - }, "chardet": { "hashes": [ "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa", @@ -275,54 +139,6 @@ "markers": "python_version >= '3.6'", "version": "==5.1.0" }, - "pycares": { - "hashes": [ - "sha256:050f00b39ed77ea8a4e555f09417d4b1a6b5baa24bb9531a3e15d003d2319b3f", - "sha256:0a24d2e580a8eb567140d7b69f12cb7de90c836bd7b6488ec69394d308605ac3", - "sha256:0c5bd1f6f885a219d5e972788d6eef7b8043b55c3375a845e5399638436e0bba", - "sha256:11c628402cc8fc8ef461076d4e47f88afc1f8609989ebbff0dbffcd54c97239f", - "sha256:18dfd4fd300f570d6c4536c1d987b7b7673b2a9d14346592c5d6ed716df0d104", - "sha256:1917b82494907a4a342db420bc4dd5bac355a5fa3984c35ba9bf51422b020b48", - "sha256:1b90fa00a89564df059fb18e796458864cc4e00cb55e364dbf921997266b7c55", - "sha256:1d8d177c40567de78108a7835170f570ab04f09084bfd32df9919c0eaec47aa1", - "sha256:236286f81664658b32c141c8e79d20afc3d54f6e2e49dfc8b702026be7265855", - "sha256:2e4f74677542737fb5af4ea9a2e415ec5ab31aa67e7b8c3c969fdb15c069f679", - "sha256:48a7750f04e69e1f304f4332b755728067e7c4b1abe2760bba1cacd9ff7a847a", - "sha256:7d86e62b700b21401ffe7fd1bbfe91e08489416fecae99c6570ab023c6896022", - "sha256:7e2d7effd08d2e5a3cb95d98a7286ebab71ab2fbce84fa93cc2dd56caf7240dd", - "sha256:81edb016d9e43dde7473bc3999c29cdfee3a6b67308fed1ea21049f458e83ae0", - "sha256:96c90e11b4a4c7c0b8ff5aaaae969c5035493136586043ff301979aae0623941", - "sha256:9a0a1845f8cb2e62332bca0aaa9ad5494603ac43fb60d510a61d5b5b170d7216", - "sha256:a05bbfdfd41f8410a905a818f329afe7510cbd9ee65c60f8860a72b6c64ce5dc", - "sha256:a5089fd660f0b0d228b14cdaa110d0d311edfa5a63f800618dbf1321dcaef66b", - "sha256:c457a709e6f2befea7e2996c991eda6d79705dd075f6521593ba6ebc1485b811", - "sha256:c5cb72644b04e5e5abfb1e10a0e7eb75da6684ea0e60871652f348e412cf3b11", - "sha256:cce46dd4717debfd2aab79d6d7f0cbdf6b1e982dc4d9bebad81658d59ede07c2", - "sha256:cfdd1f90bcf373b00f4b2c55ea47868616fe2f779f792fc913fa82a3d64ffe43", - "sha256:d88a279cbc5af613f73e86e19b3f63850f7a2e2736e249c51995dedcc830b1bb", - "sha256:eba9a9227438da5e78fc8eee32f32eb35d9a50cf0a0bd937eb6275c7cc3015fe", - "sha256:eee7b6a5f5b5af050cb7d66ab28179287b416f06d15a8974ac831437fec51336", - "sha256:f41ac1c858687e53242828c9f59c2e7b0b95dbcd5bdd09c7e5d3c48b0f89a25a", - "sha256:f8deaefefc3a589058df1b177275f79233e8b0eeee6734cf4336d80164ecd022", - "sha256:fa78e919f3bd7d6d075db262aa41079b4c02da315c6043c6f43881e2ebcdd623", - "sha256:fadb97d2e02dabdc15a0091591a972a938850d79ddde23d385d813c1731983f0" - ], - "version": "==3.1.1" - }, - "pycparser": { - "hashes": [ - "sha256:2d475327684562c3a96cc71adf7dc8c4f0565175cf86b6d7a404ff4c771f15f0", - "sha256:7582ad22678f0fcd81102833f60ef8d0e57288b6b5fb00323d101be910e35705" - ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", - "version": "==2.20" - }, - "python-forge": { - "hashes": [ - "sha256:bf91f9a42150d569c2e9a0d90ab60a8cbed378bdf185e5120532a3481067395c" - ], - "version": "==18.6.0" - }, "requests": { "hashes": [ "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804", @@ -401,9 +217,6 @@ }, "develop": { "aiohttp": { - "extras": [ - "speedups" - ], "hashes": [ "sha256:02f46fc0e3c5ac58b80d4d56eb0a7c7d97fcef69ace9326289fb9f1955e65cfe", "sha256:0563c1b3826945eecd62186f3f5c7d31abb7391fedc893b7e2b26303b5a9f3fe", @@ -444,7 +257,7 @@ "sha256:fe60131d21b31fd1a14bd43e6bb88256f69dfc3188b3a89d736d6c71ed43ec95" ], "index": "pypi", - "version": "==3.7.4.post0" + "version": "==3.7.4post0" }, "aiohttp-theme": { "hashes": [ diff --git a/hypixelio/ext/asyncio/client.py b/hypixelio/ext/asyncio/client.py index afb06d7..03b8395 100644 --- a/hypixelio/ext/asyncio/client.py +++ b/hypixelio/ext/asyncio/client.py @@ -41,7 +41,13 @@ class AsyncClient: """The client for this wrapper, that handles the requests, authentication, loading and usages of the end user. - +from hypixelio.ext.asyncio import AsyncClient +c = AsyncClient("1c30559c-ade3-47dd-9228-9d8c21ea7349") +import asyncio +async def main(): + p = await c.get_player("Sadashi_") + return p +asyncio.run(main()) Examples -------- Import the async client first. @@ -69,7 +75,7 @@ def __init__(self, api_key: t.Union[str, list]) -> None: """ self.url = API_PATH["HYPIXEL"] - self.__session = aiohttp.ClientSession() + self.__session = None self.__lock = asyncio.Lock() self.requests_remaining = -1 @@ -119,6 +125,9 @@ async def _fetch(self, url: str, data: dict = None, key: bool = True) -> t.Tuple `t.Tuple[dict, bool]` The JSON Response from the Fetch Done to the API and the SUCCESS Value from the Response. """ + if not self.__session: + self.__session = aiohttp.ClientSession() + if ( self.requests_remaining != -1 and # noqa: W504 (self.requests_remaining == 0 and self._ratelimit_reset > datetime.now()) or # noqa: W504 @@ -137,7 +146,7 @@ async def _fetch(self, url: str, data: dict = None, key: bool = True) -> t.Tuple url = form_url(HYPIXEL_API, url, data) async with self.__lock: - async with self.__session.get(url, timeout=TIMEOUT, headers=headers) as response: + async with self.__session.get(url, headers=headers, timeout=TIMEOUT) as response: if response.status == 429: self.requests_remaining = 0 self.retry_after = datetime.now() + timedelta(seconds=int(response.headers["Retry-After"]))