Skip to content

Commit

Permalink
Make explicit when an optional variable is 'None' when not being used
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Zacchigna committed Apr 18, 2024
1 parent 725e268 commit 07891b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
6 changes: 2 additions & 4 deletions pyetrade/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ def list_transactions(
)

payload = {
"startDate": start_date.date().strftime("%m%d%Y")
if start_date
else start_date,
"endDate": end_date.date().strftime("%m%d%Y") if end_date else end_date,
"startDate": start_date.date().strftime("%m%d%Y") if start_date else None,
"endDate": end_date.date().strftime("%m%d%Y") if end_date else None,
"sortOrder": sort_order,
"marker": marker,
"count": count,
Expand Down
1 change: 0 additions & 1 deletion pyetrade/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ def get_option_expire_date(self, symbol: str, resp_format: str = "xml") -> dict:
:EtradeRef: https://apisb.etrade.com/docs/api/market/api-market-v1.html
"""

assert isinstance(resp_format, str)
assert resp_format in ["xml", "json"]

api_url = "%s%s" % (
Expand Down
26 changes: 14 additions & 12 deletions pyetrade/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ def to_decimal_str(price: float, round_down: bool) -> str:
def get_request_result(req: OAuth1Session.request, resp_format: str = "xml") -> dict:
LOGGER.debug(req.text)

if resp_format == "json":
if req.text.strip() == "":
# otherwise, when ETrade server return empty string, we got this error:
# simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
req_output = {}
else:
req_output = req.json()
else:
# Initialize as empty dict, otherwise, when ETrade server returns an empty string, you get this error:
# "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
req_output = {}

assert resp_format in ["xml", "json"]

if resp_format == "json" and req.text.strip() != "":
req_output = req.json()
elif resp_format == "xml":
req_output = xmltodict.parse(req.text)

if "Error" in req_output.keys():
raise Exception(
f'Etrade API Error - Code: {req_output["Error"]["code"]}, Msg: {req_output["Error"]["message"]}'
)

return req_output


Expand Down Expand Up @@ -204,17 +206,17 @@ def list_orders(

if count >= 101:
LOGGER.debug(
f"Count {count} is greater than the max allowable value (100), using 100"
f"Count {count} is greater than the max allowable value (100), using 100."
)
count = 100

payload = {
"marker": marker,
"count": count,
"status": status,
"fromDate": from_date.date().strftime("%m%d%Y") if from_date else from_date,
"toDate": to_date.date().strftime("%m%d%Y") if to_date else to_date,
"symbol": ",".join([sym for sym in symbols[:25]]) if symbols else symbols,
"fromDate": from_date.date().strftime("%m%d%Y") if from_date else None,
"toDate": to_date.date().strftime("%m%d%Y") if to_date else None,
"symbol": ",".join([sym for sym in symbols[:25]]) if symbols else None,
"securityType": security_type,
"transactionType": transaction_type,
"marketSession": market_session,
Expand Down

0 comments on commit 07891b6

Please sign in to comment.