Skip to content

Commit

Permalink
added some more docstrings and small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
haxdds committed Jun 25, 2022
1 parent 067d43f commit 5e92430
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dojima/brokers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from ledgerx import LedgerX
from .ledgerx import LedgerX
81 changes: 45 additions & 36 deletions dojima/brokers/ledgerx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@ def __init__(self, jwt_token: str) -> None:
self._jwt_token = jwt_token
self._session = Session()

def _request(self, method: str, url: str, data: Optional[dict] = None) -> APIData:

headers = {"Authorization": "JWT " + self._jwt_token}

opts = {
"headers": headers,
"allow_redirects": False,
}

if method.upper() in ["GET", "DELETE"]:
opts["params"] = data
else:
opts["json"] = data

response = self._session.request(method, url, **opts)

try:
response.raise_for_status()
except HTTPError as error:
print("error: ", error)
return {}

if response.text != "":
return response.json()
else:
# return status code for empty response
return {"data": {"status": response.status_code}}

def get_contracts(
self,
active: Optional[bool] = None,
Expand Down Expand Up @@ -124,7 +96,7 @@ def get_traded_contracts(

return response.get("data")

def get_contract_details(self, contract_id: str) -> APIData:
def get_contract_details(self, contract_id: Union[str, int]) -> APIData:
"""
Returns contract details for a single contract ID.
https://docs.ledgerx.com/reference/retrievecontract
Expand All @@ -141,7 +113,7 @@ def get_contract_details(self, contract_id: str) -> APIData:

return response.get("data")

def get_contract_position(self, contract_id: str) -> APIData:
def get_contract_position(self, contract_id: Union[str, int]) -> APIData:
"""
Returns your position for a given contract.
https://api.ledgerx.com/trading/contracts/{id}/position
Expand All @@ -160,7 +132,7 @@ def get_contract_position(self, contract_id: str) -> APIData:

def get_contract_ticker(
self,
contract_id: str,
contract_id: Union[str, int],
time: Optional[datetime] = None,
asset: Optional[str] = None,
) -> APIData:
Expand Down Expand Up @@ -220,7 +192,7 @@ def get_positions(

return response.get("data")

def get_trades_for_position(self, contract_id: str) -> APIData:
def get_trades_for_position(self, contract_id: Union[str, int]) -> APIData:
"""
Returns a list of your trades for a given position.
https://docs.ledgerx.com/reference/tradesposition
Expand Down Expand Up @@ -253,7 +225,7 @@ def get_open_orders(self) -> APIData:

def create_order(
self,
contract_id: str,
contract_id: Union[str, int],
order_type: str,
is_ask: bool,
size: int,
Expand Down Expand Up @@ -311,7 +283,7 @@ def delete_all_orders(self) -> APIData:

return response.get("data")

def delete_single_order(self, mid: str, contract_id: str) -> APIData:
def delete_single_order(self, mid: str, contract_id: Union[str, int]) -> APIData:
"""
Cancel a single resting limit order
Expand All @@ -334,7 +306,7 @@ def delete_single_order(self, mid: str, contract_id: str) -> APIData:
return response.get("data")

def patch_order(
self, mid: str, contract_id: str, price: int, size: int
self, mid: str, contract_id: Union[str, int], price: int, size: int
) -> APIData:
"""
Cancel and replace order.
Expand Down Expand Up @@ -362,7 +334,7 @@ def patch_order(

return response.get("data")

def get_current_book_state(self, contract_id: str) -> APIData:
def get_current_book_state(self, contract_id: Union[str, int]) -> APIData:
"""
Request the current book state for a contract
Expand All @@ -382,3 +354,40 @@ def get_current_book_state(self, contract_id: str) -> APIData:
)

return response.get("data")

def _request(self, method: str, url: str, data: Optional[dict] = None) -> APIData:
"""Utility method for submitting HTTP requests.
Args:
method: The type of HTTP request ("POST", "GET", "DELETE", etc)
url: The endpoint URL.
data: The payload parameters, if any.
Returns:
The API response.
"""
headers = {"Authorization": "JWT " + self._jwt_token}

opts = {
"headers": headers,
"allow_redirects": False,
}

if method.upper() in ["GET", "DELETE"]:
opts["params"] = data
else:
opts["json"] = data

response = self._session.request(method, url, **opts)

try:
response.raise_for_status()
except HTTPError as error:
print("error: ", error)
return {}

if response.text != "":
return response.json()
else:
# return status code for empty response
return {"data": {"status": response.status_code}}
2 changes: 1 addition & 1 deletion dojima/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from brokers.ledgerx import LedgerX
from dojima.brokers import LedgerX
from constants import LEDGERX_JWT


Expand Down

0 comments on commit 5e92430

Please sign in to comment.