From 0b23248decffe23b88c16b64c193761eccfb7432 Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:35:39 +0900 Subject: [PATCH 1/7] Delete unittest_github_action.py --- test/unittest_github_action.py | 125 --------------------------------- 1 file changed, 125 deletions(-) delete mode 100644 test/unittest_github_action.py diff --git a/test/unittest_github_action.py b/test/unittest_github_action.py deleted file mode 100644 index 3594d3b..0000000 --- a/test/unittest_github_action.py +++ /dev/null @@ -1,125 +0,0 @@ -import solvedac_community as SolvedAC -import asyncio -import unittest - -loop = asyncio.get_event_loop() - -client = SolvedAC.Client() - -user_list = ["devruby"] - -background_list = [ - "users_100k" -] - -badge_list = ["ghudegy2023"] - -query_list = ["graph"] - -search_query_list = zip( - ("tree",), - ("asc",), - (1,), - ("id",), -) - -problem_id_array_list = [ - [1000, 1001, 1002, 1003, 1004], -] - -problem_id_list = [1000] - -DEBUG = True - - -class Test(unittest.TestCase): - def test_get_user(self): - for user in user_list: - if DEBUG: - print(loop.run_until_complete(client.get_user(user))) - else: - (loop.run_until_complete(client.get_user(user))) - - def test_get_user_organizations(self): - for user in user_list: - if DEBUG: - print(loop.run_until_complete(client.get_user_organizations(user))) - else: - (loop.run_until_complete(client.get_user_organizations(user))) - - def test_get_user_problem_stats(self): - for user in user_list: - if DEBUG: - print(loop.run_until_complete(client.get_user_problem_stats(user))) - else: - (loop.run_until_complete(client.get_user_problem_stats(user))) - - def test_get_background(self): - for background in background_list: - if DEBUG: - print(loop.run_until_complete(client.get_background(background))) - else: - (loop.run_until_complete(client.get_background(background))) - - def test_get_badge(self): - for badge in badge_list: - if DEBUG: - print(loop.run_until_complete(client.get_badge(badge))) - else: - (loop.run_until_complete(client.get_badge(badge))) - - def test_get_coins_exchange_rate(self): - if DEBUG: - print(loop.run_until_complete(client.get_coins_exchange_rate())) - else: - (loop.run_until_complete(client.get_coins_exchange_rate())) - - def test_get_coin_shop_products(self): - if DEBUG: - print(loop.run_until_complete(client.get_coinshop_products())) - else: - (loop.run_until_complete(client.get_coinshop_products())) - - def test_get_site_stats(self): - if DEBUG: - print(loop.run_until_complete(client.get_site_stats())) - else: - (loop.run_until_complete(client.get_site_stats())) - - def test_get_problem_by_id_array(self): - for problem_id_array in problem_id_array_list: - if DEBUG: - print(loop.run_until_complete(client.get_problem_by_id_array(problem_id_array))) - else: - (loop.run_until_complete(client.get_problem_by_id_array(problem_id_array))) - - def test_get_problem_by_id(self): - for problem_id in problem_id_list: - if DEBUG: - print(loop.run_until_complete(client.get_problem_by_id(problem_id))) - else: - (loop.run_until_complete(client.get_problem_by_id(problem_id))) - - def test_get_problem_level(self): - if DEBUG: - print(loop.run_until_complete(client.get_problem_level())) - else: - (loop.run_until_complete(client.get_problem_level())) - - def test_search_problem(self): - for query, direction, page, sort in search_query_list: - if DEBUG: - print(loop.run_until_complete(client.search_problem(query, direction, page, sort))) - else: - print(loop.run_until_complete(client.search_problem(query, direction, page, sort))) - - def test_get_search_auto_completion(self): - for query in query_list: - if DEBUG: - print(loop.run_until_complete(client.get_search_auto_completion(query))) - else: - (loop.run_until_complete(client.get_search_auto_completion(query))) - - -if __name__ == "__main__": - unittest.main() From 39147b3fd685ef8b68a361787a0e34bb9808faf9 Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:36:23 +0900 Subject: [PATCH 2/7] Add `solvedac_token` parameter to `AbstractHTTPClient` constructor --- solvedac_community/HTTPClients/abstract_http_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solvedac_community/HTTPClients/abstract_http_client.py b/solvedac_community/HTTPClients/abstract_http_client.py index c7e74f0..ea8a728 100644 --- a/solvedac_community/HTTPClients/abstract_http_client.py +++ b/solvedac_community/HTTPClients/abstract_http_client.py @@ -23,7 +23,7 @@ class AbstractHTTPClient(metaclass=ABCMeta): @abstractmethod - def __init__(self): + def __init__(self, solvedac_token: Optional[str] = None): pass @abstractmethod From 3a0e156772cfbccfae2a7af2135bf1aeb0496b8c Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:37:05 +0900 Subject: [PATCH 3/7] Delete `loop` parameter in `HttpxHTTPClient` constructor --- solvedac_community/HTTPClients/httpx_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solvedac_community/HTTPClients/httpx_client.py b/solvedac_community/HTTPClients/httpx_client.py index 22d3dba..c8eaf1b 100644 --- a/solvedac_community/HTTPClients/httpx_client.py +++ b/solvedac_community/HTTPClients/httpx_client.py @@ -27,8 +27,8 @@ class HttpxHTTPClient(AbstractHTTPClient): USER_AGENT: ClassVar[str] = "Mozilla/5.0" - def __init__(self, loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None) -> None: - self.loop: asyncio.AbstractEventLoop = loop + def __init__(self, solvedac_token: Optional[str] = None) -> None: + self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() self.lock: asyncio.Lock = asyncio.Lock() self.solvedac_token: Union[str, None] = solvedac_token From 67ea06b55c478de101e5025b4ba87613286a9126 Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:37:16 +0900 Subject: [PATCH 4/7] Delete `loop` parameter in `AiohttpHTTPClient` constructor --- solvedac_community/HTTPClients/aiohttp_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solvedac_community/HTTPClients/aiohttp_client.py b/solvedac_community/HTTPClients/aiohttp_client.py index 577ae6e..cdef7c5 100644 --- a/solvedac_community/HTTPClients/aiohttp_client.py +++ b/solvedac_community/HTTPClients/aiohttp_client.py @@ -22,18 +22,18 @@ from solvedac_community.HTTPClients.abstract_http_client import AbstractHTTPClient from solvedac_community.HTTPClients.httpclient import MISSING, ResponseData, Route - __all__ = ["AiohttpHTTPClient"] class AiohttpHTTPClient(AbstractHTTPClient): USER_AGENT: ClassVar[str] = "Mozilla/5.0" - def __init__(self, loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None) -> None: - self.loop: asyncio.AbstractEventLoop = loop + def __init__(self, solvedac_token: Optional[str] = None) -> None: + self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() self.session: aiohttp.ClientSession = MISSING self.lock: asyncio.Lock = asyncio.Lock() self.solvedac_token: Union[str, None] = solvedac_token + atexit.register(self.close) async def __create_session(self): From 1328357d8b2df16214ca5ca78335d69b9cc8f160 Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:38:04 +0900 Subject: [PATCH 5/7] Improve `get_http_client` method, Remove `HTTPClientLibrary` enum --- solvedac_community/HTTPClients/httpclient.py | 47 ++++++-------------- 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/solvedac_community/HTTPClients/httpclient.py b/solvedac_community/HTTPClients/httpclient.py index cabcc0b..eafce5c 100644 --- a/solvedac_community/HTTPClients/httpclient.py +++ b/solvedac_community/HTTPClients/httpclient.py @@ -13,11 +13,10 @@ OTHER DEALINGS IN THE SOFTWARE. """ -import asyncio -from enum import Enum, auto +from enum import Enum from typing import ClassVar, Optional, Dict, Any -__all__ = ["HTTPClientLibrary", "ResponseData", "RequestMethod", "Route", "get_http_client"] +__all__ = ["ResponseData", "RequestMethod", "Route", "get_http_client"] class Missing: @@ -27,42 +26,22 @@ class Missing: MISSING: Any = Missing() -class HTTPClientLibrary(Enum): - AIOHTTP = auto() - HTTPX = auto() - - -def get_http_client( - loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None, lib: Optional[HTTPClientLibrary] = None -): - if lib is None: - try: - import aiohttp - from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient - - return AiohttpHTTPClient(loop, solvedac_token) - except ImportError: - pass - - try: - import httpx - from solvedac_community.HTTPClients.httpx_client import HttpxHTTPClient - - return HttpxHTTPClient(loop, solvedac_token) - except ImportError: - pass +def get_http_client(solvedac_token: Optional[str] = None): + try: + from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient - raise ImportError("At least one of aiohttp or httpx libraries is required") + return AiohttpHTTPClient(solvedac_token) + except ImportError: + pass - if lib == HTTPClientLibrary.HTTPX: + try: from solvedac_community.HTTPClients.httpx_client import HttpxHTTPClient - return HttpxHTTPClient(loop, solvedac_token) - - elif lib == HTTPClientLibrary.AIOHTTP: - from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient + return HttpxHTTPClient(solvedac_token) + except ImportError: + pass - return AiohttpHTTPClient(loop, solvedac_token) + raise ImportError("At least one of aiohttp or httpx libraries is required.") class RequestMethod(Enum): From 4af6c1eaf35239622fd7432c1caeb0b260dfd6ff Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:38:17 +0900 Subject: [PATCH 6/7] Remove `HTTPClientLibrary` from __all__ --- solvedac_community/HTTPClients/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solvedac_community/HTTPClients/__init__.py b/solvedac_community/HTTPClients/__init__.py index 48e1bfe..cd59f9c 100644 --- a/solvedac_community/HTTPClients/__init__.py +++ b/solvedac_community/HTTPClients/__init__.py @@ -14,10 +14,9 @@ """ from .abstract_http_client import AbstractHTTPClient -from .httpclient import HTTPClientLibrary from .httpclient import RequestMethod from .httpclient import ResponseData from .httpclient import Route from .httpclient import get_http_client -__all__ = ["AbstractHTTPClient", "HTTPClientLibrary", "RequestMethod", "ResponseData", "Route", "get_http_client"] +__all__ = ["AbstractHTTPClient", "RequestMethod", "ResponseData", "Route", "get_http_client"] From 23b8089d8d97aa5b615426d847242f69c91d6bdf Mon Sep 17 00:00:00 2001 From: Eunho Lee Date: Thu, 11 Apr 2024 20:38:40 +0900 Subject: [PATCH 7/7] Change `http_library` parameter to `http_client` --- solvedac_community/client.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/solvedac_community/client.py b/solvedac_community/client.py index 1e0972f..2794254 100644 --- a/solvedac_community/client.py +++ b/solvedac_community/client.py @@ -25,17 +25,19 @@ class Client: - __loop: asyncio.AbstractEventLoop __http_client: AbstractHTTPClient __has_token: bool - def __init__(self, solvedac_token: Optional[str] = None, http_library: HTTPClientLibrary = None) -> None: + def __init__(self, solvedac_token: Optional[str] = None, http_client: AbstractHTTPClient = None) -> None: try: asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) except AttributeError: pass - self.__loop = asyncio.get_event_loop() - self.__http_client = get_http_client(self.__loop, solvedac_token=solvedac_token, lib=http_library) + + if http_client is None: + self.__http_client = get_http_client(solvedac_token=solvedac_token) + else: + self.__http_client = http_client self.__has_token = bool(solvedac_token) async def get_user(self, handle: str) -> Models.User: @@ -272,7 +274,9 @@ async def verify_account_credentials(self) -> Models.AccountInfo: :return: :class:`Models.AccountInfo` """ - response: ResponseData = await self.__http_client.request(Route(RequestMethod.GET, "/account/verify_credentials")) + response: ResponseData = await self.__http_client.request( + Route(RequestMethod.GET, "/account/verify_credentials") + ) check_stats_code(response.status)