Skip to content

Commit 68d233a

Browse files
authored
Merge pull request #2483 from astrofrog/api-key-error
Astrometry.net: Improvements to API key warnings/errors
2 parents 3b37e03 + ed1f080 commit 68d233a

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ astrometry.net
3737
solved by constructing a source list internally before sending data to
3838
astrometry.net. [#2484]
3939

40+
astrometry.net
41+
^^^^^^^^^^^^^^
42+
43+
- Avoid duplicated warnings about API key and raise an error only when API key is
44+
needed but not set. [#2483]
45+
4046
cadc
4147
^^^^
4248

astroquery/astrometry_net/core.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@
3333
__all__ = ['AstrometryNet', 'AstrometryNetClass']
3434

3535

36+
MISSING_API_KEY = """
37+
Astrometry.net API key not set. You should either set this in the astroquery configuration file using:
38+
39+
[astrometry_net]
40+
api_key = ADD_YOUR_API_KEY_HERE
41+
42+
or you can set it for this session only using the ``conf`` object:
43+
44+
from astroquery.astrometry_net import conf
45+
conf.api_key = 'ADD_YOUR_API_KEY_HERE'
46+
47+
or using the ``api_key`` property on the ``AstrometryNet`` class:
48+
49+
from astroquery.astrometry_net import AstrometryNet
50+
AstrometryNet.api_key = 'ADD_YOUR_API_KEY_HERE'
51+
""".lstrip()
52+
53+
3654
@async_to_sync
3755
class AstrometryNetClass(BaseQuery):
3856
"""
@@ -71,7 +89,7 @@ class AstrometryNetClass(BaseQuery):
7189
def api_key(self):
7290
""" Return the Astrometry.net API key. """
7391
if not conf.api_key:
74-
log.error("Astrometry.net API key not in configuration file")
92+
raise RuntimeError(MISSING_API_KEY)
7593
return conf.api_key
7694

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

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

115127
def _login(self):
116-
if not self.api_key:
117-
raise RuntimeError('You must set the API key before using this service.')
118128
login_url = url_helpers.join(self.API_URL, 'login')
119129
payload = self._construct_payload({'apikey': self.api_key})
120130
result = self._request('POST', login_url,

astroquery/astrometry_net/tests/test_astrometry_net.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ def data_path(filename):
1313
return os.path.join(DATA_DIR, filename)
1414

1515

16-
def test_api_key_property(caplog):
16+
def test_api_key_property():
1717
"""
1818
Check that an empty key is returned if the api key is not in
1919
the configuration file and that the expected message shows up in
2020
the log.
2121
"""
22-
caplog.clear()
2322
anet = AstrometryNet()
24-
key = anet.api_key
25-
assert not key
26-
assert "Astrometry.net API key not in configuration file" in caplog.text
23+
with pytest.raises(RuntimeError, match='Astrometry.net API key not set'):
24+
anet.api_key
2725

2826

2927
def test_empty_settings_property():
@@ -56,9 +54,8 @@ def test_login_fails_with_no_api_key():
5654
"""
5755
anet = AstrometryNet()
5856
anet.api_key = ''
59-
with pytest.raises(RuntimeError) as e:
57+
with pytest.raises(RuntimeError, match='Astrometry.net API key not set'):
6058
anet._login()
61-
assert "You must set the API key before using this service." in str(e.value)
6259

6360

6461
def test_construct_payload():

0 commit comments

Comments
 (0)