Skip to content

Commit

Permalink
added additional error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
whittlem committed Aug 16, 2023
1 parent 96dad6a commit 7222d7c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
24 changes: 16 additions & 8 deletions models/config/binance_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os.path
import re
import sys

from .default_parser import is_currency_valid, default_config_parse, merge_config_and_args

Expand Down Expand Up @@ -101,14 +102,21 @@ def parser(app, binance_config, args={}):
app.api_key_file = binance_config["api_key_file"]

if app.api_key_file is not None:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
binance_config["api_key"] = key
binance_config["api_secret"] = secret
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")
if not os.path.isfile(app.api_key_file):
try:
raise Exception(f"Unable to read {app.api_key_file}, please check the file exists and is readable. Remove it from the config file for test mode!\n")
except Exception as e:
print(f"{type(e).__name__}: {e}")
sys.exit(1)
else:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
binance_config["api_key"] = key
binance_config["api_secret"] = secret
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")

if "api_key" in binance_config and "api_secret" in binance_config and "api_url" in binance_config:
# validates the api key is syntactically correct
Expand Down
24 changes: 16 additions & 8 deletions models/config/coinbase_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os.path
import re
import sys

from .default_parser import is_currency_valid, default_config_parse, merge_config_and_args
from models.exchange.Granularity import Granularity
Expand Down Expand Up @@ -56,14 +57,21 @@ def parser(app, coinbase_config, args={}):
app.api_key_file = coinbase_config["api_key_file"]

if app.api_key_file is not None:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
coinbase_config["api_key"] = key
coinbase_config["api_secret"] = secret
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")
if not os.path.isfile(app.api_key_file):
try:
raise Exception(f"Unable to read {app.api_key_file}, please check the file exists and is readable. Remove \"api_key_file\" key from the config file for test mode!\n")
except Exception as e:
print(f"{type(e).__name__}: {e}")
sys.exit(1)
else:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
coinbase_config["api_key"] = key
coinbase_config["api_secret"] = secret
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")

if "api_url" in coinbase_config["config"]:
coinbase_config["api_url"] = coinbase_config["config"]["api_url"]
Expand Down
28 changes: 18 additions & 10 deletions models/config/coinbase_pro_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os.path
import re
import sys

from .default_parser import is_currency_valid, default_config_parse, merge_config_and_args
from models.exchange.Granularity import Granularity
Expand Down Expand Up @@ -57,16 +58,23 @@ def parser(app, coinbase_config, args={}):
app.api_key_file = coinbase_config["api_key_file"]

if app.api_key_file is not None:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
password = f.readline().strip()
coinbase_config["api_key"] = key
coinbase_config["api_secret"] = secret
coinbase_config["api_passphrase"] = password
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")
if not os.path.isfile(app.api_key_file):
try:
raise Exception(f"Unable to read {app.api_key_file}, please check the file exists and is readable. Remove \"api_key_file\" key from the config file for test mode!\n")
except Exception as e:
print(f"{type(e).__name__}: {e}")
sys.exit(1)
else:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
password = f.readline().strip()
coinbase_config["api_key"] = key
coinbase_config["api_secret"] = secret
coinbase_config["api_passphrase"] = password
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")

if "api_key" in coinbase_config and "api_secret" in coinbase_config and "api_passphrase" in coinbase_config and "api_url" in coinbase_config:

Expand Down
27 changes: 17 additions & 10 deletions models/config/kucoin_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ def parser(app, kucoin_config, args={}):
app.api_key_file = kucoin_config["api_key_file"]

if app.api_key_file is not None:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
password = f.readline().strip()
kucoin_config["api_key"] = key
kucoin_config["api_secret"] = secret
kucoin_config["api_passphrase"] = password
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")
if not os.path.isfile(app.api_key_file):
try:
raise Exception(f"Unable to read {app.api_key_file}, please check the file exists and is readable. Remove \"api_key_file\" key from the config file for test mode!\n")
except Exception as e:
print(f"{type(e).__name__}: {e}")
sys.exit(1)
else:
try:
with open(app.api_key_file, "r") as f:
key = f.readline().strip()
secret = f.readline().strip()
password = f.readline().strip()
kucoin_config["api_key"] = key
kucoin_config["api_secret"] = secret
kucoin_config["api_passphrase"] = password
except Exception:
raise RuntimeError(f"Unable to read {app.api_key_file}")

if "api_key" in kucoin_config and "api_secret" in kucoin_config and "api_passphrase" in kucoin_config and "api_url" in kucoin_config:
# validates the api key is syntactically correct
Expand Down

0 comments on commit 7222d7c

Please sign in to comment.