Skip to content

Commit

Permalink
Merge pull request #2483 from astrofrog/api-key-error
Browse files Browse the repository at this point in the history
Astrometry.net: Improvements to API key warnings/errors
  • Loading branch information
bsipocz authored Aug 17, 2022
2 parents 3b37e03 + ed1f080 commit 68d233a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ astrometry.net
solved by constructing a source list internally before sending data to
astrometry.net. [#2484]

astrometry.net
^^^^^^^^^^^^^^

- Avoid duplicated warnings about API key and raise an error only when API key is
needed but not set. [#2483]

cadc
^^^^

Expand Down
28 changes: 19 additions & 9 deletions astroquery/astrometry_net/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
__all__ = ['AstrometryNet', 'AstrometryNetClass']


MISSING_API_KEY = """
Astrometry.net API key not set. You should either set this in the astroquery configuration file using:
[astrometry_net]
api_key = ADD_YOUR_API_KEY_HERE
or you can set it for this session only using the ``conf`` object:
from astroquery.astrometry_net import conf
conf.api_key = 'ADD_YOUR_API_KEY_HERE'
or using the ``api_key`` property on the ``AstrometryNet`` class:
from astroquery.astrometry_net import AstrometryNet
AstrometryNet.api_key = 'ADD_YOUR_API_KEY_HERE'
""".lstrip()


@async_to_sync
class AstrometryNetClass(BaseQuery):
"""
Expand Down Expand Up @@ -71,7 +89,7 @@ class AstrometryNetClass(BaseQuery):
def api_key(self):
""" Return the Astrometry.net API key. """
if not conf.api_key:
log.error("Astrometry.net API key not in configuration file")
raise RuntimeError(MISSING_API_KEY)
return conf.api_key

@api_key.setter
Expand Down Expand Up @@ -103,18 +121,10 @@ def show_allowed_settings(self):
values=key_info['allowed']))

def __init__(self):
""" Show a warning message if the API key is not in the configuration file. """
super().__init__()
if not conf.api_key:
log.warning("Astrometry.net API key not found in configuration file")
log.warning("You need to manually edit the configuration file and add it")
log.warning(
"You may also register it for this session with AstrometryNet.key = 'XXXXXXXX'")
self._session_id = None

def _login(self):
if not self.api_key:
raise RuntimeError('You must set the API key before using this service.')
login_url = url_helpers.join(self.API_URL, 'login')
payload = self._construct_payload({'apikey': self.api_key})
result = self._request('POST', login_url,
Expand Down
11 changes: 4 additions & 7 deletions astroquery/astrometry_net/tests/test_astrometry_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ def data_path(filename):
return os.path.join(DATA_DIR, filename)


def test_api_key_property(caplog):
def test_api_key_property():
"""
Check that an empty key is returned if the api key is not in
the configuration file and that the expected message shows up in
the log.
"""
caplog.clear()
anet = AstrometryNet()
key = anet.api_key
assert not key
assert "Astrometry.net API key not in configuration file" in caplog.text
with pytest.raises(RuntimeError, match='Astrometry.net API key not set'):
anet.api_key


def test_empty_settings_property():
Expand Down Expand Up @@ -56,9 +54,8 @@ def test_login_fails_with_no_api_key():
"""
anet = AstrometryNet()
anet.api_key = ''
with pytest.raises(RuntimeError) as e:
with pytest.raises(RuntimeError, match='Astrometry.net API key not set'):
anet._login()
assert "You must set the API key before using this service." in str(e.value)


def test_construct_payload():
Expand Down

0 comments on commit 68d233a

Please sign in to comment.