Skip to content

Commit bc938f6

Browse files
authored
Merge pull request #33 from solvedac/dev
Improve http client, Adhere LSP
2 parents 2efce53 + 23b8089 commit bc938f6

File tree

7 files changed

+29
-172
lines changed

7 files changed

+29
-172
lines changed

solvedac_community/HTTPClients/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
"""
1515

1616
from .abstract_http_client import AbstractHTTPClient
17-
from .httpclient import HTTPClientLibrary
1817
from .httpclient import RequestMethod
1918
from .httpclient import ResponseData
2019
from .httpclient import Route
2120
from .httpclient import get_http_client
2221

23-
__all__ = ["AbstractHTTPClient", "HTTPClientLibrary", "RequestMethod", "ResponseData", "Route", "get_http_client"]
22+
__all__ = ["AbstractHTTPClient", "RequestMethod", "ResponseData", "Route", "get_http_client"]

solvedac_community/HTTPClients/abstract_http_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
class AbstractHTTPClient(metaclass=ABCMeta):
2525
@abstractmethod
26-
def __init__(self):
26+
def __init__(self, solvedac_token: Optional[str] = None):
2727
pass
2828

2929
@abstractmethod

solvedac_community/HTTPClients/aiohttp_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
from solvedac_community.HTTPClients.abstract_http_client import AbstractHTTPClient
2323
from solvedac_community.HTTPClients.httpclient import MISSING, ResponseData, Route
2424

25-
2625
__all__ = ["AiohttpHTTPClient"]
2726

2827

2928
class AiohttpHTTPClient(AbstractHTTPClient):
3029
USER_AGENT: ClassVar[str] = "Mozilla/5.0"
3130

32-
def __init__(self, loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None) -> None:
33-
self.loop: asyncio.AbstractEventLoop = loop
31+
def __init__(self, solvedac_token: Optional[str] = None) -> None:
32+
self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
3433
self.session: aiohttp.ClientSession = MISSING
3534
self.lock: asyncio.Lock = asyncio.Lock()
3635
self.solvedac_token: Union[str, None] = solvedac_token
36+
3737
atexit.register(self.close)
3838

3939
async def __create_session(self):

solvedac_community/HTTPClients/httpclient.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
OTHER DEALINGS IN THE SOFTWARE.
1414
"""
1515

16-
import asyncio
17-
from enum import Enum, auto
16+
from enum import Enum
1817
from typing import ClassVar, Optional, Dict, Any
1918

20-
__all__ = ["HTTPClientLibrary", "ResponseData", "RequestMethod", "Route", "get_http_client"]
19+
__all__ = ["ResponseData", "RequestMethod", "Route", "get_http_client"]
2120

2221

2322
class Missing:
@@ -27,42 +26,22 @@ class Missing:
2726
MISSING: Any = Missing()
2827

2928

30-
class HTTPClientLibrary(Enum):
31-
AIOHTTP = auto()
32-
HTTPX = auto()
33-
34-
35-
def get_http_client(
36-
loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None, lib: Optional[HTTPClientLibrary] = None
37-
):
38-
if lib is None:
39-
try:
40-
import aiohttp
41-
from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient
42-
43-
return AiohttpHTTPClient(loop, solvedac_token)
44-
except ImportError:
45-
pass
46-
47-
try:
48-
import httpx
49-
from solvedac_community.HTTPClients.httpx_client import HttpxHTTPClient
50-
51-
return HttpxHTTPClient(loop, solvedac_token)
52-
except ImportError:
53-
pass
29+
def get_http_client(solvedac_token: Optional[str] = None):
30+
try:
31+
from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient
5432

55-
raise ImportError("At least one of aiohttp or httpx libraries is required")
33+
return AiohttpHTTPClient(solvedac_token)
34+
except ImportError:
35+
pass
5636

57-
if lib == HTTPClientLibrary.HTTPX:
37+
try:
5838
from solvedac_community.HTTPClients.httpx_client import HttpxHTTPClient
5939

60-
return HttpxHTTPClient(loop, solvedac_token)
61-
62-
elif lib == HTTPClientLibrary.AIOHTTP:
63-
from solvedac_community.HTTPClients.aiohttp_client import AiohttpHTTPClient
40+
return HttpxHTTPClient(solvedac_token)
41+
except ImportError:
42+
pass
6443

65-
return AiohttpHTTPClient(loop, solvedac_token)
44+
raise ImportError("At least one of aiohttp or httpx libraries is required.")
6645

6746

6847
class RequestMethod(Enum):

solvedac_community/HTTPClients/httpx_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
class HttpxHTTPClient(AbstractHTTPClient):
2828
USER_AGENT: ClassVar[str] = "Mozilla/5.0"
2929

30-
def __init__(self, loop: asyncio.AbstractEventLoop, solvedac_token: Optional[str] = None) -> None:
31-
self.loop: asyncio.AbstractEventLoop = loop
30+
def __init__(self, solvedac_token: Optional[str] = None) -> None:
31+
self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
3232
self.lock: asyncio.Lock = asyncio.Lock()
3333
self.solvedac_token: Union[str, None] = solvedac_token
3434

solvedac_community/client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@
2525

2626

2727
class Client:
28-
__loop: asyncio.AbstractEventLoop
2928
__http_client: AbstractHTTPClient
3029
__has_token: bool
3130

32-
def __init__(self, solvedac_token: Optional[str] = None, http_library: HTTPClientLibrary = None) -> None:
31+
def __init__(self, solvedac_token: Optional[str] = None, http_client: AbstractHTTPClient = None) -> None:
3332
try:
3433
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
3534
except AttributeError:
3635
pass
37-
self.__loop = asyncio.get_event_loop()
38-
self.__http_client = get_http_client(self.__loop, solvedac_token=solvedac_token, lib=http_library)
36+
37+
if http_client is None:
38+
self.__http_client = get_http_client(solvedac_token=solvedac_token)
39+
else:
40+
self.__http_client = http_client
3941
self.__has_token = bool(solvedac_token)
4042

4143
async def get_user(self, handle: str) -> Models.User:
@@ -272,7 +274,9 @@ async def verify_account_credentials(self) -> Models.AccountInfo:
272274
:return: :class:`Models.AccountInfo`
273275
"""
274276

275-
response: ResponseData = await self.__http_client.request(Route(RequestMethod.GET, "/account/verify_credentials"))
277+
response: ResponseData = await self.__http_client.request(
278+
Route(RequestMethod.GET, "/account/verify_credentials")
279+
)
276280

277281
check_stats_code(response.status)
278282

test/unittest_github_action.py

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)