qmenta-core 4.1.1.dev722__tar.gz → 5.0.0.dev723__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: qmenta-core
3
- Version: 4.1.1.dev722
3
+ Version: 5.0.0.dev723
4
4
  Summary: QMENTA core library to communicate with the QMENTA platform.
5
5
  License: Proprietary
6
6
  Author: QMENTA
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "qmenta-core"
3
- version = "4.1.1.dev722"
3
+ version = "5.0.0.dev723"
4
4
  description = "QMENTA core library to communicate with the QMENTA platform."
5
5
  license = "Proprietary"
6
6
  authors = ["QMENTA <dev@qmenta.com>"]
@@ -111,12 +111,34 @@ class Auth:
111
111
  return cls(url, token)
112
112
 
113
113
  @classmethod
114
- def login(cls, username: str, password: str,
114
+ def login(cls,
115
+ username: Optional[str] = None,
116
+ password: Optional[str] = None,
115
117
  code_2fa: Optional[str] = None,
118
+ *,
119
+ api_key: Optional[str] = None,
116
120
  ask_for_2fa_input: bool = False,
117
121
  base_url: str = PlatformURL.platform.value) -> 'Auth':
118
122
  """
119
- Authenticate to the platform using username and password.
123
+ Authenticate to the platform using username and password,
124
+ or an API key.
125
+
126
+ ``api_key``, ``ask_for_2fa_input``, and ``base_url`` must be passed
127
+ as keyword arguments.
128
+
129
+ Valid parameter combinations:
130
+
131
+ - ``username``, ``password``: standard login.
132
+ - ``username``, ``password``, ``code_2fa``: login with a
133
+ pre-supplied 2FA code.
134
+ - ``username``, ``password``, ``ask_for_2fa_input=True``: login
135
+ and prompt for the 2FA code interactively when required.
136
+ - ``api_key``: login using an API key.
137
+
138
+ ``base_url`` can be combined with any of the above to target a
139
+ specific platform environment.
140
+ When ``username`` + ``password`` and ``api_key`` are all provided,
141
+ ``username`` + ``password`` takes precedence.
120
142
 
121
143
  Parameters
122
144
  ----------
@@ -128,6 +150,10 @@ class Auth:
128
150
  The QMENTA platform password of the user.
129
151
  code_2fa : str
130
152
  The 2FA code that was sent to your phone (optional).
153
+ api_key : str
154
+ A QMENTA API key. See the platform manual to learn how to
155
+ generate one. When provided together with ``username`` and
156
+ ``password``, the username/password credentials take precedence.
131
157
  ask_for_2fa_input: bool
132
158
  When set to True, the user is asked input the 2FA code
133
159
  in the command-line interface when it is needed. If the user does
@@ -154,21 +180,36 @@ class Auth:
154
180
  If the platform returned an invalid response.
155
181
  InvalidLoginError
156
182
  If the login was invalid. This can happen when the
157
- username/password combination is incorrect, or when the account is
158
- not active or 2FA is required to be set up.
183
+ username/password combination is incorrect, when the api_key is
184
+ invalid, when the account is not active, or when 2FA is required
185
+ to be set up. Also raised when neither credentials nor api_key
186
+ are provided.
159
187
  Needs2FAError
160
188
  When a login attempt was done without a valid 2FA code.
161
189
  The 2FA code has been sent to your phone, and must be provided
162
190
  in the next call to the login function.
163
191
  """
164
192
  url: str = urljoin(base_url, '/login')
165
-
166
- try:
167
- response: requests.Response = requests.post(
168
- url, data={
193
+ if username and password:
194
+ request_data = {
169
195
  'username': username, 'password': password,
170
196
  'code_2fa': code_2fa
171
197
  }
198
+ headers = {}
199
+ elif api_key:
200
+ headers = {
201
+ 'X-QMENTA-API-KEY': api_key
202
+ }
203
+ request_data = {}
204
+ else:
205
+ raise InvalidLoginError(
206
+ 'Missing username and password, or api-key'
207
+ )
208
+ try:
209
+ response: requests.Response = requests.post(
210
+ url,
211
+ data=request_data,
212
+ headers=headers
172
213
  )
173
214
  # Raises an exception for 4xx and 5xx status codes
174
215
  response.raise_for_status()
@@ -186,7 +227,6 @@ class Auth:
186
227
  raise ConnectionError(str(e))
187
228
  except Exception as e:
188
229
  raise Exception("An error occurred:", e)
189
-
190
230
  try:
191
231
  if data["success"] != 1:
192
232
  # Login was not successful