Skip to content

Commit bbc15da

Browse files
committed
Avoid duplicated warnings about API key being unset and avoid warning about API key before user has had chance to set it.
1 parent b1fcfff commit bbc15da

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ alma
2525

2626
- Fixed a regression to handle arrays of string input for the ``query`` methods. [#2094]
2727

28+
astrometry.net
29+
^^^^^^^^^^^^^^
30+
31+
- Avoid duplicated warnings about API key and raise an error only when API key is
32+
needed but not set. [#2483]
33+
2834
cadc
2935
^^^^
3036

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 = qwdqwjnoi12ioj
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 = 'qwdqwjnoi12ioj'
46+
47+
or using the ``api_key`` property on the ``AstrometryNet`` class:
48+
49+
from astroquery.astrometry_net import AstrometryNet
50+
AstrometryNet.api_key = 'qwdqwjnoi12ioj'
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,

0 commit comments

Comments
 (0)