-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from Davi0kProgramsThings/v3.0.0b3
Merge branch `Davi0kProgramsThings:v3.0.0b3` into branch `bitfinexcom:master`.
- Loading branch information
Showing
83 changed files
with
1,766 additions
and
1,697 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .client import Client | ||
|
||
from .urls import REST_HOST, PUB_REST_HOST, \ | ||
WSS_HOST, PUB_WSS_HOST | ||
|
||
from .version import __version__ | ||
from ._client import \ | ||
Client, \ | ||
REST_HOST, \ | ||
WSS_HOST, \ | ||
PUB_REST_HOST, \ | ||
PUB_WSS_HOST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import \ | ||
TYPE_CHECKING, List, Optional | ||
|
||
from bfxapi._utils.logging import ColorLogger | ||
|
||
from bfxapi.rest import BfxRestInterface | ||
from bfxapi.websocket import BfxWebSocketClient | ||
from bfxapi.exceptions import IncompleteCredentialError | ||
|
||
if TYPE_CHECKING: | ||
from bfxapi.websocket._client.bfx_websocket_client import \ | ||
_Credentials | ||
|
||
REST_HOST = "https://api.bitfinex.com/v2" | ||
WSS_HOST = "wss://api.bitfinex.com/ws/2" | ||
|
||
PUB_REST_HOST = "https://api-pub.bitfinex.com/v2" | ||
PUB_WSS_HOST = "wss://api-pub.bitfinex.com/ws/2" | ||
|
||
class Client: | ||
def __init__( | ||
self, | ||
api_key: Optional[str] = None, | ||
api_secret: Optional[str] = None, | ||
*, | ||
rest_host: str = REST_HOST, | ||
wss_host: str = WSS_HOST, | ||
filters: Optional[List[str]] = None, | ||
timeout: Optional[int] = 60 * 15, | ||
log_filename: Optional[str] = None | ||
) -> 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="INFO") | ||
|
||
if log_filename: | ||
logger.register(filename=log_filename) | ||
|
||
self.wss = BfxWebSocketClient(wss_host, \ | ||
credentials=credentials, timeout=timeout, logger=logger) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import Dict, Any | ||
|
||
import re, json | ||
|
||
def _to_snake_case(string: str) -> str: | ||
return re.sub(r"(?<!^)(?=[A-Z])", "_", string).lower() | ||
|
||
def _object_hook(data: Dict[str, Any]) -> Any: | ||
return { _to_snake_case(key): value for key, value in data.items() } | ||
|
||
class JSONDecoder(json.JSONDecoder): | ||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
super().__init__(object_hook=_object_hook, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import \ | ||
Union, List, Dict, \ | ||
Any | ||
|
||
import json | ||
|
||
from decimal import Decimal | ||
|
||
_ExtJSON = Union[Dict[str, "_ExtJSON"], List["_ExtJSON"], \ | ||
bool, int, float, str, Decimal, None] | ||
|
||
_StrictJSON = Union[Dict[str, "_StrictJSON"], List["_StrictJSON"], \ | ||
int, str, None] | ||
|
||
def _clear(dictionary: Dict[str, Any]) -> Dict[str, Any]: | ||
return { key: value for key, value in dictionary.items() \ | ||
if value is not None } | ||
|
||
def _adapter(data: _ExtJSON) -> _StrictJSON: | ||
if isinstance(data, bool): | ||
return int(data) | ||
if isinstance(data, float): | ||
return format(Decimal(repr(data)), "f") | ||
if isinstance(data, Decimal): | ||
return format(data, "f") | ||
|
||
if isinstance(data, list): | ||
return [ _adapter(sub_data) for sub_data in data ] | ||
if isinstance(data, dict): | ||
return _clear({ key: _adapter(value) for key, value in data.items() }) | ||
|
||
return data | ||
|
||
class JSONEncoder(json.JSONEncoder): | ||
def encode(self, o: _ExtJSON) -> str: | ||
return super().encode(_adapter(o)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import \ | ||
TYPE_CHECKING, Literal, Optional | ||
|
||
#pylint: disable-next=wildcard-import,unused-wildcard-import | ||
from logging import * | ||
|
||
from copy import copy | ||
|
||
import sys | ||
|
||
if TYPE_CHECKING: | ||
_Level = Literal["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] | ||
|
||
_BLACK, _RED, _GREEN, _YELLOW, \ | ||
_BLUE, _MAGENTA, _CYAN, _WHITE = \ | ||
[ f"\033[0;{90 + i}m" for i in range(8) ] | ||
|
||
_BOLD_BLACK, _BOLD_RED, _BOLD_GREEN, _BOLD_YELLOW, \ | ||
_BOLD_BLUE, _BOLD_MAGENTA, _BOLD_CYAN, _BOLD_WHITE = \ | ||
[ f"\033[1;{90 + i}m" for i in range(8) ] | ||
|
||
_NC = "\033[0m" | ||
|
||
class _ColorFormatter(Formatter): | ||
__LEVELS = { | ||
"INFO": _BLUE, | ||
"WARNING": _YELLOW, | ||
"ERROR": _RED, | ||
"CRITICAL": _BOLD_RED, | ||
"DEBUG": _BOLD_WHITE | ||
} | ||
|
||
def format(self, record: LogRecord) -> str: | ||
_record = copy(record) | ||
_record.name = _MAGENTA + record.name + _NC | ||
_record.levelname = _ColorFormatter.__format_level(record.levelname) | ||
|
||
return super().format(_record) | ||
|
||
#pylint: disable-next=invalid-name | ||
def formatTime(self, record: LogRecord, datefmt: Optional[str] = None) -> str: | ||
return _GREEN + super().formatTime(record, datefmt) + _NC | ||
|
||
@staticmethod | ||
def __format_level(level: str) -> str: | ||
return _ColorFormatter.__LEVELS[level] + level + _NC | ||
|
||
_FORMAT = "%(asctime)s %(name)s %(levelname)s %(message)s" | ||
|
||
_DATE_FORMAT = "%d-%m-%Y %H:%M:%S" | ||
|
||
class ColorLogger(Logger): | ||
__FORMATTER = Formatter(_FORMAT,_DATE_FORMAT) | ||
|
||
def __init__(self, name: str, level: "_Level" = "NOTSET") -> None: | ||
super().__init__(name, level) | ||
|
||
formatter = _ColorFormatter(_FORMAT, _DATE_FORMAT) | ||
|
||
handler = StreamHandler(stream=sys.stderr) | ||
handler.setFormatter(fmt=formatter) | ||
self.addHandler(hdlr=handler) | ||
|
||
def register(self, filename: str) -> None: | ||
handler = FileHandler(filename=filename) | ||
handler.setFormatter(fmt=ColorLogger.__FORMATTER) | ||
self.addHandler(hdlr=handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "3.0.0b3" |
Oops, something went wrong.