Session¶
- class astrometry_net_client.session.Session(api_key: Optional[str] = None, key_location: Optional[str] = None)¶
Class to hold information on the Astrometry.net API session. Is able to read an API key from multiple possible locations, and login to retrieve a session key (stored in the id attribute).
- logged_in¶
Boolean indicating if a
login()
has happened successfully since creation of the class. Does not guarentee that the sessionkey
is (still) valid.- Type:
bool
- api_key¶
The API key used to log in.
- Type:
str
Examples
Normal usage of a session is as follows; you create the Session class, and then
login()
:>>> session = Session('the api key') >>> session.login()
There are multiple ways of giving your api key. You can just give it as a plain string (not very safe):
>>> session_1 = Session(api_key='the api key')
You can enter it in a separate private file and give the path to it. This file should only contain the key, nothing else.
>>> session_2 = Session(key_location='path/to/file/with/key')
You can specify the key as an environment variable named
ASTROMETRY_API_KEY
.>>> session_3 = Session()
If you do not specify any of the above, an exception will be thrown:
>>> Session() APIKeyError
- Raises:
APIKeyError – When no API key is specified (see examples)
- login(force: bool = False) None ¶
Method used to log-in or start a session with the Astrometry.net API.
Only sends a request if it is absolutely needed (or forced to). If
logged_in
is True, it assumes the session is still valid (there is no way to check if the session is still valid).After a successful login, the session key is stored in the
key
attribute andlogged_in
is set to True.- Parameters:
force (bool) – Forces the login by ignoring the
logged_in
boolean.- Raises:
LoginFailedException – If the login response does not have the status ‘success’.
- class astrometry_net_client.session.SessionRequest(session: Session, *args, **kwargs)¶
Wraps the normal Request around an authentication layout, ensuring the user is logged in and the session key is send alongside the request.
The separate login request (if needed) is only send just before the original request is made, (e.g. when calling make / _make_request).