Skip to content

Commit 44e60b7

Browse files
Merge pull request #3 from Nasdaq/jc/local-credentials
DRY local configuration changes and apikey autoloading
2 parents 991ea12 + 5ff4ae9 commit 44e60b7

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,24 @@ nasdaqdatalink.ApiConfig.verify_ssl = False
3737
```
3838

3939
### Local API Key file
40-
Save local key to `$HOME/.nasdaq_data_link_api_key` file
41-
```
42-
import nasdaqdatalink
43-
nasdaqdatalink.save_key("supersecret")
44-
print(nasdaqdatalink.ApiConfig.api_key)
45-
```
4640

47-
Load the API Key without exposing the key in the script or notebook
41+
The default configuration file location is `~/.nasdaq/data_link_apikey`. The
42+
client will attempt to load this file if it exists. Note: if the file exists
43+
and empty, a ValueError will be thrown.
44+
45+
Save your api key locally:
4846
```
4947
import nasdaqdatalink
50-
nasdaqdatalink.read_key()
48+
nasdaqdatalink.save_key("supersecret")
5149
print(nasdaqdatalink.ApiConfig.api_key)
5250
```
5351

54-
Set a custom location for the API key file, e.g. store the externally outside a docker container
52+
Set a custom location for the API key file:
5553
```
5654
import nasdaqdatalink
5755
nasdaqdatalink.save_key("ourcorporateapikey", filename="/srv/data/somecontainer/.corporatenasdaqdatalinkapikey")
5856
```
59-
and call within the docker container with mount point at `/data`
57+
Requires an explicit read_key() call with the absolute path:
6058
```
6159
import nasdaqdatalink
6260
nasdaqdatalink.read_key(filepath="/data/.corporatenasdaqdatalinkapikey")

nasdaqdatalink/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
from .export_table import export_table
1616
from .get_table import get_table
1717
from .get_point_in_time import get_point_in_time
18+
19+
20+
if api_config.default_config_file_exists():
21+
read_key()

nasdaqdatalink/api_config.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,57 @@ class ApiConfig:
1616
verify_ssl = True
1717

1818

19+
def create_file(config_filename):
20+
# Create the file as well as the parent dir if needed.
21+
dirname = os.path.split(config_filename)[0]
22+
if not os.path.isdir(dirname):
23+
os.makedirs(dirname)
24+
with os.fdopen(os.open(config_filename,
25+
os.O_WRONLY | os.O_CREAT, 0o600), 'w'):
26+
pass
27+
28+
29+
def create_file_if_necessary(config_filename):
30+
if not os.path.isfile(config_filename):
31+
create_file(config_filename)
32+
33+
34+
def default_config_filename():
35+
config_file = os.path.join('~', '.nasdaq', 'data_link_apikey')
36+
return os.path.expanduser(config_file)
37+
38+
39+
def default_config_file_exists():
40+
config_filename = default_config_filename()
41+
return os.path.isfile(config_filename)
42+
43+
1944
def save_key(apikey, filename=None):
2045
if filename is None:
21-
filename = os.path.join(os.path.expanduser('~'), '.nasdaq_data_link_api_key')
46+
filename = default_config_filename()
47+
create_file_if_necessary(filename)
2248

2349
fileptr = open(filename, 'w')
2450
fileptr.write(apikey)
2551
fileptr.close()
2652
ApiConfig.api_key = apikey
2753

2854

55+
def raise_empty_file(config_filename):
56+
raise ValueError("File '{:s}' is empty.".format(config_filename))
57+
58+
2959
def read_key(filename=None):
3060
if filename is None:
31-
filename = os.path.join(os.path.expanduser('~'), '.nasdaq_data_link_api_key')
61+
filename = default_config_filename()
62+
63+
if not os.path.isfile(filename):
64+
raise_empty_file(filename)
3265

3366
with open(filename, 'r') as f:
3467
apikey = f.read()
3568

3669
if not apikey:
37-
raise ValueError("File '{:s}' is empty.".format(filename))
70+
raise_empty_file(filename)
3871

3972
ApiConfig.api_key = apikey

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