diff --git a/celigo/core.py b/celigo/core.py index fe7a7c6..e1060d3 100644 --- a/celigo/core.py +++ b/celigo/core.py @@ -19,14 +19,11 @@ class BackupCeligo(object): """ def __init__(self, data_dir, - api_key, headers=None, base_url=None,): """ :param data_dir: The directory to read and store JSON files for imports and configurations. - :param api_key: String of api key to add to the header for - the api calls. :param headers: Single level dict of headers to add the the api call session. :param base_url: Url for the base of the api to call to. @@ -35,7 +32,9 @@ class BackupCeligo(object): if not base_url: base_url = DEFAULT_BASE_URL - self.api_key = api_key + self.api_key = os.environ.get("CELIGO_API_KEY", None) + if not self.api_key: + raise Exception("Please set CELIGO_API_KEY environment variable") self.data_dir = data_dir self.base_url = base_url self.imports_cache = {} diff --git a/tests.py b/tests.py index ea69f42..17b9efb 100644 --- a/tests.py +++ b/tests.py @@ -13,22 +13,34 @@ class CeligoTest(unittest.TestCase): def setUp(self): self.FAKE_API_KEY = 'fake' + os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY def test_needs_all_params(self): """ - Test that we need to pass in both an api key and a data directory. + Test that we need to pass in a data directory. """ - with pytest.raises(TypeError): + with self.assertRaises(TypeError): celigo.BackupCeligo() data_dir = "fakedir" - celigo.BackupCeligo(data_dir, self.FAKE_API_KEY) + celigo.BackupCeligo(data_dir) + + def test_needs_api_key_env(self): + """ + Test that we need to pass in a data directory. + """ + data_dir = "fakedir" + os.environ['CELIGO_API_KEY'] = '' + with self.assertRaises(Exception): + celigo.BackupCeligo(data_dir) + os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY + celigo.BackupCeligo(data_dir) def test_ensure_directories(self): """ Test that the ensure_directories_exist works properly. """ tempdir = tempfile.mkdtemp('celigo_testing') - bc = celigo.BackupCeligo(tempdir, self.FAKE_API_KEY) + bc = celigo.BackupCeligo(tempdir) imports_dir = os.path.join(tempdir, "imports") connections_dir = os.path.join(tempdir, "connections") # Check that the directories don't exist already. @@ -56,4 +68,4 @@ class CeligoTest(unittest.TestCase): def test_fake_requests(self, rqm): rqm.get(celigo.DEFAULT_BASE_URL, text='resp') tempdir = tempfile.mkdtemp('celigo_testing') - bc = celigo.BackupCeligo(tempdir, self.FAKE_API_KEY) + bc = celigo.BackupCeligo(tempdir)