metaspace2020 / metaspace

Cloud engine and platform for metabolite annotation for imaging mass spectrometry
https://metaspace2020.eu/
Apache License 2.0
47 stars 10 forks source link

Fix AttributeError when checking `logged_in` #1549

Closed aeisenbarth closed 2 weeks ago

aeisenbarth commented 3 months ago

When METASPACE login fails in the constructor, the error is silently caught and printed, which goes unnoticed if not using an interactive console/notebook. When checking logged_in(), an AttributeError is raised because the instance is only fully initialized after successful login.

Example

>>> from metaspace.sm_annotation_utils import SMInstance
>>> sm = SMInstance(email="partyparrot@ateam.com", api_key="invalid-api_key")
Failed to connect to https://metaspace2020.eu: Invalid API key

>>> sm
SMInstance(https://metaspace2020.eu/graphql)
>>> sm.logged_in()
Traceback (most recent call last):
  File "…/metaspace/metaspace/python-client/metaspace/sm_annotation_utils.py", line 1707, in logged_in
    return self._gqclient.logged_in
AttributeError: 'SMInstance' object has no attribute '_gqclient'

Solution

I changed the API key login in GraphQLClient to be handled the same as password login: no exception, but setting login status and printing a message. This way, GraphQLClient can be instantiated despite invalid API key, and SMInstance can be completely initialized.