Skip to content

Commit

Permalink
init: add read_key() convenience
Browse files Browse the repository at this point in the history
Currently the data link library allows users to define a simple
credential file where the api key is anticipated, but requires an
explicit call to read_key().  Introduce convenience logic to set the api
key in global configuration, if the file exist.

Update documentation in support of read_key() convenience.

Signed-off-by: Jamie Couture <[email protected]>
  • Loading branch information
couture-ql committed Nov 5, 2021
1 parent fe273db commit 5430535
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,23 @@ nasdaqdatalink.ApiConfig.verify_ssl = False

### Local API Key file

The default configuration file location is `~/.nasdaq/data_link_apikey`.
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 api key locally:
Save your api key locally:
```
import nasdaqdatalink
nasdaqdatalink.save_key("supersecret")
print(nasdaqdatalink.ApiConfig.api_key)
```

Load the API Key without exposing the key in the script or notebook

```
import nasdaqdatalink
nasdaqdatalink.read_key()
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()
5 changes: 5 additions & 0 deletions nasdaqdatalink/api_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def default_config_filename():
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 = default_config_filename()
Expand Down

0 comments on commit 5430535

Please sign in to comment.