arkindex-client 1.0.9__py3-none-any.whl → 1.0.12__py3-none-any.whl

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.
arkindex/client.py CHANGED
@@ -32,9 +32,18 @@ def options_from_env():
32
32
  Get API client keyword arguments from environment variables.
33
33
  """
34
34
  options = {}
35
- if "ARKINDEX_API_TOKEN" in os.environ:
35
+ # ARKINDEX_TASK_TOKEN takes priority over ARKINDEX_API_TOKEN
36
+ if "ARKINDEX_TASK_TOKEN" in os.environ:
37
+ options["auth_scheme"] = "Ponos"
38
+ options["token"] = os.environ.get("ARKINDEX_TASK_TOKEN")
39
+ elif "ARKINDEX_API_TOKEN" in os.environ:
40
+ options["auth_scheme"] = "Token"
36
41
  options["token"] = os.environ.get("ARKINDEX_API_TOKEN")
37
42
 
43
+ # Allow overriding the default auth schemes
44
+ if "ARKINDEX_API_AUTH_SCHEME" in os.environ:
45
+ options["auth_scheme"] = os.environ.get("ARKINDEX_API_AUTH_SCHEME")
46
+
38
47
  if "ARKINDEX_API_URL" in os.environ:
39
48
  options["base_url"] = os.environ.get("ARKINDEX_API_URL")
40
49
 
@@ -63,6 +72,7 @@ class ArkindexClient(apistar.Client):
63
72
  def __init__(
64
73
  self,
65
74
  token=None,
75
+ auth_scheme="Token",
66
76
  base_url=DEFAULT_BASE_URL,
67
77
  schema_url=None,
68
78
  csrf_cookie=None,
@@ -139,7 +149,11 @@ class ArkindexClient(apistar.Client):
139
149
  csrf_cookie = csrf_cookies.get(split_base_url.netloc) or "arkindex.csrf"
140
150
 
141
151
  self.configure(
142
- token=token, base_url=base_url, csrf_cookie=csrf_cookie, sleep=sleep
152
+ token=token,
153
+ auth_scheme=auth_scheme,
154
+ base_url=base_url,
155
+ csrf_cookie=csrf_cookie,
156
+ sleep=sleep,
143
157
  )
144
158
 
145
159
  def __repr__(self):
@@ -151,12 +165,25 @@ class ArkindexClient(apistar.Client):
151
165
  def init_transport(self, *args, **kwargs):
152
166
  return ArkindexHTTPTransport(*args, **kwargs)
153
167
 
154
- def configure(self, token=None, base_url=None, csrf_cookie=None, sleep=None):
168
+ def configure(
169
+ self,
170
+ token=None,
171
+ auth_scheme="Token",
172
+ base_url=None,
173
+ csrf_cookie=None,
174
+ sleep=None,
175
+ ):
155
176
  """
156
177
  Reconfigure the API client.
157
178
 
158
179
  :param token: An API token to use. If omitted, access is restricted to public endpoints.
159
180
  :type token: str or None
181
+ :param auth_scheme:
182
+ An authentication scheme to use. This is added in the HTTP header before the token:
183
+ ``Authorization: [scheme] [token]``.
184
+ This should use ``Token`` to authenticate as a regular user and ``Ponos`` to authenticate as a Ponos task.
185
+ If omitted, this defaults to ``Token``.
186
+ :type auth_scheme: str or None
160
187
  :param base_url: A custom base URL for the client. If omitted, defaults to the Arkindex main server.
161
188
  :type base_url: str or None
162
189
  :param csrf_cookie: Use a custom CSRF cookie name. Falls back to ``arkindex.csrf``.
@@ -167,7 +194,9 @@ class ArkindexClient(apistar.Client):
167
194
  if not csrf_cookie:
168
195
  csrf_cookie = "arkindex.csrf"
169
196
  self.transport.session.auth = TokenSessionAuthentication(
170
- token, csrf_cookie_name=csrf_cookie
197
+ token,
198
+ csrf_cookie_name=csrf_cookie,
199
+ scheme=auth_scheme,
171
200
  )
172
201
 
173
202
  if not sleep or not isinstance(sleep, float) or sleep < 0:
@@ -200,6 +229,7 @@ class ArkindexClient(apistar.Client):
200
229
  """
201
230
  resp = self.request("Login", body={"email": email, "password": password})
202
231
  if "auth_token" in resp:
232
+ self.transport.session.auth.scheme = "Token"
203
233
  self.transport.session.auth.token = resp["auth_token"]
204
234
  return resp
205
235
 
arkindex/pagination.py CHANGED
@@ -81,6 +81,14 @@ class ResponsePaginator(Sized, Iterator):
81
81
  ), "allow_missing_data must be a boolean"
82
82
 
83
83
  def _fetch_page(self):
84
+ """
85
+ Retrieve the next page and store its results
86
+
87
+ Returns None in case of a failure
88
+ Returns True in case the page returned results
89
+ Returns False in case the page returned an empty result
90
+ Raises a StopIteration in case there are no pages left to iterate on
91
+ """
84
92
  # Filter out pages with no retries
85
93
  # Transform as a list of tuples for simpler output
86
94
  remaining = sorted([(m, v) for m, v in self.pages.items() if v > 0])
@@ -136,7 +144,7 @@ class ResponsePaginator(Sized, Iterator):
136
144
  if self.count == 0:
137
145
  # Pagination has retrieved 0 results
138
146
  self.pages = {}
139
- return
147
+ return False
140
148
  self.pages_count = math.ceil(self.count / len(self.results))
141
149
  logger.info(
142
150
  f"Pagination will load a total of {self.pages_count} pages."
@@ -161,7 +169,7 @@ class ResponsePaginator(Sized, Iterator):
161
169
 
162
170
  # Stop happy path here, we don't need to process errors
163
171
  self.pages_loaded += 1
164
- return self.data
172
+ return True
165
173
 
166
174
  except apistar.exceptions.ErrorResponse as e:
167
175
  logger.warning(f"API Error {e.status_code} on pagination: {e.content}")
@@ -194,6 +202,9 @@ class ResponsePaginator(Sized, Iterator):
194
202
  else:
195
203
  raise Exception("Stopping pagination as data will be incomplete")
196
204
 
205
+ # No data could be fetch, return None
206
+ return None
207
+
197
208
  def __iter__(self):
198
209
  return self
199
210
 
@@ -216,7 +227,12 @@ class ResponsePaginator(Sized, Iterator):
216
227
  def __len__(self):
217
228
  # Handle calls to len when no requests have been made yet
218
229
  if not self.pages_loaded and self.count is None:
219
- self._fetch_page()
230
+ # Continuously try to fetch a page until there are no retries left
231
+ while self._fetch_page() is None:
232
+ pass
233
+ # Count may be null in case of a StopIteration
234
+ if self.count is None:
235
+ raise Exception("An error occurred fetching items total count")
220
236
  return self.count
221
237
 
222
238
  def __repr__(self):
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: arkindex-client
3
- Version: 1.0.9
3
+ Version: 1.0.12
4
4
  Summary: API client for the Arkindex project
5
- Home-page: https://gitlab.com/arkindex/api-client
5
+ Home-page: https://gitlab.com/teklia/arkindex/api-client
6
6
  Author: Teklia <contact@teklia.com>
7
7
  License: MIT
8
8
  Keywords: api client arkindex
@@ -12,15 +12,15 @@ Classifier: Natural Language :: English
12
12
  Classifier: Intended Audience :: Science/Research
13
13
  Classifier: Programming Language :: Python :: 3
14
14
  Classifier: Programming Language :: Python :: 3 :: Only
15
- Classifier: Programming Language :: Python :: 3.6
16
15
  Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
17
  Classifier: Topic :: Scientific/Engineering :: Image Recognition
18
18
  Classifier: Topic :: Text Processing :: Indexing
19
19
  Classifier: Topic :: Text Processing :: Linguistic
20
- Requires-Python: >=3.6
20
+ Requires-Python: >=3.7
21
21
  Description-Content-Type: text/x-rst
22
22
  Requires-Dist: apistar (==0.7.2)
23
- Requires-Dist: requests (==2.27.1)
23
+ Requires-Dist: requests (==2.28.1)
24
24
  Requires-Dist: typesystem (==0.2.5)
25
25
 
26
26
  Arkindex API Client
@@ -0,0 +1,11 @@
1
+ arkindex/__init__.py,sha256=OBw25Nwe6r1AN3a36-8lnUcDy3nkDIiebBgYNK2Zo9o,99
2
+ arkindex/auth.py,sha256=O45HgpsblBqNJr9nRALU14wZgCGcmCgcGjAoD0zAyrg,1073
3
+ arkindex/client.py,sha256=OndKsDpGPrQRdS1XQc_tPMnPaif9CQ3ta60VNdFpMgQ,9648
4
+ arkindex/exceptions.py,sha256=3jzuuXgsPA28qH7GMwPVQisWCvitAQtYiQ5uKxzFQ_M,155
5
+ arkindex/mock.py,sha256=hWU8EKfvVBAQ8VVI4Rgic809GLLDoh8Nvuvih54luKQ,2762
6
+ arkindex/pagination.py,sha256=mOF2wI70lnXeF89j4UJnhTUoVN4c_CrtmIQvbz92YXA,8826
7
+ arkindex/transports.py,sha256=cBnCQ1MPaBA163bAa3UAd7lUG-SFStYkOPajer_C_GM,298
8
+ arkindex_client-1.0.12.dist-info/METADATA,sha256=bK6CdPvqllyVm81JKGqPyfjxtW2wSBetc0G1C-IJ7Rc,7016
9
+ arkindex_client-1.0.12.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
10
+ arkindex_client-1.0.12.dist-info/top_level.txt,sha256=iP1TxDW_jSDQA4FIcahBFXiDcZXqam2a_gFVnDbR3c4,9
11
+ arkindex_client-1.0.12.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.37.0)
2
+ Generator: bdist_wheel (0.40.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,11 +0,0 @@
1
- arkindex/__init__.py,sha256=OBw25Nwe6r1AN3a36-8lnUcDy3nkDIiebBgYNK2Zo9o,99
2
- arkindex/auth.py,sha256=O45HgpsblBqNJr9nRALU14wZgCGcmCgcGjAoD0zAyrg,1073
3
- arkindex/client.py,sha256=qEqwsSuVLpf0pGbSZ9VMMnNsURQ33pYjY6zb41NmpbI,8556
4
- arkindex/exceptions.py,sha256=3jzuuXgsPA28qH7GMwPVQisWCvitAQtYiQ5uKxzFQ_M,155
5
- arkindex/mock.py,sha256=hWU8EKfvVBAQ8VVI4Rgic809GLLDoh8Nvuvih54luKQ,2762
6
- arkindex/pagination.py,sha256=s36VjFN-H2PWhaM7WyotHmDYcwkz_7fIkmok0vTOmlg,8165
7
- arkindex/transports.py,sha256=cBnCQ1MPaBA163bAa3UAd7lUG-SFStYkOPajer_C_GM,298
8
- arkindex_client-1.0.9.dist-info/METADATA,sha256=_YwLSBrmv7n9ogEp3Z1epzdqhOJhZNLOaIkwYRaHSk4,7008
9
- arkindex_client-1.0.9.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
10
- arkindex_client-1.0.9.dist-info/top_level.txt,sha256=iP1TxDW_jSDQA4FIcahBFXiDcZXqam2a_gFVnDbR3c4,9
11
- arkindex_client-1.0.9.dist-info/RECORD,,