Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions solvedac_community/HTTPClients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion solvedac_community/HTTPClients/abstract_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class AbstractHTTPClient(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
def __init__(self, solvedac_token: Optional[str] = None):
pass

@abstractmethod
Expand Down
6 changes: 3 additions & 3 deletions solvedac_community/HTTPClients/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
47 changes: 13 additions & 34 deletions solvedac_community/HTTPClients/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions solvedac_community/HTTPClients/httpx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions solvedac_community/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
125 changes: 0 additions & 125 deletions test/unittest_github_action.py

This file was deleted.