internetdata 2.9.5__tar.gz → 2.10.0__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.
Files changed (98) hide show
  1. {internetdata-2.9.5 → internetdata-2.10.0}/PKG-INFO +9 -1
  2. {internetdata-2.9.5 → internetdata-2.10.0}/README.md +8 -0
  3. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/__init__.py +1 -1
  4. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_core.py +11 -0
  5. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/aio.py +42 -10
  6. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/client.py +40 -10
  7. {internetdata-2.9.5 → internetdata-2.10.0}/tests/helpers.py +5 -2
  8. {internetdata-2.9.5 → internetdata-2.10.0}/tests/test_download.py +50 -0
  9. {internetdata-2.9.5 → internetdata-2.10.0}/tests/test_oauth.py +44 -0
  10. {internetdata-2.9.5 → internetdata-2.10.0}/.gitignore +0 -0
  11. {internetdata-2.9.5 → internetdata-2.10.0}/LICENSE +0 -0
  12. {internetdata-2.9.5 → internetdata-2.10.0}/pyproject.toml +0 -0
  13. {internetdata-2.9.5 → internetdata-2.10.0}/scripts/download-spec.sh +0 -0
  14. {internetdata-2.9.5 → internetdata-2.10.0}/scripts/generate.sh +0 -0
  15. {internetdata-2.9.5 → internetdata-2.10.0}/scripts/publish.sh +0 -0
  16. {internetdata-2.9.5 → internetdata-2.10.0}/scripts/v2_subset.py +0 -0
  17. {internetdata-2.9.5 → internetdata-2.10.0}/spec/openapi.yaml +0 -0
  18. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/__init__.py +0 -0
  19. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/__init__.py +0 -0
  20. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/account/__init__.py +0 -0
  21. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/account/account_identity.py +0 -0
  22. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/__init__.py +0 -0
  23. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/account_create_apikey.py +0 -0
  24. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/account_list_apikeys.py +0 -0
  25. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/account_reveal_apikey.py +0 -0
  26. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/account_revoke_apikey.py +0 -0
  27. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/api_keys/account_rotate_apikey.py +0 -0
  28. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/__init__.py +0 -0
  29. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_authorize.py +0 -0
  30. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_device_authorization.py +0 -0
  31. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_metadata.py +0 -0
  32. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_register.py +0 -0
  33. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_revoke.py +0 -0
  34. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/authorization/oauth_token.py +0 -0
  35. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/__init__.py +0 -0
  36. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/database_checksum_v2.py +0 -0
  37. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/database_metadata_v2.py +0 -0
  38. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/download_database_v2.py +0 -0
  39. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/list_databases.py +0 -0
  40. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/database_v_2/list_downloads.py +0 -0
  41. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/organization/__init__.py +0 -0
  42. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/organization/account_org.py +0 -0
  43. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/api/organization/account_org_members.py +0 -0
  44. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/client.py +0 -0
  45. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/errors.py +0 -0
  46. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/__init__.py +0 -0
  47. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_create_apikey_request.py +0 -0
  48. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_created_apikey.py +0 -0
  49. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_org.py +0 -0
  50. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_org_ref.py +0 -0
  51. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_org_wrap.py +0 -0
  52. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_rc.py +0 -0
  53. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_revealed_apikey.py +0 -0
  54. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/account_user.py +0 -0
  55. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/apikey_detail.py +0 -0
  56. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/apikey_list.py +0 -0
  57. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/client_registration.py +0 -0
  58. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/client_registration_request.py +0 -0
  59. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database.py +0 -0
  60. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_checksum_v2_response_200.py +0 -0
  61. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_format.py +0 -0
  62. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_license_type_type_1.py +0 -0
  63. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_license_type_type_2_type_1.py +0 -0
  64. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_license_type_type_3_type_1.py +0 -0
  65. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata.py +0 -0
  66. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_column.py +0 -0
  67. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_sample.py +0 -0
  68. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_sample_additional_property_item.py +0 -0
  69. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_sample_size.py +0 -0
  70. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_schema.py +0 -0
  71. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_metadata_size.py +0 -0
  72. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/database_version.py +0 -0
  73. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/db_checksums.py +0 -0
  74. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/device_authorization.py +0 -0
  75. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/device_authorization_request.py +0 -0
  76. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/download.py +0 -0
  77. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/download_outcome.py +0 -0
  78. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/error.py +0 -0
  79. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/identity.py +0 -0
  80. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/list_databases_response_200.py +0 -0
  81. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/list_downloads_response_200.py +0 -0
  82. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/oauth_authorize_code_challenge_method.py +0 -0
  83. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/oauth_authorize_response_type.py +0 -0
  84. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/oauth_error.py +0 -0
  85. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/oauth_metadata.py +0 -0
  86. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/oauth_revoke_response_200.py +0 -0
  87. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/revoke_request.py +0 -0
  88. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/standing.py +0 -0
  89. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/token_request.py +0 -0
  90. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/models/token_response.py +0 -0
  91. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/_generated/types.py +0 -0
  92. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/errors.py +0 -0
  93. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/models.py +0 -0
  94. {internetdata-2.9.5 → internetdata-2.10.0}/src/internetdata/py.typed +0 -0
  95. {internetdata-2.9.5 → internetdata-2.10.0}/testdata/testdata.json +0 -0
  96. {internetdata-2.9.5 → internetdata-2.10.0}/tests/conftest.py +0 -0
  97. {internetdata-2.9.5 → internetdata-2.10.0}/tests/test_client.py +0 -0
  98. {internetdata-2.9.5 → internetdata-2.10.0}/tests/test_conformance.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: internetdata
3
- Version: 2.9.5
3
+ Version: 2.10.0
4
4
  Summary: Official Python client library for the InternetData API. Download and verify licensed IP datasets.
5
5
  Project-URL: Homepage, https://internetdata.io
6
6
  Project-URL: Documentation, https://docs.internetdata.io
@@ -117,6 +117,14 @@ written = client.database.download("bogon_ip_v1", "mmdb", "./bogon_ip_v1.mmdb")
117
117
 
118
118
  Either way a short transfer fails rather than handing you a truncated file, and the key is never sent to object storage.
119
119
 
120
+ Each of the three downloads the build that is live unless you name one. A `database.updated` webhook names the build it announces and carries its `sha256`, so a receiver that downloads after the next publish asks for that build to get the file the hash is of:
121
+
122
+ ```python
123
+ client.database.download("vpn_ip_v1", "mmdb", "./vpn_ip_v1.mmdb", build=event["data"]["build"])
124
+ ```
125
+
126
+ A build is kept 7 days. Past that the call raises `bad_request` with the message `BUILD_GONE`, and downloading without `build` fetches the live one.
127
+
120
128
  Verify what you fetched against the published digests:
121
129
 
122
130
  ```python
@@ -83,6 +83,14 @@ written = client.database.download("bogon_ip_v1", "mmdb", "./bogon_ip_v1.mmdb")
83
83
 
84
84
  Either way a short transfer fails rather than handing you a truncated file, and the key is never sent to object storage.
85
85
 
86
+ Each of the three downloads the build that is live unless you name one. A `database.updated` webhook names the build it announces and carries its `sha256`, so a receiver that downloads after the next publish asks for that build to get the file the hash is of:
87
+
88
+ ```python
89
+ client.database.download("vpn_ip_v1", "mmdb", "./vpn_ip_v1.mmdb", build=event["data"]["build"])
90
+ ```
91
+
92
+ A build is kept 7 days. Past that the call raises `bad_request` with the message `BUILD_GONE`, and downloading without `build` fetches the live one.
93
+
86
94
  Verify what you fetched against the published digests:
87
95
 
88
96
  ```python
@@ -40,7 +40,7 @@ from .models import (
40
40
  TokenResponse,
41
41
  )
42
42
 
43
- __version__ = "2.9.5"
43
+ __version__ = "2.10.0"
44
44
 
45
45
  __all__ = [
46
46
  "DATABASE_FORMATS",
@@ -23,6 +23,7 @@ from urllib.parse import quote
23
23
  import httpx
24
24
 
25
25
  from ._generated.client import AuthenticatedClient, Client
26
+ from ._generated.types import UNSET, Unset
26
27
  from .errors import InternetDataError, error_from_response, oauth_error_from
27
28
  from .models import Database, Download, Pkce, to_database, to_download
28
29
 
@@ -158,6 +159,16 @@ def check_api_key(api_key: str | None) -> str | None:
158
159
  return key or None
159
160
 
160
161
 
162
+ def check_build(build: str | None) -> str | Unset:
163
+ """`build` as the download takes it: unset for None or an empty name, so a value read
164
+ from an unset setting downloads the live build."""
165
+ if build is None or build == "":
166
+ return UNSET
167
+ if not isinstance(build, str):
168
+ raise TypeError(f"build must be a build's name, a string, not {build!r}")
169
+ return build
170
+
171
+
161
172
  def check_timeout(timeout: float | None) -> float | None:
162
173
  """`timeout`, once it is a bound an attempt can meet.
163
174
 
@@ -35,6 +35,7 @@ from ._core import (
35
35
  authorization_url,
36
36
  build_async_transfer_client,
37
37
  build_client,
38
+ check_build,
38
39
  check_retries,
39
40
  check_timeout,
40
41
  checksums_of,
@@ -231,7 +232,12 @@ class AsyncDatabaseApi:
231
232
  return await self._retrying(call)
232
233
 
233
234
  async def download_url(
234
- self, database_id: str, format: Format, *, timeout: float | None = None
235
+ self,
236
+ database_id: str,
237
+ format: Format,
238
+ *,
239
+ build: str | None = None,
240
+ timeout: float | None = None,
235
241
  ) -> str:
236
242
  """The time-limited URL for one database file.
237
243
 
@@ -241,9 +247,15 @@ class AsyncDatabaseApi:
241
247
  gigabytes; the link authorizes the START of a transfer, so one already running is
242
248
  not interrupted when it lapses.
243
249
 
250
+ `build` names one build of the database, as a `database.updated` webhook gives it
251
+ (`20261105.0t53s5`), so the file is the one whose `sha256` the webhook carries.
252
+ None, the default, is the build that is live. A build is kept 7 days; past that
253
+ the call raises `bad_request` with the message `BUILD_GONE`.
254
+
244
255
  `timeout` bounds each attempt at MINTING the link, which is an ordinary API
245
256
  request, and says nothing about the transfer you then run with it.
246
257
  """
258
+ build_ = check_build(build)
247
259
 
248
260
  async def call() -> str:
249
261
  res = await request_async(
@@ -252,12 +264,20 @@ class AsyncDatabaseApi:
252
264
  self._bound(timeout),
253
265
  id=database_id,
254
266
  format_=DatabaseFormat(format),
267
+ build=build_,
255
268
  )
256
269
  return redirect_location(res)
257
270
 
258
271
  return await self._retrying(call)
259
272
 
260
- async def download(self, database_id: str, format: Format, path: str | os.PathLike[str]) -> int:
273
+ async def download(
274
+ self,
275
+ database_id: str,
276
+ format: Format,
277
+ path: str | os.PathLike[str],
278
+ *,
279
+ build: str | None = None,
280
+ ) -> int:
261
281
  """Download one database file to `path`, and return the bytes written.
262
282
 
263
283
  The bytes are streamed straight to disk, so nothing larger than a chunk is ever
@@ -274,7 +294,7 @@ class AsyncDatabaseApi:
274
294
  event loop would stall every other task in the process for the length of the
275
295
  transfer, which is the one thing an async caller cannot afford.
276
296
  """
277
- res = await self._open_transfer(database_id, format)
297
+ res = await self._open_transfer(database_id, format, build)
278
298
  try:
279
299
  loop = asyncio.get_running_loop()
280
300
  written = 0
@@ -288,7 +308,9 @@ class AsyncDatabaseApi:
288
308
  finally:
289
309
  await res.aclose()
290
310
 
291
- async def download_bytes(self, database_id: str, format: Format) -> bytes:
311
+ async def download_bytes(
312
+ self, database_id: str, format: Format, *, build: str | None = None
313
+ ) -> bytes:
292
314
  """Download one database file and hand back its bytes.
293
315
 
294
316
  **This holds the entire file in memory**, and the catalog spans seven orders of
@@ -296,7 +318,7 @@ class AsyncDatabaseApi:
296
318
  small end, where the bytes go straight into a parser, and use `download` for
297
319
  anything you have not checked `metadata` for.
298
320
  """
299
- res = await self._open_transfer(database_id, format)
321
+ res = await self._open_transfer(database_id, format, build)
300
322
  try:
301
323
  body = await res.aread()
302
324
  assert_whole_transfer(res, len(body))
@@ -311,8 +333,10 @@ class AsyncDatabaseApi:
311
333
  #
312
334
  # Returns the response with its body still unread, so the caller decides whether a
313
335
  # database is going to disk or into memory.
314
- async def _open_transfer(self, database_id: str, format: Format) -> httpx.Response:
315
- url = await self.download_url(database_id, format)
336
+ async def _open_transfer(
337
+ self, database_id: str, format: Format, build: str | None
338
+ ) -> httpx.Response:
339
+ url = await self.download_url(database_id, format, build=build)
316
340
  transfer = self._owner._transfer
317
341
 
318
342
  async def call() -> httpx.Response:
@@ -478,9 +502,17 @@ class AsyncOauthApi:
478
502
  while True:
479
503
  # No wait runs past the deadline: an interval ending after it waits only the
480
504
  # time left, and the expiry follows with nothing sent. In full, an interval of
481
- # 2147483647 held a poll with 2 s left for 68 years (2.6.0).
482
- await self._clock.sleep(min(interval, max(deadline - self._clock.now(), 0)))
483
- if self._clock.now() >= deadline:
505
+ # 2147483647 held a poll with 2 s left for 68 years (2.6.0). A remainder not
506
+ # above 0 is none left: a hand-built `expires_in` of NaN, which compares false
507
+ # with everything, polled every interval for good (2.9.5). `max(0, ...)` is 0
508
+ # for NaN only in that order; `max(nan, 0)` is NaN.
509
+ left = max(0, deadline - self._clock.now())
510
+ to_deadline = left <= interval
511
+ await self._clock.sleep(left if to_deadline else interval)
512
+ # A wait that ran to the deadline ends the poll without reading the clock again:
513
+ # a timer may wake before the clock has moved the whole wait, and the deadline
514
+ # would then read as not yet reached, so one more request would go out at it.
515
+ if to_deadline or self._clock.now() >= deadline:
484
516
  raise OauthExpiredTokenError()
485
517
  try:
486
518
  return await self.exchange_device_code(
@@ -30,6 +30,7 @@ from ._core import (
30
30
  authorization_url,
31
31
  build_client,
32
32
  build_transfer_client,
33
+ check_build,
33
34
  check_retries,
34
35
  check_timeout,
35
36
  checksums_of,
@@ -252,7 +253,12 @@ class DatabaseApi:
252
253
  return self._retrying(call)
253
254
 
254
255
  def download_url(
255
- self, database_id: str, format: Format, *, timeout: float | None = None
256
+ self,
257
+ database_id: str,
258
+ format: Format,
259
+ *,
260
+ build: str | None = None,
261
+ timeout: float | None = None,
256
262
  ) -> str:
257
263
  """The time-limited URL for one database file.
258
264
 
@@ -262,9 +268,15 @@ class DatabaseApi:
262
268
  gigabytes; the link authorizes the START of a transfer, so one already running is
263
269
  not interrupted when it lapses.
264
270
 
271
+ `build` names one build of the database, as a `database.updated` webhook gives it
272
+ (`20261105.0t53s5`), so the file is the one whose `sha256` the webhook carries.
273
+ None, the default, is the build that is live. A build is kept 7 days; past that
274
+ the call raises `bad_request` with the message `BUILD_GONE`.
275
+
265
276
  `timeout` bounds each attempt at MINTING the link, which is an ordinary API
266
277
  request, and says nothing about the transfer you then run with it.
267
278
  """
279
+ build_ = check_build(build)
268
280
 
269
281
  def call() -> str:
270
282
  res = request(
@@ -273,12 +285,20 @@ class DatabaseApi:
273
285
  self._bound(timeout),
274
286
  id=database_id,
275
287
  format_=DatabaseFormat(format),
288
+ build=build_,
276
289
  )
277
290
  return redirect_location(res)
278
291
 
279
292
  return self._retrying(call)
280
293
 
281
- def download(self, database_id: str, format: Format, path: str | os.PathLike[str]) -> int:
294
+ def download(
295
+ self,
296
+ database_id: str,
297
+ format: Format,
298
+ path: str | os.PathLike[str],
299
+ *,
300
+ build: str | None = None,
301
+ ) -> int:
282
302
  """Download one database file to `path`, and return the bytes written.
283
303
 
284
304
  The bytes are streamed straight to disk, so nothing larger than a chunk is ever
@@ -291,7 +311,7 @@ class DatabaseApi:
291
311
  `OSError`, rather than as this library's error type: a reset socket and a full
292
312
  disk are different problems, and only one of them is ours.
293
313
  """
294
- res = self._open_transfer(database_id, format)
314
+ res = self._open_transfer(database_id, format, build)
295
315
  try:
296
316
  written = 0
297
317
  with part_file(path) as sink:
@@ -304,7 +324,9 @@ class DatabaseApi:
304
324
  finally:
305
325
  res.close()
306
326
 
307
- def download_bytes(self, database_id: str, format: Format) -> bytes:
327
+ def download_bytes(
328
+ self, database_id: str, format: Format, *, build: str | None = None
329
+ ) -> bytes:
308
330
  """Download one database file and hand back its bytes.
309
331
 
310
332
  **This holds the entire file in memory**, and the catalog spans seven orders of
@@ -312,7 +334,7 @@ class DatabaseApi:
312
334
  small end, where the bytes go straight into a parser, and use `download` for
313
335
  anything you have not checked `metadata` for.
314
336
  """
315
- res = self._open_transfer(database_id, format)
337
+ res = self._open_transfer(database_id, format, build)
316
338
  try:
317
339
  body = res.read()
318
340
  assert_whole_transfer(res, len(body))
@@ -327,8 +349,8 @@ class DatabaseApi:
327
349
  #
328
350
  # Returns the response with its body still unread, so the caller decides whether a
329
351
  # database is going to disk or into memory.
330
- def _open_transfer(self, database_id: str, format: Format) -> httpx.Response:
331
- url = self.download_url(database_id, format)
352
+ def _open_transfer(self, database_id: str, format: Format, build: str | None) -> httpx.Response:
353
+ url = self.download_url(database_id, format, build=build)
332
354
  transfer = self._owner._transfer
333
355
 
334
356
  def call() -> httpx.Response:
@@ -537,9 +559,17 @@ class OauthApi:
537
559
  while True:
538
560
  # No wait runs past the deadline: an interval ending after it waits only the
539
561
  # time left, and the expiry follows with nothing sent. In full, an interval of
540
- # 2147483647 held a poll with 2 s left for 68 years (2.6.0).
541
- self._clock.sleep(min(interval, max(deadline - self._clock.now(), 0)))
542
- if self._clock.now() >= deadline:
562
+ # 2147483647 held a poll with 2 s left for 68 years (2.6.0). A remainder not
563
+ # above 0 is none left: a hand-built `expires_in` of NaN, which compares false
564
+ # with everything, polled every interval for good (2.9.5). `max(0, ...)` is 0
565
+ # for NaN only in that order; `max(nan, 0)` is NaN.
566
+ left = max(0, deadline - self._clock.now())
567
+ to_deadline = left <= interval
568
+ self._clock.sleep(left if to_deadline else interval)
569
+ # A wait that ran to the deadline ends the poll without reading the clock again:
570
+ # a timer may wake before the clock has moved the whole wait, and the deadline
571
+ # would then read as not yet reached, so one more request would go out at it.
572
+ if to_deadline or self._clock.now() >= deadline:
543
573
  raise OauthExpiredTokenError()
544
574
  try:
545
575
  return self.exchange_device_code(client_id, device.device_code, timeout=timeout)
@@ -222,11 +222,14 @@ class FakeClock(Clock):
222
222
  """A poll's sleep and monotonic clock, replaced together, recording every wait in
223
223
  seconds. Past LOOP_BOUND waits, or twice that many clock reads, it trips `bound`."""
224
224
 
225
- def __init__(self, bound: LoopBound) -> None:
225
+ def __init__(self, bound: LoopBound, short: float = 0.0) -> None:
226
226
  self.waits: list[float] = []
227
227
  self._elapsed = 0.0
228
228
  self._reads = 0
229
229
  self._bound = bound
230
+ # Each wait of at least 1 ms wakes this many seconds before the clock has moved
231
+ # its delay, as a runtime timer may.
232
+ self._short = short
230
233
 
231
234
  def now(self) -> float:
232
235
  self._reads += 1
@@ -238,7 +241,7 @@ class FakeClock(Clock):
238
241
  if len(self.waits) == LOOP_BOUND:
239
242
  self._bound.trip(f"waited more than {LOOP_BOUND} times")
240
243
  self.waits.append(seconds)
241
- self._elapsed += seconds
244
+ self._elapsed += seconds - self._short if seconds >= 0.001 else seconds
242
245
 
243
246
 
244
247
  class AsyncFakeClock(AsyncClock):
@@ -179,6 +179,56 @@ def test_download_url_hands_back_the_link_without_fetching_it(
179
179
  assert served.paths() == [DOWNLOAD_PATH], "the link was fetched as well as returned"
180
180
 
181
181
 
182
+ def test_a_build_goes_on_the_download_request_and_none_without_one(
183
+ make_client: ClientFactory, tmp_path: Path
184
+ ) -> None:
185
+ served = origin(make_client)
186
+ database = served.client.database
187
+
188
+ database.download_url("bogon_ip_v1", "csvgz", build="20261105.0t53s5")
189
+ database.download_bytes("bogon_ip_v1", "csvgz", build="20261105.0t53s5")
190
+ database.download("bogon_ip_v1", "csvgz", tmp_path / "built.csv.gz", build="20261105.0t53s5")
191
+ database.download_url("bogon_ip_v1", "csvgz", build="")
192
+ database.download_bytes("bogon_ip_v1", "csvgz")
193
+
194
+ named = {"id": "bogon_ip_v1", "format": "csvgz", "build": "20261105.0t53s5"}
195
+ live = {"id": "bogon_ip_v1", "format": "csvgz"}
196
+ asked = [dict(r.url.params) for r in served.requests if r.url.path == DOWNLOAD_PATH]
197
+ assert asked == [named, named, named, live, live]
198
+
199
+
200
+ def test_a_build_that_is_no_string_is_refused_with_nothing_sent(
201
+ make_client: ClientFactory, tmp_path: Path
202
+ ) -> None:
203
+ served = origin(make_client)
204
+ database = served.client.database
205
+
206
+ with pytest.raises(TypeError, match="build"):
207
+ database.download_url("bogon_ip_v1", "csvgz", build=20261105)
208
+ with pytest.raises(TypeError, match="build"):
209
+ database.download_bytes("bogon_ip_v1", "csvgz", build=b"20261105.0t53s5")
210
+ with pytest.raises(TypeError, match="build"):
211
+ database.download("bogon_ip_v1", "csvgz", tmp_path / "x", build=["20261105.0t53s5"])
212
+ assert served.requests == []
213
+
214
+
215
+ def test_a_gone_build_is_a_bad_request_asked_once(make_client: ClientFactory) -> None:
216
+ seen: list[httpx.Request] = []
217
+
218
+ def gone(request: httpx.Request) -> httpx.Response:
219
+ seen.append(request)
220
+ return httpx.Response(410, json={"rc": "BUILD_GONE"})
221
+
222
+ client = make_client(api_key=API_KEY, base_url=API, transport=httpx.MockTransport(gone))
223
+
224
+ with pytest.raises(InternetDataError) as caught:
225
+ client.database.download_url("bogon_ip_v1", "csvgz", build="20261029.aaaaaa")
226
+ assert caught.value.kind == "bad_request"
227
+ assert caught.value.retryable is False
228
+ assert caught.value.message == "BUILD_GONE"
229
+ assert len(seen) == 1
230
+
231
+
182
232
  # The link is the whole answer for this method, and it is only useful because it carries
183
233
  # no credential of ours: it authorizes itself, so it can be handed to anything that speaks
184
234
  # HTTP without also handing over the key.
@@ -9,6 +9,7 @@ from __future__ import annotations
9
9
 
10
10
  import asyncio
11
11
  import json
12
+ import math
12
13
  import re
13
14
  import time
14
15
  import urllib.parse
@@ -389,6 +390,49 @@ def test_an_expires_in_past_any_float_still_polls(make_client: ClientFactory) ->
389
390
  assert clock.waits == [1]
390
391
 
391
392
 
393
+ @pytest.mark.parametrize("interval", [5, 3])
394
+ def test_a_wait_that_ran_to_the_deadline_ends_the_poll_however_early_the_timer_woke(
395
+ make_client: ClientFactory, interval: int
396
+ ) -> None:
397
+ """A timer that wakes before the clock has moved its delay leaves the deadline reading as
398
+ not yet reached, so a poll that read the clock again would ask once more at it. A wait
399
+ cut to the deadline, or ending at it, ends the poll."""
400
+ bound = LoopBound()
401
+ stub = OauthStub([{"status": 400, "body": {"error": "authorization_pending"}}], bound)
402
+ client = make_client(base_url=BASE_URL, transport=stub.transport)
403
+ clock = FakeClock(bound, short=0.0005)
404
+ client.oauth.use_clock(clock)
405
+ device = DeviceAuthorization(**{**_device_fields(), "expires_in": 3, "interval": interval})
406
+
407
+ outcome = settle(lambda: client.oauth.poll_device_token("internetdata-cli", device), bound)
408
+
409
+ assert isinstance(outcome, OauthExpiredTokenError), f"settled with {outcome!r}"
410
+ assert outcome.status is None, "the local deadline, not the server's"
411
+ assert stub.requests == []
412
+ assert clock.waits == [3]
413
+
414
+
415
+ @pytest.mark.parametrize("expires_in", [math.nan, -math.nan, math.inf - math.inf])
416
+ def test_a_lifetime_that_is_not_a_number_ends_the_poll_at_once(
417
+ make_client: ClientFactory, expires_in: float
418
+ ) -> None:
419
+ """A hand-built device can carry NaN, which compares false with everything, so the
420
+ deadline was never reached and the poll asked every interval for good."""
421
+ bound = LoopBound()
422
+ stub = OauthStub([{"status": 400, "body": {"error": "authorization_pending"}}], bound)
423
+ client = make_client(base_url=BASE_URL, transport=stub.transport)
424
+ clock = FakeClock(bound)
425
+ client.oauth.use_clock(clock)
426
+ device = DeviceAuthorization(**{**_device_fields(), "expires_in": expires_in, "interval": 1})
427
+
428
+ outcome = settle(lambda: client.oauth.poll_device_token("internetdata-cli", device), bound)
429
+
430
+ assert isinstance(outcome, OauthExpiredTokenError), f"settled with {outcome!r}"
431
+ assert outcome.status is None, "the local deadline, not the server's"
432
+ assert stub.requests == []
433
+ assert clock.waits == [0]
434
+
435
+
392
436
  def _device_fields() -> dict[str, Any]:
393
437
  return {
394
438
  "device_code": "mo_dc_x",
File without changes
File without changes