-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use an API key when requesting OpenTopography data (#25)
* add api_key keyword to Topography * ignore .open_topography.txt file * return empty string rather than None for file contents * add OpenTopography API key to the test action * add unit tests for finding the api key * Add contributors * add a teeny bit of documentation about api keys * Use opentopography in place of open_topography Squished together is closer to the org name. Co-authored-by: Mark Piper <[email protected]>
- Loading branch information
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,6 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# OpenTopography API key file | ||
.opentopography.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
from unittest import mock | ||
|
||
from bmi_topography.topography import find_api_key, read_first_of | ||
|
||
|
||
def copy_environ(exclude=None): | ||
if exclude is None: | ||
exclude = {} | ||
elif isinstance(exclude, str): | ||
exclude = {exclude} | ||
|
||
return {key: value for key, value in os.environ.items() if key not in exclude} | ||
|
||
|
||
def test_find_api_key_not_found(): | ||
"""The API key is not given anywhere""" | ||
env = copy_environ(exclude="OPENTOPOGRAPHY_API_KEY") | ||
with mock.patch.dict(os.environ, env, clear=True): | ||
assert find_api_key() == "" | ||
|
||
|
||
@mock.patch.dict(os.environ, {"OPENTOPOGRAPHY_API_KEY": "foo"}) | ||
def test_find_api_key_env(tmpdir): | ||
"""The API key is an environment variable""" | ||
with tmpdir.as_cwd(): | ||
with open(".opentopography.txt", "w") as fp: | ||
fp.write("bar") | ||
assert find_api_key() == "foo" | ||
|
||
|
||
@mock.patch.dict(os.environ, {"OPENTOPOGRAPHY_API_KEY": "foo"}) | ||
def test_find_api_key_from_file(tmpdir): | ||
"""The API key is in a file""" | ||
env = copy_environ(exclude="OPENTOPOGRAPHY_API_KEY") | ||
with tmpdir.as_cwd(): | ||
with open(".opentopography.txt", "w") as fp: | ||
fp.write("bar") | ||
|
||
with mock.patch.dict(os.environ, env, clear=True): | ||
assert find_api_key() == "bar" | ||
|
||
|
||
def test_read_first_missing(tmpdir): | ||
with tmpdir.as_cwd(): | ||
assert read_first_of(["foo.txt"]) == "" | ||
assert read_first_of([]) == "" | ||
|
||
|
||
def test_read_first_file(tmpdir): | ||
with tmpdir.as_cwd(): | ||
with open("foo.txt", "w") as fp: | ||
fp.write("foo") | ||
with open("bar.txt", "w") as fp: | ||
fp.write("bar") | ||
|
||
assert read_first_of(["foo.txt", "bar.txt"]) == "foo" | ||
assert read_first_of(["bar.txt", "foo.txt"]) == "bar" |