Skip to content

DRY local configuration changes and apikey autoloading #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,24 @@ nasdaqdatalink.ApiConfig.verify_ssl = False
```

### Local API Key file
Save local key to `$HOME/.nasdaq_data_link_api_key` file
```
import nasdaqdatalink
nasdaqdatalink.save_key("supersecret")
print(nasdaqdatalink.ApiConfig.api_key)
```

Load the API Key without exposing the key in the script or notebook
The default configuration file location is `~/.nasdaq/data_link_apikey`. The
client will attempt to load this file if it exists. Note: if the file exists
and empty, a ValueError will be thrown.

Save your api key locally:
```
import nasdaqdatalink
nasdaqdatalink.read_key()
nasdaqdatalink.save_key("supersecret")
print(nasdaqdatalink.ApiConfig.api_key)
```

Set a custom location for the API key file, e.g. store the externally outside a docker container
Set a custom location for the API key file:
```
import nasdaqdatalink
nasdaqdatalink.save_key("ourcorporateapikey", filename="/srv/data/somecontainer/.corporatenasdaqdatalinkapikey")
```
and call within the docker container with mount point at `/data`
Requires an explicit read_key() call with the absolute path:
```
import nasdaqdatalink
nasdaqdatalink.read_key(filepath="/data/.corporatenasdaqdatalinkapikey")
Expand Down
4 changes: 4 additions & 0 deletions nasdaqdatalink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
from .export_table import export_table
from .get_table import get_table
from .get_point_in_time import get_point_in_time


if api_config.default_config_file_exists():
read_key()
39 changes: 36 additions & 3 deletions nasdaqdatalink/api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,57 @@ class ApiConfig:
verify_ssl = True


def create_file(config_filename):
# Create the file as well as the parent dir if needed.
dirname = os.path.split(config_filename)[0]
if not os.path.isdir(dirname):
os.makedirs(dirname)
with os.fdopen(os.open(config_filename,
os.O_WRONLY | os.O_CREAT, 0o600), 'w'):
pass


def create_file_if_necessary(config_filename):
if not os.path.isfile(config_filename):
create_file(config_filename)


def default_config_filename():
config_file = os.path.join('~', '.nasdaq', 'data_link_apikey')
return os.path.expanduser(config_file)


def default_config_file_exists():
config_filename = default_config_filename()
return os.path.isfile(config_filename)


def save_key(apikey, filename=None):
if filename is None:
filename = os.path.join(os.path.expanduser('~'), '.nasdaq_data_link_api_key')
filename = default_config_filename()
create_file_if_necessary(filename)

fileptr = open(filename, 'w')
fileptr.write(apikey)
fileptr.close()
ApiConfig.api_key = apikey


def raise_empty_file(config_filename):
raise ValueError("File '{:s}' is empty.".format(config_filename))


def read_key(filename=None):
if filename is None:
filename = os.path.join(os.path.expanduser('~'), '.nasdaq_data_link_api_key')
filename = default_config_filename()

if not os.path.isfile(filename):
raise_empty_file(filename)

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

if not apikey:
raise ValueError("File '{:s}' is empty.".format(filename))
raise_empty_file(filename)

ApiConfig.api_key = apikey
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