diff --git a/bfxapi/client.py b/bfxapi/client.py index 4a7bbdf..fe86615 100644 --- a/bfxapi/client.py +++ b/bfxapi/client.py @@ -1,11 +1,18 @@ -from typing import List, Literal, Optional +from typing import \ + TYPE_CHECKING, TypedDict, List, Literal, Optional from bfxapi._utils.logger import ColorLogger +from bfxapi.exceptions import IncompleteCredentialError + from bfxapi.rest import BfxRestInterface from bfxapi.websocket import BfxWebSocketClient from bfxapi.urls import REST_HOST, WSS_HOST +if TYPE_CHECKING: + _Credentials = TypedDict("_Credentials", \ + { "api_key": str, "api_secret": str, "filters": Optional[List[str]] }) + class Client: def __init__( self, @@ -15,16 +22,28 @@ class Client: rest_host: str = REST_HOST, wss_host: str = WSS_HOST, filters: Optional[List[str]] = None, - wss_timeout: Optional[float] = 60 * 15, + timeout: Optional[float] = 60 * 15, log_filename: Optional[str] = None, log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "INFO" ) -> None: + credentials: Optional["_Credentials"] = None + + if api_key and api_secret: + credentials = \ + { "api_key": api_key, "api_secret": api_secret, "filters": filters } + elif api_key: + raise IncompleteCredentialError( \ + "You must provide both an API-KEY and an API-SECRET (missing API-KEY).") + elif api_secret: + raise IncompleteCredentialError( \ + "You must provide both an API-KEY and an API-SECRET (missing API-SECRET).") + + self.rest = BfxRestInterface(rest_host, api_key, api_secret) + logger = ColorLogger("bfxapi", level=log_level) if log_filename: logger.register(filename=log_filename) - self.rest = BfxRestInterface(rest_host, api_key, api_secret) - - self.wss = BfxWebSocketClient(wss_host, api_key, api_secret, - filters=filters, wss_timeout=wss_timeout, logger=logger) + self.wss = BfxWebSocketClient(wss_host, \ + credentials=credentials, timeout=timeout, logger=logger) diff --git a/bfxapi/exceptions.py b/bfxapi/exceptions.py index 136f5f1..b636119 100644 --- a/bfxapi/exceptions.py +++ b/bfxapi/exceptions.py @@ -6,3 +6,8 @@ class BfxBaseException(Exception): """ Base class for every custom exception in bfxapi/rest/exceptions.py and bfxapi/websocket/exceptions.py. """ + +class IncompleteCredentialError(BfxBaseException): + """ + This error indicates an incomplete credential object (missing api-key or api-secret). + """ diff --git a/bfxapi/websocket/_client/bfx_websocket_client.py b/bfxapi/websocket/_client/bfx_websocket_client.py index 8ff481f..dd02469 100644 --- a/bfxapi/websocket/_client/bfx_websocket_client.py +++ b/bfxapi/websocket/_client/bfx_websocket_client.py @@ -36,13 +36,12 @@ from .bfx_websocket_bucket import BfxWebSocketBucket from .bfx_websocket_inputs import BfxWebSocketInputs if TYPE_CHECKING: + from bfxapi.client import _Credentials + from asyncio import Task _T = TypeVar("_T", bound=Callable[..., None]) - _Credentials = TypedDict("_Credentials", \ - { "api_key": str, "api_secret": str, "filters": Optional[List[str]] }) - _Reconnection = TypedDict("_Reconnection", { "attempts": int, "reason": str, "timestamp": datetime })