Skip to content

Commit 3478b48

Browse files
CP-9044/api-key-as-environment-variable (#6)
* CP-9044/api-key-as-environment-variable: Checking for environment variable before files
1 parent 4ae3763 commit 3478b48

File tree

5 files changed

+106
-8
lines changed

5 files changed

+106
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
.idea/*
1111
venv
1212
/.DS_Store
13+
/test/.nasdaq-config

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ By default, SSL verification is enabled. To bypass SSL verification
3636
nasdaqdatalink.ApiConfig.verify_ssl = False
3737
```
3838

39+
### Local API Key Environment Variable
40+
41+
If you wish to store your API as an environment variable, you can do so by setting `NASDAQ_DATA_LINK_API_KEY`. If set, NASDAQ_DATA_LINK_API_KEY will take precedence over the API Key file mentioned below.
42+
3943
### Local API Key file
4044

4145
The default configuration file location is `~/.nasdaq/data_link_apikey`. The
@@ -60,7 +64,6 @@ import nasdaqdatalink
6064
nasdaqdatalink.read_key(filepath="/data/.corporatenasdaqdatalinkapikey")
6165
```
6266

63-
6467
## Retrieving Data
6568

6669
There are two methods for retrieving data in Python: the Quick method and the Detailed method. The latter is more suitable to application programming. Both methods work with Nasdaq Data Link's two types of data structures: time-series (dataset) data and non-time series (datatable).
@@ -124,7 +127,7 @@ We recommend the following tools for testing any changes:
124127

125128
The following are instructions for running our tests:
126129

127-
1. Make sure a version of python 2.7 or python 3.x is installed locally in your system. To avoid permission issues on OSX we recommend installing the packages from: https://www.python.org/downloads/
130+
1. Make sure a version of 3.x is installed locally in your system. To avoid permission issues on OSX we recommend installing the packages from: https://www.python.org/downloads/
128131
2. Install `virtualenv` and `tox` using:
129132
`pip install tox virtualenv`
130133
3. Run following command (you may notice slow performance the first time):

nasdaqdatalink/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@
1717
from .get_point_in_time import get_point_in_time
1818

1919

20-
if api_config.default_config_file_exists():
21-
read_key()
20+
read_key()

nasdaqdatalink/api_config.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22

3+
NASDAQ_DATA_LINK_API_KEY = "NASDAQ_DATA_LINK_API_KEY"
4+
35

46
class ApiConfig:
57
api_key = None
@@ -36,9 +38,11 @@ def default_config_filename():
3638
return os.path.expanduser(config_file)
3739

3840

39-
def default_config_file_exists():
40-
config_filename = default_config_filename()
41-
return os.path.isfile(config_filename)
41+
def config_file_exists(filename=None):
42+
if filename is None:
43+
filename = default_config_filename()
44+
45+
return os.path.isfile(filename)
4246

4347

4448
def save_key(apikey, filename=None):
@@ -56,7 +60,7 @@ def raise_empty_file(config_filename):
5660
raise ValueError("File '{:s}' is empty.".format(config_filename))
5761

5862

59-
def read_key(filename=None):
63+
def read_key_from_file(filename=None):
6064
if filename is None:
6165
filename = default_config_filename()
6266

@@ -70,3 +74,18 @@ def read_key(filename=None):
7074
raise_empty_file(filename)
7175

7276
ApiConfig.api_key = apikey
77+
78+
79+
def api_key_environment_variable_exists():
80+
return NASDAQ_DATA_LINK_API_KEY in os.environ
81+
82+
83+
def read_key_from_environment_variable():
84+
ApiConfig.api_key = os.environ.get(NASDAQ_DATA_LINK_API_KEY)
85+
86+
87+
def read_key(filename=None):
88+
if api_key_environment_variable_exists():
89+
read_key_from_environment_variable()
90+
elif config_file_exists(filename):
91+
read_key_from_file(filename)

test/test_api_config.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# flake8: noqa
2+
3+
import os
4+
from unittest import TestCase, mock
5+
from nasdaqdatalink.api_config import *
6+
7+
TEST_KEY_FILE = os.path.join(
8+
os.path.dirname(os.path.realpath(__file__)), ".nasdaq-config", "testkeyfile"
9+
)
10+
11+
TEST_DEFAULT_FILE = os.path.join(
12+
os.path.dirname(os.path.realpath(__file__)), ".nasdaq-config", "defaultkeyfile"
13+
)
14+
15+
16+
class ApiConfigTest(TestCase):
17+
def setUp(self):
18+
if not os.path.exists(os.path.dirname(TEST_KEY_FILE)):
19+
try:
20+
os.makedirs(os.path.dirname(TEST_KEY_FILE))
21+
except OSError as exc: # Guard against race condition
22+
if exc.errno != errno.EEXIST:
23+
raise
24+
25+
def tearDown(self):
26+
if NASDAQ_DATA_LINK_API_KEY in os.environ:
27+
del os.environ['NASDAQ_DATA_LINK_API_KEY']
28+
29+
if os.path.exists(TEST_KEY_FILE):
30+
os.remove(TEST_KEY_FILE)
31+
32+
if os.path.exists(TEST_DEFAULT_FILE):
33+
os.remove(TEST_DEFAULT_FILE)
34+
35+
36+
def test_read_key_when_environment_variable_set(self):
37+
os.environ['NASDAQ_DATA_LINK_API_KEY'] = 'setinenv'
38+
ApiConfig.api_key = None
39+
read_key()
40+
self.assertEqual(ApiConfig.api_key, "setinenv")
41+
42+
43+
def test_read_key_environment_variable_takes_precedence(self):
44+
os.environ['NASDAQ_DATA_LINK_API_KEY'] = 'setinenvprecedence'
45+
save_key("keyforfilenot", TEST_KEY_FILE)
46+
ApiConfig.api_key = None
47+
read_key()
48+
self.assertEqual(ApiConfig.api_key, "setinenvprecedence")
49+
50+
51+
def test_read_key_when_environment_variable_not_set(self):
52+
save_key("keyforfile", TEST_KEY_FILE)
53+
ApiConfig.api_key = None # Set None, we are not testing save_key
54+
read_key(TEST_KEY_FILE)
55+
self.assertEqual(ApiConfig.api_key, 'keyforfile')
56+
57+
58+
def test_read_key_when_files_not_set(self):
59+
ApiConfig.api_key = None
60+
with mock.patch("nasdaqdatalink.api_config.default_config_filename") as mock_default_config_filename:
61+
mock_default_config_filename.return_value = TEST_DEFAULT_FILE
62+
read_key()
63+
64+
mock_default_config_filename.assert_called_once
65+
self.assertEqual(ApiConfig.api_key, None)
66+
67+
68+
def test_read_key_when_default_file_set(self):
69+
save_key("keyfordefaultfile", TEST_DEFAULT_FILE)
70+
ApiConfig.api_key = None # Set None, we are not testing save_key
71+
72+
with mock.patch("nasdaqdatalink.api_config.default_config_filename") as mock_default_config_filename:
73+
mock_default_config_filename.return_value = TEST_DEFAULT_FILE
74+
read_key()
75+
76+
self.assertEqual(ApiConfig.api_key, 'keyfordefaultfile')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy