Skip to content

Commit

Permalink
Merge pull request #5 from sndmndss/dev
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
sndmndss authored Apr 30, 2024
2 parents 5489201 + 4af2adb commit 82f5e34
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 79 deletions.
43 changes: 23 additions & 20 deletions bpx/__async/account.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
from bpx.base.base_account import BaseAccount
from bpx.http_client.async_http_client import AsyncHttpClient

default_http_client = AsyncHttpClient()

class Account(BaseAccount, AsyncHttpClient):

class Account(BaseAccount):
def __init__(self,
public_key: str,
secret_key: str,
window: int = 5000,
proxy: str = None,
debug: bool = False
debug: bool = False,
http_client: AsyncHttpClient = default_http_client
):

BaseAccount.__init__(self,
public_key,
secret_key,
window,
debug)
AsyncHttpClient.__init__(self, proxy=proxy)
super().__init__(public_key,
secret_key,
window,
debug)
self.http_client = http_client
self.http_client.proxy = proxy

async def get_balances(self, window: int = None):
url, headers = super().get_balances(window)
return await self._get(url, headers=headers)
return await self.http_client.get(url, headers=headers)

async def deposits(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().deposits(limit, offset, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def get_deposit_address(self, blockchain: str, window: int = None):
url, headers, params = super().get_deposit_address(blockchain, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def get_withdrawals(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().get_withdrawals(limit, offset, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def withdrawal(self, address: str,
symbol: str,
blockchain: str,
quantity: str,
window: int = None):
url, headers, params = super().withdrawal(address, symbol, blockchain, quantity, window)
return await self._post(url, headers=headers, data=params)
return await self.http_client.post(url, headers=headers, data=params)

async def get_order_history_query(self, symbol: str, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().get_order_history_query(symbol, limit, offset, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def get_fill_history_query(self, symbol: str,
limit: int = 100,
Expand All @@ -58,14 +61,14 @@ async def get_fill_history_query(self, symbol: str,
__from,
to,
window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def get_open_order(self, symbol: str,
order_id: str = None,
client_id: int = None,
window: int = None):
url, headers, params = super().get_open_order(symbol, order_id, client_id, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def execute_order(self, symbol: str,
side: str,
Expand All @@ -85,19 +88,19 @@ async def execute_order(self, symbol: str,
price, trigger_price,
self_trade_prevention, quote_quantity,
client_id, post_only, window)
return await self._post(url, headers=headers, data=params)
return await self.http_client.post(url, headers=headers, data=params)

async def cancel_order(self, symbol: str,
order_id: str = None,
client_id: int = None,
window: int = None):
url, headers, params = super().cancel_order(symbol, order_id, client_id, window)
return await self._delete(url, headers=headers, data=params)
return await self.http_client.delete(url, headers=headers, data=params)

async def get_open_orders(self, symbol: str = None, window: int = None):
url, headers, params = super().get_open_orders(symbol, window)
return await self._get(url, headers=headers, params=params)
return await self.http_client.get(url, headers=headers, params=params)

async def cancel_all_orders(self, symbol: str, window: int = None):
url, headers, params = super().cancel_all_orders(symbol, window)
return await self._delete(url, headers=headers, data=params)
return await self.http_client.delete(url, headers=headers, data=params)
29 changes: 16 additions & 13 deletions bpx/__async/public.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
from bpx.base.base_public import BasePublic
from bpx.http_client.async_http_client import AsyncHttpClient

default_http_client = AsyncHttpClient()

class Public(BasePublic, AsyncHttpClient):

def __init__(self, proxy: str = None):
AsyncHttpClient.__init__(self, proxy=proxy)
class Public(BasePublic):

def __init__(self, proxy: str = None, http_client: AsyncHttpClient = default_http_client):
self.http_client = http_client
self.http_client.proxy = proxy

async def get_assets(self):
return await self._get(self.get_assets_url())
return await self.http_client.get(self.get_assets_url())

async def get_markets(self):
return await self._get(self.get_markets_url())
return await self.http_client.get(self.get_markets_url())

async def get_ticker(self, symbol: str):
return await self._get(self.get_ticker_url(symbol))
return await self.http_client.get(self.get_ticker_url(symbol))

async def get_depth(self, symbol: str):
return await self._get(self.get_depth_url(symbol))
return await self.http_client.get(self.get_depth_url(symbol))

async def get_klines(self, symbol: str, interval: str, start_time=0, end_time=0):
return await self._get(self.get_klines_url(symbol, interval, start_time, end_time))
return await self.http_client.get(self.get_klines_url(symbol, interval, start_time, end_time))

async def get_status(self):
return await self._get(self.get_status_url())
return await self.http_client.get(self.get_status_url())

async def get_ping(self):
return await self._get(self.get_ping_url())
return await self.http_client.get(self.get_ping_url())

async def get_time(self):
return await self._get(self.get_time_url())
return await self.http_client.get(self.get_time_url())

async def get_recent_trades(self, symbol: str, limit=100):
return await self._get(self.get_recent_trades_url(symbol, limit))
return await self.http_client.get(self.get_recent_trades_url(symbol, limit))

async def get_history_trades(self, symbol: str, limit=100, offset=0):
return await self._get(self.get_history_trades_url(symbol, limit, offset))
return await self.http_client.get(self.get_history_trades_url(symbol, limit, offset))
43 changes: 23 additions & 20 deletions bpx/account.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
from bpx.base.base_account import BaseAccount
from bpx.http_client.sync_http_client import SyncHttpClient

http_client = SyncHttpClient()

class Account(BaseAccount, SyncHttpClient):

class Account(BaseAccount):
def __init__(self,
public_key: str,
secret_key: str,
window: int = 5000,
proxy: dict = None,
debug: bool = False
debug: bool = False,
default_http_client: SyncHttpClient = http_client
):

BaseAccount.__init__(self,
public_key,
secret_key,
window,
debug)
SyncHttpClient.__init__(self, proxies=proxy)
super().__init__(public_key,
secret_key,
window,
debug)
self.http_client = default_http_client
self.http_client.proxies = proxy

def get_balances(self, window: int = None):
url, headers = super().get_balances(window)
return self._get(url, headers=headers)
return self.http_client.get(url, headers=headers)

def deposits(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().deposits(limit, offset, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def get_deposit_address(self, blockchain: str, window: int = None):
url, headers, params = super().get_deposit_address(blockchain, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def get_withdrawals(self, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().get_withdrawals(limit, offset, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def withdrawal(self, address: str,
symbol: str,
blockchain: str,
quantity: str,
window: int = None):
url, headers, params = super().withdrawal(address, symbol, blockchain, quantity, window)
return self._post(url, headers=headers, data=params)
return self.http_client.post(url, headers=headers, data=params)

def get_order_history_query(self, symbol: str, limit: int = 100, offset: int = 0, window: int = None):
url, headers, params = super().get_order_history_query(symbol, limit, offset, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def get_fill_history_query(self, symbol: str,
limit: int = 100,
Expand All @@ -58,14 +61,14 @@ def get_fill_history_query(self, symbol: str,
__from,
to,
window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def get_open_order(self, symbol: str,
order_id: str = None,
client_id: int = None,
window: int = None):
url, headers, params = super().get_open_order(symbol, order_id, client_id, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def execute_order(self, symbol: str,
side: str,
Expand All @@ -85,19 +88,19 @@ def execute_order(self, symbol: str,
price, trigger_price,
self_trade_prevention, quote_quantity,
client_id, post_only, window)
return self._post(url, headers=headers, data=params)
return self.http_client.post(url, headers=headers, data=params)

def cancel_order(self, symbol: str,
order_id: str = None,
client_id: int = None,
window: int = None):
url, headers, params = super().cancel_order(symbol, order_id, client_id, window)
return self._delete(url, headers=headers, data=params)
return self.http_client.delete(url, headers=headers, data=params)

def get_open_orders(self, symbol: str = None, window: int = None):
url, headers, params = super().get_open_orders(symbol, window)
return self._get(url, headers=headers, params=params)
return self.http_client.get(url, headers=headers, params=params)

def cancel_all_orders(self, symbol: str, window: int = None):
url, headers, params = super().cancel_all_orders(symbol, window)
return self._delete(url, headers=headers, data=params)
return self.http_client.delete(url, headers=headers, data=params)
2 changes: 1 addition & 1 deletion bpx/base/base_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_depth_url(self, symbol):

def get_klines_url(self, symbol, interval, start_time, end_time):
if start_time < 0 or end_time < 0:
raise NegativeValueError
raise NegativeValueError("start_time and end_time")
if not TimeInterval.has_value(interval):
raise InvalidTimeIntervalError(interval)
url = f'api/v1/klines?symbol={symbol}&interval={interval}'
Expand Down
6 changes: 3 additions & 3 deletions bpx/http_client/async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AsyncHttpClient(HttpClient):
def __init__(self, proxy: str = ""):
self.proxy = proxy

async def _get(self, url, headers=None, params=None):
async def get(self, url, headers=None, params=None):
ssl_context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession() as session:
async with session.get(url, proxy=self.proxy, params=params,
Expand All @@ -23,7 +23,7 @@ async def _get(self, url, headers=None, params=None):
except aiohttp.client_exceptions.ContentTypeError:
return await response.text()

async def _post(self, url, headers=None, data=None):
async def post(self, url, headers=None, data=None):
ssl_context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession() as session:
async with session.post(url, proxy=self.proxy, headers=headers,
Expand All @@ -36,7 +36,7 @@ async def _post(self, url, headers=None, data=None):
except aiohttp.client_exceptions.ContentTypeError:
return await response.text()

async def _delete(self, url, headers=None, data=None):
async def delete(self, url, headers=None, data=None):
ssl_context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession() as session:
async with session.delete(url, proxy=self.proxy, headers=headers,
Expand Down
6 changes: 3 additions & 3 deletions bpx/http_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

class HttpClient(abc.ABC):
@abc.abstractmethod
def _get(self, url, headers=None, params=None):
def get(self, url, headers=None, params=None):
"""Perform a GET request."""
pass

@abc.abstractmethod
def _post(self, url, headers=None, data=None):
def post(self, url, headers=None, data=None):
"""Perform a POST request."""
pass

@abc.abstractmethod
def _delete(self, url, headers=None):
def delete(self, url, headers=None):
"""Perform a DELETE request"""
pass
6 changes: 3 additions & 3 deletions bpx/http_client/sync_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ class SyncHttpClient(HttpClient):
def __init__(self, proxies: dict = None):
self.proxies = proxies

def _get(self, url, headers=None, params=None):
def get(self, url, headers=None, params=None):
response = requests.get(url=url, proxies=self.proxies, headers=headers, params=params)
try:
return response.json()
except json.JSONDecodeError:
return response.text

def _post(self, url, headers=None, data=None):
def post(self, url, headers=None, data=None):
response = requests.post(url=url, proxies=self.proxies, headers=headers, data=json.dumps(data))
try:
return response.json()
except json.JSONDecodeError:
return response.text

def _delete(self, url, headers=None, data=None):
def delete(self, url, headers=None, data=None):
response = requests.delete(url, proxies=self.proxies, headers=headers, data=json.dumps(data))
try:
return response.json()
Expand Down
Loading

0 comments on commit 82f5e34

Please sign in to comment.