Skip to content

Commit

Permalink
api_config: raise errors for mis-configured environment variable
Browse files Browse the repository at this point in the history
It is possible users could define an environment variable but not set
it.  Help users avoid shooting themselves in the foot by raising an
exception when it is determine callers have:

  defined the NASDAQ_DATA_LINK_API_KEY; and
  it is considered empty

Signed-off-by: Jamie Couture <[email protected]>
  • Loading branch information
couture-ql committed Jan 28, 2022
1 parent 22325ad commit a478f5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion nasdaqdatalink/api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def raise_empty_file(config_filename):
raise ValueError("File '{:s}' is empty.".format(config_filename))


def raise_empty_environment_variable():
raise ValueError("NASDAQ_DATA_LINK_API_KEY cannot be empty")


def get_first_non_empty(file_handle):
lines = [line.strip() for line in file_handle.readlines()]
return next((line for line in lines if line), None)
Expand All @@ -86,7 +90,11 @@ def api_key_environment_variable_exists():


def read_key_from_environment_variable():
ApiConfig.api_key = os.environ.get(NASDAQ_DATA_LINK_API_KEY)
apikey = os.environ.get(NASDAQ_DATA_LINK_API_KEY)
if not apikey:
raise_empty_environment_variable()

ApiConfig.api_key = apikey


def read_key(filename=None):
Expand Down
6 changes: 6 additions & 0 deletions test/test_api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def test_read_key_empty_file(self):
read_key()


def test_read_key_when_env_key_empty(self):
os.environ['NASDAQ_DATA_LINK_API_KEY'] = ''
with self.assertRaises(ValueError):
read_key()


def test_read_key_when_files_not_set(self):
ApiConfig.api_key = None
with mock.patch("nasdaqdatalink.api_config.default_config_filename") as mock_default_config_filename:
Expand Down

0 comments on commit a478f5c

Please sign in to comment.