Skip to content

Commit

Permalink
Merge pull request #92 from azriel1rf/remove_default_empty_list
Browse files Browse the repository at this point in the history
Remove default empty list
  • Loading branch information
MtkN1 committed Oct 30, 2021
2 parents 253bba4 + 0c4b9ce commit 195d39f
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 177 deletions.
28 changes: 17 additions & 11 deletions pybotters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import asyncio
from typing import Any, Dict, List, Mapping, Optional, Tuple, Union
from typing import Any, Mapping, Optional, Tuple, Union

import aiohttp
from aiohttp import hdrs
Expand Down Expand Up @@ -54,9 +56,11 @@ async def _request(
*,
params: Optional[Mapping[str, str]] = None,
data: Any = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
if apis is None:
apis = {}
async with Client(apis=apis, response_class=SyncClientResponse) as client:
async with client.request(
method, url, params=params, data=data, **kwargs
Expand All @@ -71,7 +75,7 @@ def request(
*,
params: Optional[Mapping[str, str]] = None,
data: Any = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
loop = asyncio.get_event_loop()
Expand All @@ -84,7 +88,7 @@ def get(
url: str,
*,
params: Optional[Mapping[str, str]] = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
loop = asyncio.get_event_loop()
Expand All @@ -97,7 +101,7 @@ def post(
url: str,
*,
data: Any = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
loop = asyncio.get_event_loop()
Expand All @@ -110,7 +114,7 @@ def put(
url: str,
*,
data: Any = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
loop = asyncio.get_event_loop()
Expand All @@ -123,7 +127,7 @@ def delete(
url: str,
*,
data: Any = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> SyncClientResponse:
loop = asyncio.get_event_loop()
Expand All @@ -135,13 +139,15 @@ def delete(
async def _ws_connect(
url: str,
*,
send_str: Optional[Union[str, List[str]]] = None,
send_str: Optional[Union[str, list[str]]] = None,
send_json: Any = None,
hdlr_str: Optional[WsStrHandler] = None,
hdlr_json: Optional[WsJsonHandler] = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> None:
if apis is None:
apis = {}
async with Client(apis=apis) as client:
wstask = await client.ws_connect(
url,
Expand All @@ -157,11 +163,11 @@ async def _ws_connect(
def ws_connect(
url: str,
*,
send_str: Optional[Union[str, List[str]]] = None,
send_str: Optional[Union[str, list[str]]] = None,
send_json: Any = None,
hdlr_str: Optional[WsStrHandler] = None,
hdlr_json: Optional[WsJsonHandler] = None,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
**kwargs: Any,
) -> None:
loop = asyncio.get_event_loop()
Expand Down
44 changes: 23 additions & 21 deletions pybotters/auth.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import base64
import hashlib
import hmac
import json
import time
from dataclasses import dataclass
from typing import Any, Dict, Tuple
from typing import Any

import aiohttp
from aiohttp.formdata import FormData
Expand All @@ -16,10 +18,10 @@

class Auth:
@staticmethod
def bybit(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def bybit(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}

session: aiohttp.ClientSession = kwargs['session']
key: str = session.__dict__['_apis'][Hosts.items[url.host].name][0]
Expand Down Expand Up @@ -58,10 +60,10 @@ def bybit(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def binance(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def binance(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand Down Expand Up @@ -95,10 +97,10 @@ def binance(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def bitflyer(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def bitflyer(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -118,10 +120,10 @@ def bitflyer(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def gmocoin(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def gmocoin(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -145,9 +147,9 @@ def gmocoin(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def liquid(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def liquid(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand Down Expand Up @@ -181,10 +183,10 @@ def liquid(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def bitbank(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def bitbank(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -207,10 +209,10 @@ def bitbank(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def ftx(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def ftx(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -228,10 +230,10 @@ def ftx(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def bitmex(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def bitmex(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
method: str = args[0]
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -251,9 +253,9 @@ def bitmex(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def phemex(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def phemex(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand All @@ -278,9 +280,9 @@ def phemex(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
return args

@staticmethod
def coincheck(args: Tuple[str, URL], kwargs: Dict[str, Any]) -> Tuple[str, URL]:
def coincheck(args: tuple[str, URL], kwargs: dict[str, Any]) -> tuple[str, URL]:
url: URL = args[1]
data: Dict[str, Any] = kwargs['data'] or {}
data: dict[str, Any] = kwargs['data'] or {}
headers: CIMultiDict = kwargs['headers']

session: aiohttp.ClientSession = kwargs['session']
Expand Down
24 changes: 17 additions & 7 deletions pybotters/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import asyncio
import json
import logging
import os
from typing import Any, Dict, List, Mapping, Optional, Tuple, Union
from typing import Any, Mapping, Optional, Union

import aiohttp
from aiohttp import hdrs
Expand All @@ -22,7 +24,7 @@ class Client:

def __init__(
self,
apis: Union[Dict[str, List[str]], str] = {},
apis: Optional[Union[dict[str, list[str]], str]] = None,
base_url: str = '',
**kwargs: Any,
) -> None:
Expand All @@ -49,8 +51,8 @@ def _request(
method: str,
url: str,
*,
params: Optional[Dict[str, Any]] = None,
data: Optional[Dict[str, Any]] = None,
params: Optional[Mapping[str, Any]] = None,
data: Optional[dict[str, Any]] = None,
auth: Optional[Auth] = Auth,
**kwargs: Any,
) -> _RequestContextManager:
Expand Down Expand Up @@ -118,7 +120,7 @@ async def ws_connect(
self,
url: str,
*,
send_str: Optional[Union[str, List[str]]] = None,
send_str: Optional[Union[str, list[str]]] = None,
send_json: Any = None,
hdlr_str: Optional[WsStrHandler] = None,
hdlr_json: Optional[WsJsonHandler] = None,
Expand All @@ -141,7 +143,11 @@ async def ws_connect(
return task

@staticmethod
def _load_apis(apis: Union[Dict[str, List[str]], str]) -> Dict[str, List[str]]:
def _load_apis(
apis: Optional[Union[dict[str, list[str]], str]]
) -> dict[str, list[str]]:
if apis is None:
apis = {}
if isinstance(apis, dict):
if apis:
return apis
Expand All @@ -165,7 +171,11 @@ def _load_apis(apis: Union[Dict[str, List[str]], str]) -> Dict[str, List[str]]:
return {}

@staticmethod
def _encode_apis(apis: Dict[str, List[str]]) -> Dict[str, Tuple[str, bytes]]:
def _encode_apis(
apis: Optional[dict[str, list[str]]]
) -> dict[str, tuple[str, bytes]]:
if apis is None:
apis = {}
encoded = {}
for name in apis:
if len(apis[name]) == 2:
Expand Down
18 changes: 11 additions & 7 deletions pybotters/models/binance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import asyncio
from collections import deque
from typing import Any, Awaitable, Dict, List, Optional, Union
from typing import Any, Awaitable, Optional, Union

import aiohttp

Expand Down Expand Up @@ -132,7 +134,7 @@ def _onmessage(self, item: Item) -> None:
class MarkPrice(DataStore):
_KEYS = ['s']

def _onmessage(self, data: Union[Item, List[Item]]) -> None:
def _onmessage(self, data: Union[Item, list[Item]]) -> None:
if isinstance(data, list):
self._update(data)
else:
Expand All @@ -156,7 +158,7 @@ def _onmessage(self, item: Item) -> None:
class Ticker(DataStore):
_KEYS = ['s']

def _onmessage(self, data: Union[Item, List[Item]]) -> None:
def _onmessage(self, data: Union[Item, list[Item]]) -> None:
if isinstance(data, list):
self._update(data)
else:
Expand All @@ -183,7 +185,9 @@ def _init(self) -> None:
self.initialized = False
self._buff = deque(maxlen=200)

def sorted(self, query: Item = {}) -> Dict[str, List[float]]:
def sorted(self, query: Optional[Item] = None) -> dict[str, list[float]]:
if query is None:
query = {}
result = {self._MAPSIDE[k]: [] for k in self._MAPSIDE}
for item in self:
if all(k in item and query[k] == item[k] for k in query):
Expand Down Expand Up @@ -220,7 +224,7 @@ class Balance(DataStore):
def _onmessage(self, item: Item) -> None:
self._update(item['a']['B'])

def _onresponse(self, data: List[Item]) -> None:
def _onresponse(self, data: list[Item]) -> None:
for item in data:
self._update(
[
Expand All @@ -239,7 +243,7 @@ class Position(DataStore):
def _onmessage(self, item: Item) -> None:
self._update(item['a']['P'])

def _onresponse(self, data: List[Item]) -> None:
def _onresponse(self, data: list[Item]) -> None:
for item in data:
self._update(
[
Expand All @@ -264,7 +268,7 @@ def _onmessage(self, item: Item) -> None:
else:
self._delete([item['o']])

def _onresponse(self, symbol: Optional[str], data: List[Item]) -> None:
def _onresponse(self, symbol: Optional[str], data: list[Item]) -> None:
if symbol is not None:
self._delete(self.find({'symbol': symbol}))
else:
Expand Down

0 comments on commit 195d39f

Please sign in to comment.