xtr-http-client 6.8.0__tar.gz → 7.0.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 (59) hide show
  1. xtr_http_client-6.8.0/README.md → xtr_http_client-7.0.0/PKG-INFO +95 -13
  2. xtr_http_client-6.8.0/PKG-INFO → xtr_http_client-7.0.0/README.md +68 -44
  3. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/pyproject.toml +22 -25
  4. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/pyproject.toml.orig +25 -27
  5. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/.agents/skills/xtr-http-client/SKILL.md +12 -2
  6. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/_clients.py +34 -5
  7. xtr_http_client-7.0.0/src/xtr_http_client/bundle/_profiler.py +29 -0
  8. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/http_client_bundle.py +27 -4
  9. xtr_http_client-7.0.0/src/xtr_http_client/data_collector/__init__.py +12 -0
  10. xtr_http_client-7.0.0/src/xtr_http_client/data_collector/_masking.py +51 -0
  11. xtr_http_client-7.0.0/src/xtr_http_client/data_collector/http_client_data_collector.py +151 -0
  12. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/http_client.py +1 -3
  13. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/http_client_mixin.py +1 -2
  14. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/httpx_http_client.py +13 -32
  15. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/httpx_body.py +5 -11
  16. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/httpx_clients.py +26 -31
  17. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/httpx_request.py +3 -5
  18. xtr_http_client-7.0.0/src/xtr_http_client/internal/reachable_url.py +31 -0
  19. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/retry_count_response.py +1 -2
  20. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/retry_delay.py +2 -2
  21. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/mock_http_client.py +1 -2
  22. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/no_private_network_http_client.py +106 -35
  23. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/response/http_response.py +1 -2
  24. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/response/httpx_response.py +2 -2
  25. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/response/mock_response.py +1 -2
  26. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/retry/generic_retry_strategy.py +26 -4
  27. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/retryable_http_client.py +5 -8
  28. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/scoping_http_client.py +1 -2
  29. xtr_http_client-7.0.0/src/xtr_http_client/templates/collector/http_client.html.j2 +226 -0
  30. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/throttling_http_client.py +6 -4
  31. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/traceable_http_client.py +50 -7
  32. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/LICENSE +0 -0
  33. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/__init__.py +0 -0
  34. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/__init__.py +0 -0
  35. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/client_options_config.py +0 -0
  36. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/http_client_config.py +0 -0
  37. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/retry_failed_config.py +0 -0
  38. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/bundle/scoped_client_config.py +0 -0
  39. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/exception/__init__.py +0 -0
  40. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/exception/_http_error_message.py +0 -0
  41. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/exception/logic_error.py +0 -0
  42. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/__init__.py +0 -0
  43. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/debug_type.py +0 -0
  44. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/httpx_errors.py +0 -0
  45. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/httpx_redirect.py +0 -0
  46. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/mock_head.py +0 -0
  47. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/mock_info.py +0 -0
  48. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/option_merge.py +0 -0
  49. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/response_body.py +0 -0
  50. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/internal/retry_options.py +0 -0
  51. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/py.typed +0 -0
  52. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/recipe/__init__.py +0 -0
  53. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/recipe/files/config/http_client.py.tmpl +0 -0
  54. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/recipe/manifest.toml +0 -0
  55. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/response/__init__.py +0 -0
  56. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/response/json_mock_response.py +0 -0
  57. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/retry/__init__.py +0 -0
  58. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/retry/retry_context.py +0 -0
  59. {xtr_http_client-6.8.0 → xtr_http_client-7.0.0}/src/xtr_http_client/retry/retry_strategy_interface.py +0 -0
@@ -1,10 +1,37 @@
1
+ Metadata-Version: 2.4
2
+ Name: xtr-http-client
3
+ Version: 7.0.0
4
+ Summary: An async HTTP client behind one interface: typed request options, bodies read on demand, scoped clients, a private-network guard and a mock client for tests.
5
+ Keywords: http,http-client,request,response,mock,asyncio
6
+ Author: Xterr
7
+ Author-email: Xterr <me@xterr.dev>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Framework :: AsyncIO
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Topic :: Internet :: WWW/HTTP
15
+ Classifier: Typing :: Typed
16
+ Requires-Dist: httpx>=0.27
17
+ Requires-Dist: idna>=3.11
18
+ Requires-Dist: xtr-http-client-contracts>=7.0,<8
19
+ Requires-Dist: xtr-http-message>=7.0,<8
20
+ Requires-Dist: xtr-http-message-contracts>=7.0,<8
21
+ Requires-Dist: xtr-logging-contracts>=7.0,<8
22
+ Requires-Dist: xtr-service-contracts>=7.0,<8
23
+ Requires-Dist: httpx[http2]>=0.27 ; extra == 'http2'
24
+ Requires-Python: >=3.14.4
25
+ Provides-Extra: http2
26
+ Description-Content-Type: text/markdown
27
+
1
28
  <div align="center">
2
29
 
3
30
  # xtr-http-client
4
31
 
5
32
  **An async HTTP client behind one interface: typed options in, a response read on demand out.**
6
33
 
7
- <img alt="python 3.11+" src="https://img.shields.io/badge/python-%E2%89%A5%203.11-3776AB?logo=python&logoColor=white">
34
+ <img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
8
35
  <img alt="asyncio" src="https://img.shields.io/badge/asyncio-native-1f6feb">
9
36
  <img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
10
37
  <img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
@@ -34,7 +61,7 @@ uv add xtr-rate-limiter # beside it: the limiter the t
34
61
  uv add xtr-http-foundation # beside it: the networks the private-network guard refuses
35
62
  ```
36
63
 
37
- Requires Python 3.11+. `http2` is the only extra, and the container is not a dependency: the
64
+ Requires Python 3.14.4+. `http2` is the only extra, and the container is not a dependency: the
38
65
  bundle is used when [xtr-dependency-injection](../xtr-dependency-injection) is installed beside the
39
66
  package, never as an extra, and every client works without it, built by hand as below. Installed
40
67
  beside it too, [xtr-rate-limiter](../xtr-rate-limiter) supplies the limiter `ThrottlingHttpClient`
@@ -65,6 +92,9 @@ Three things in that are worth saying out loud.
65
92
  the message stream over that same kept body.
66
93
  - **A response holds a connection until it is done with.** Leaving the `async with` block
67
94
  closes the body, which frees the connection; `await response.cancel()` does it mid-transfer.
95
+ A response that is neither read to the end nor closed is never freed for you — closing needs
96
+ an `await` no finalizer can make — so it keeps its connection, and enough of them exhaust the
97
+ pool. Always use the `async with`, or read the body.
68
98
 
69
99
  `get_content()` and `to_array()` are where an unsuccessful status *is* raised —
70
100
  `RedirectionError`, `ClientError`, `ServerError`. Pass `throw=False` to read the body a 4xx
@@ -111,8 +141,8 @@ the rest. An option this client does not know is refused with `InvalidArgumentEr
111
141
  the closest one it does, because an option spelled wrong that was quietly dropped is a setting
112
142
  that silently never applied.
113
143
 
114
- A host `resolve` pins to an address stays pinned for the requests after it; `reset()` forgets
115
- every pin and keeps the connections open.
144
+ A host `resolve` pins to an address is pinned for that request only; the next request asks a
145
+ resolver again unless it pins the host too. `reset()` keeps the connections open.
116
146
 
117
147
  A `body` mapping holding a file opened `"rb"` is uploaded as `multipart/form-data` — plain
118
148
  values as `data`, each file as a part named after its file and typed from that name — and a
@@ -164,8 +194,14 @@ async with HttpxHttpClient({"timeout": 5}, max_host_connections=4) as client:
164
194
  - Redirects are followed here rather than by the backend, for the one thing a backend cannot be
165
195
  told: an `Authorization` header belongs to the host it was meant for, so it is dropped the
166
196
  moment a chain leaves that host and put back only if the chain returns to it.
197
+ Only that header is: a custom credential header — `X-Api-Key`, say — follows a redirect to
198
+ another host, here and through `NoPrivateNetworkHttpClient` alike. Send such a request with
199
+ `max_redirects=0`, or put the credential in `Authorization`.
167
200
  - `aclose()` — or leaving the `async with` — closes every pool. Nothing is opened until a
168
201
  request is made.
202
+ - Each event loop gets pools of its own, since a connection cannot move between loops: one
203
+ client can serve several `asyncio.run` calls, or threads each running a loop. `aclose()`
204
+ closes the pools of the loop it runs on; another loop's pools go with that loop.
169
205
 
170
206
  What it refuses, before anything is sent: an empty method or one that is not uppercase ASCII
171
207
  letters; `auth_basic` and `auth_bearer` together, or either in a shape credentials cannot be
@@ -206,10 +242,13 @@ client = RetryableHttpClient(inner, GenericRetryStrategy([500, 502, 503]), max_r
206
242
  ```
207
243
 
208
244
  `max_retries` is how many *further* attempts are allowed, so the request is made at most that
209
- many times plus one; a request may lower or raise it with the `max_retries` option. A
210
- `Retry-After` header wins over whatever delay the strategy names — a server saying when to come
211
- back is better informed than a client guessing. The response reports how many tries it took,
212
- under the `retry_count` entry of `get_info()`.
245
+ many times plus one; a request may lower or raise it with the `max_retries` option. The
246
+ strategy names the delay, from the answer it is handed: `GenericRetryStrategy` obeys a
247
+ `Retry-After` header over its own backoff — a server saying when to come back is better
248
+ informed than a client guessing — up to `max_delay_ms`, or up to `max_retry_after_ms` (a minute)
249
+ when that sets no ceiling, so an answer cannot park the caller for as long as it likes. A
250
+ strategy of your own reads `context.response` to do the same. The response reports how many
251
+ tries it took, under the `retry_count` entry of `get_info()`.
213
252
 
214
253
  A body that cannot be produced twice is sent once: a second attempt would send a request that
215
254
  is not the one that failed. And when `base_uri` names several, each attempt moves one along
@@ -244,7 +283,13 @@ moment that booking is due; booking first and waiting after is what makes the pa
244
283
  concurrency, since requests then leave in the order they were asked for rather than all at
245
284
  once. Which requests share a pace is decided by which of them are given the same limiter — one
246
285
  per host, one per account, one for the whole application — and a copy made by `with_options()`
247
- shares it.
286
+ shares it. `reset()` keeps the pace: the end of one unit of work forgets nothing counted
287
+ against a rate every unit shares.
288
+
289
+ A booking is never handed back: a request cancelled or timed out while it waits still counts
290
+ against the pace, and later requests queue behind its turn. Bookings are not bounded either;
291
+ under a burst, callers queue as far ahead as the limiter's rate dictates, so bound the wait
292
+ itself (a timeout around the call) where waiting long is worse than failing.
248
293
 
249
294
  ### `TraceableHttpClient` — a record of every request made
250
295
 
@@ -267,7 +312,10 @@ reads the body. So a trace read mid-request says how far it has got rather than
267
312
  The bodies are the costly half of a record and the half most likely to carry a credential, so
268
313
  `extra={"trace_content": False}` leaves them out: `body` and `json` are dropped from `options`
269
314
  and `content` is `False` rather than bytes. Pass `disabled=lambda: ...` to record nothing while
270
- it says so. There is one record per client and its copies; `reset()` clears it.
315
+ it says so. `auth_basic`, `auth_bearer` and an `Authorization`, `Cookie` or
316
+ `Proxy-Authorization` header are always recorded as `"***"`. There is one record per client and
317
+ its copies, keeping the last `TraceableHttpClient.MAX_TRACES` (1000) requests; `reset()` clears
318
+ it.
271
319
 
272
320
  ### `NoPrivateNetworkHttpClient` — refuse what leads inwards
273
321
 
@@ -285,8 +333,18 @@ The check runs on what the host *resolves to*, not on how it is written, and one
285
333
  address refuses the whole request — whichever of them the connection would pick, nothing is
286
334
  sent. The request is then pinned to the address that was checked, through the `resolve` option,
287
335
  so a name whose records change between the check and the connection reaches what was vetted or
288
- nothing at all. Redirects are followed here too, because a hop nobody checked would walk
289
- straight past the guard.
336
+ nothing at all. A host named in another script — `https://münchen.de/` — is checked and pinned
337
+ in the `xn--` form it is reached by. Redirects are followed here too, because a hop nobody
338
+ checked would walk straight past the guard.
339
+
340
+ A redirect to another origin — another scheme, host or port — carries no credentials meant for
341
+ the first: the `Authorization`, `Cookie` and `Proxy-Authorization` headers, the `auth_bearer`
342
+ and `auth_basic` options and the `query` parameters (an API key travels there) are left behind.
343
+ The hop sends the request's other headers and an empty `query`, and never the wrapped client's
344
+ own default headers or query. A custom credential header — `X-Api-Key` — is one of the other
345
+ headers and goes on: send it with `max_redirects=0`. A body a 307 or 308 keeps goes on, as the status asks. An
346
+ address the checks cannot read is refused; one with a zone, `fe80::1%eth0`, is checked as the
347
+ address it names.
290
348
 
291
349
  A refusal is `TransportError`: `Host "<host>" is blocked for "<url>".`, or `IP "<ip>" is
292
350
  blocked for "<url>".` for an address written out. With no `subnets` it refuses
@@ -294,7 +352,8 @@ blocked for "<url>".` for an address written out. With no `subnets` it refuses
294
352
  private, link-local, carrier-grade NAT, unique-local, multicast, reserved and unspecified
295
353
  alike. `subnets` replaces that set and `allow_list` opens one receiver without opening the
296
354
  network it sits in; both take addresses or CIDR networks, never host names. What a host
297
- resolved to is remembered for the client's life, and `reset()` forgets it.
355
+ resolved to is remembered for one request's chain of redirects, so hops back to the same host
356
+ ask the resolver once; the next request asks again.
298
357
 
299
358
  xtr-http-foundation is not a dependency: install it beside this package (`uv add
300
359
  xtr-http-foundation`) to use the guard. Without it, building `NoPrivateNetworkHttpClient`
@@ -356,6 +415,24 @@ client's, filled in when the client issues it. Reading one no client issued rais
356
415
  `InvalidArgumentError`, and `get_request_method()`, `get_request_url()` and
357
416
  `get_request_options()` are what a test asserts the issued half on.
358
417
 
418
+ ## Profiler panel
419
+
420
+ With [xtr-http-kernel](../xtr-http-kernel)'s profiler on — the web profiler turns it on in
421
+ development — the bundle registers an `http_client` panel, `HttpClientDataCollector`, and traces
422
+ every client it builds into it. For each outgoing request a profiled request made, the panel shows
423
+ the client service it went through (`http_client`, or `http_client_<name>` for a scoped client),
424
+ the method, the URL, the status, how long the answer took to arrive, the error when none did, the
425
+ request's headers and body, the response's headers and the size of the body read — with the
426
+ request count, the failures (an error, or a 4xx or 5xx) and the total time on top. Credentials —
427
+ `Authorization`, `Proxy-Authorization`, cookies, `X-Api-Key`, and any header or body field named
428
+ like a password, a secret, a token or an API key — are kept as `******`, and a request made with
429
+ `extra={"trace_content": False}` keeps no bodies.
430
+
431
+ The records belong to the request being profiled, so requests served at once never mix. A client
432
+ that is traced only for the panel (`tracing` left off) records nothing outside a profiled request.
433
+ With the profiler off, nothing is registered and the clients are built exactly as configured:
434
+ the panel costs nothing.
435
+
359
436
  ## Use in an application
360
437
 
361
438
  Everything adding this package to an application on
@@ -488,6 +565,11 @@ The hierarchy is the [contract's](../xtr-http-client-contracts#errors), re-expor
488
565
  An unsuccessful status names itself: `HTTP 404 returned for "<url>".`, the status line the
489
566
  server sent, or the `title` and `detail` of the body when that body is a JSON error document.
490
567
 
568
+ ## Upgrading
569
+
570
+ What changes for an application moving to 7.0, and how to keep the old behaviour where that is
571
+ still possible: [Upgrading to 7.0](../../UPGRADING.md#xtr-http-client).
572
+
491
573
  ## Development
492
574
 
493
575
  Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
@@ -1,41 +1,10 @@
1
- Metadata-Version: 2.4
2
- Name: xtr-http-client
3
- Version: 6.8.0
4
- Summary: An async HTTP client behind one interface: typed request options, bodies read on demand, scoped clients, a private-network guard and a mock client for tests.
5
- Keywords: http,http-client,request,response,mock,asyncio
6
- Author: Xterr
7
- Author-email: Xterr <me@xterr.dev>
8
- License-Expression: MIT
9
- License-File: LICENSE
10
- Classifier: Development Status :: 3 - Alpha
11
- Classifier: Framework :: AsyncIO
12
- Classifier: Intended Audience :: Developers
13
- Classifier: Programming Language :: Python :: 3.11
14
- Classifier: Programming Language :: Python :: 3.12
15
- Classifier: Programming Language :: Python :: 3.13
16
- Classifier: Programming Language :: Python :: 3.14
17
- Classifier: Topic :: Internet :: WWW/HTTP
18
- Classifier: Typing :: Typed
19
- Requires-Dist: typing-extensions>=4.4
20
- Requires-Dist: anyio>=4.0
21
- Requires-Dist: httpx>=0.27
22
- Requires-Dist: xtr-http-client-contracts>=6.0,<7
23
- Requires-Dist: xtr-http-message>=6.0,<7
24
- Requires-Dist: xtr-http-message-contracts>=6.0,<7
25
- Requires-Dist: xtr-logging-contracts>=6.0,<7
26
- Requires-Dist: xtr-service-contracts>=6.0,<7
27
- Requires-Dist: httpx[http2]>=0.27 ; extra == 'http2'
28
- Requires-Python: >=3.11
29
- Provides-Extra: http2
30
- Description-Content-Type: text/markdown
31
-
32
1
  <div align="center">
33
2
 
34
3
  # xtr-http-client
35
4
 
36
5
  **An async HTTP client behind one interface: typed options in, a response read on demand out.**
37
6
 
38
- <img alt="python 3.11+" src="https://img.shields.io/badge/python-%E2%89%A5%203.11-3776AB?logo=python&logoColor=white">
7
+ <img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
39
8
  <img alt="asyncio" src="https://img.shields.io/badge/asyncio-native-1f6feb">
40
9
  <img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
41
10
  <img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
@@ -65,7 +34,7 @@ uv add xtr-rate-limiter # beside it: the limiter the t
65
34
  uv add xtr-http-foundation # beside it: the networks the private-network guard refuses
66
35
  ```
67
36
 
68
- Requires Python 3.11+. `http2` is the only extra, and the container is not a dependency: the
37
+ Requires Python 3.14.4+. `http2` is the only extra, and the container is not a dependency: the
69
38
  bundle is used when [xtr-dependency-injection](../xtr-dependency-injection) is installed beside the
70
39
  package, never as an extra, and every client works without it, built by hand as below. Installed
71
40
  beside it too, [xtr-rate-limiter](../xtr-rate-limiter) supplies the limiter `ThrottlingHttpClient`
@@ -96,6 +65,9 @@ Three things in that are worth saying out loud.
96
65
  the message stream over that same kept body.
97
66
  - **A response holds a connection until it is done with.** Leaving the `async with` block
98
67
  closes the body, which frees the connection; `await response.cancel()` does it mid-transfer.
68
+ A response that is neither read to the end nor closed is never freed for you — closing needs
69
+ an `await` no finalizer can make — so it keeps its connection, and enough of them exhaust the
70
+ pool. Always use the `async with`, or read the body.
99
71
 
100
72
  `get_content()` and `to_array()` are where an unsuccessful status *is* raised —
101
73
  `RedirectionError`, `ClientError`, `ServerError`. Pass `throw=False` to read the body a 4xx
@@ -142,8 +114,8 @@ the rest. An option this client does not know is refused with `InvalidArgumentEr
142
114
  the closest one it does, because an option spelled wrong that was quietly dropped is a setting
143
115
  that silently never applied.
144
116
 
145
- A host `resolve` pins to an address stays pinned for the requests after it; `reset()` forgets
146
- every pin and keeps the connections open.
117
+ A host `resolve` pins to an address is pinned for that request only; the next request asks a
118
+ resolver again unless it pins the host too. `reset()` keeps the connections open.
147
119
 
148
120
  A `body` mapping holding a file opened `"rb"` is uploaded as `multipart/form-data` — plain
149
121
  values as `data`, each file as a part named after its file and typed from that name — and a
@@ -195,8 +167,14 @@ async with HttpxHttpClient({"timeout": 5}, max_host_connections=4) as client:
195
167
  - Redirects are followed here rather than by the backend, for the one thing a backend cannot be
196
168
  told: an `Authorization` header belongs to the host it was meant for, so it is dropped the
197
169
  moment a chain leaves that host and put back only if the chain returns to it.
170
+ Only that header is: a custom credential header — `X-Api-Key`, say — follows a redirect to
171
+ another host, here and through `NoPrivateNetworkHttpClient` alike. Send such a request with
172
+ `max_redirects=0`, or put the credential in `Authorization`.
198
173
  - `aclose()` — or leaving the `async with` — closes every pool. Nothing is opened until a
199
174
  request is made.
175
+ - Each event loop gets pools of its own, since a connection cannot move between loops: one
176
+ client can serve several `asyncio.run` calls, or threads each running a loop. `aclose()`
177
+ closes the pools of the loop it runs on; another loop's pools go with that loop.
200
178
 
201
179
  What it refuses, before anything is sent: an empty method or one that is not uppercase ASCII
202
180
  letters; `auth_basic` and `auth_bearer` together, or either in a shape credentials cannot be
@@ -237,10 +215,13 @@ client = RetryableHttpClient(inner, GenericRetryStrategy([500, 502, 503]), max_r
237
215
  ```
238
216
 
239
217
  `max_retries` is how many *further* attempts are allowed, so the request is made at most that
240
- many times plus one; a request may lower or raise it with the `max_retries` option. A
241
- `Retry-After` header wins over whatever delay the strategy names — a server saying when to come
242
- back is better informed than a client guessing. The response reports how many tries it took,
243
- under the `retry_count` entry of `get_info()`.
218
+ many times plus one; a request may lower or raise it with the `max_retries` option. The
219
+ strategy names the delay, from the answer it is handed: `GenericRetryStrategy` obeys a
220
+ `Retry-After` header over its own backoff — a server saying when to come back is better
221
+ informed than a client guessing — up to `max_delay_ms`, or up to `max_retry_after_ms` (a minute)
222
+ when that sets no ceiling, so an answer cannot park the caller for as long as it likes. A
223
+ strategy of your own reads `context.response` to do the same. The response reports how many
224
+ tries it took, under the `retry_count` entry of `get_info()`.
244
225
 
245
226
  A body that cannot be produced twice is sent once: a second attempt would send a request that
246
227
  is not the one that failed. And when `base_uri` names several, each attempt moves one along
@@ -275,7 +256,13 @@ moment that booking is due; booking first and waiting after is what makes the pa
275
256
  concurrency, since requests then leave in the order they were asked for rather than all at
276
257
  once. Which requests share a pace is decided by which of them are given the same limiter — one
277
258
  per host, one per account, one for the whole application — and a copy made by `with_options()`
278
- shares it.
259
+ shares it. `reset()` keeps the pace: the end of one unit of work forgets nothing counted
260
+ against a rate every unit shares.
261
+
262
+ A booking is never handed back: a request cancelled or timed out while it waits still counts
263
+ against the pace, and later requests queue behind its turn. Bookings are not bounded either;
264
+ under a burst, callers queue as far ahead as the limiter's rate dictates, so bound the wait
265
+ itself (a timeout around the call) where waiting long is worse than failing.
279
266
 
280
267
  ### `TraceableHttpClient` — a record of every request made
281
268
 
@@ -298,7 +285,10 @@ reads the body. So a trace read mid-request says how far it has got rather than
298
285
  The bodies are the costly half of a record and the half most likely to carry a credential, so
299
286
  `extra={"trace_content": False}` leaves them out: `body` and `json` are dropped from `options`
300
287
  and `content` is `False` rather than bytes. Pass `disabled=lambda: ...` to record nothing while
301
- it says so. There is one record per client and its copies; `reset()` clears it.
288
+ it says so. `auth_basic`, `auth_bearer` and an `Authorization`, `Cookie` or
289
+ `Proxy-Authorization` header are always recorded as `"***"`. There is one record per client and
290
+ its copies, keeping the last `TraceableHttpClient.MAX_TRACES` (1000) requests; `reset()` clears
291
+ it.
302
292
 
303
293
  ### `NoPrivateNetworkHttpClient` — refuse what leads inwards
304
294
 
@@ -316,8 +306,18 @@ The check runs on what the host *resolves to*, not on how it is written, and one
316
306
  address refuses the whole request — whichever of them the connection would pick, nothing is
317
307
  sent. The request is then pinned to the address that was checked, through the `resolve` option,
318
308
  so a name whose records change between the check and the connection reaches what was vetted or
319
- nothing at all. Redirects are followed here too, because a hop nobody checked would walk
320
- straight past the guard.
309
+ nothing at all. A host named in another script — `https://münchen.de/` — is checked and pinned
310
+ in the `xn--` form it is reached by. Redirects are followed here too, because a hop nobody
311
+ checked would walk straight past the guard.
312
+
313
+ A redirect to another origin — another scheme, host or port — carries no credentials meant for
314
+ the first: the `Authorization`, `Cookie` and `Proxy-Authorization` headers, the `auth_bearer`
315
+ and `auth_basic` options and the `query` parameters (an API key travels there) are left behind.
316
+ The hop sends the request's other headers and an empty `query`, and never the wrapped client's
317
+ own default headers or query. A custom credential header — `X-Api-Key` — is one of the other
318
+ headers and goes on: send it with `max_redirects=0`. A body a 307 or 308 keeps goes on, as the status asks. An
319
+ address the checks cannot read is refused; one with a zone, `fe80::1%eth0`, is checked as the
320
+ address it names.
321
321
 
322
322
  A refusal is `TransportError`: `Host "<host>" is blocked for "<url>".`, or `IP "<ip>" is
323
323
  blocked for "<url>".` for an address written out. With no `subnets` it refuses
@@ -325,7 +325,8 @@ blocked for "<url>".` for an address written out. With no `subnets` it refuses
325
325
  private, link-local, carrier-grade NAT, unique-local, multicast, reserved and unspecified
326
326
  alike. `subnets` replaces that set and `allow_list` opens one receiver without opening the
327
327
  network it sits in; both take addresses or CIDR networks, never host names. What a host
328
- resolved to is remembered for the client's life, and `reset()` forgets it.
328
+ resolved to is remembered for one request's chain of redirects, so hops back to the same host
329
+ ask the resolver once; the next request asks again.
329
330
 
330
331
  xtr-http-foundation is not a dependency: install it beside this package (`uv add
331
332
  xtr-http-foundation`) to use the guard. Without it, building `NoPrivateNetworkHttpClient`
@@ -387,6 +388,24 @@ client's, filled in when the client issues it. Reading one no client issued rais
387
388
  `InvalidArgumentError`, and `get_request_method()`, `get_request_url()` and
388
389
  `get_request_options()` are what a test asserts the issued half on.
389
390
 
391
+ ## Profiler panel
392
+
393
+ With [xtr-http-kernel](../xtr-http-kernel)'s profiler on — the web profiler turns it on in
394
+ development — the bundle registers an `http_client` panel, `HttpClientDataCollector`, and traces
395
+ every client it builds into it. For each outgoing request a profiled request made, the panel shows
396
+ the client service it went through (`http_client`, or `http_client_<name>` for a scoped client),
397
+ the method, the URL, the status, how long the answer took to arrive, the error when none did, the
398
+ request's headers and body, the response's headers and the size of the body read — with the
399
+ request count, the failures (an error, or a 4xx or 5xx) and the total time on top. Credentials —
400
+ `Authorization`, `Proxy-Authorization`, cookies, `X-Api-Key`, and any header or body field named
401
+ like a password, a secret, a token or an API key — are kept as `******`, and a request made with
402
+ `extra={"trace_content": False}` keeps no bodies.
403
+
404
+ The records belong to the request being profiled, so requests served at once never mix. A client
405
+ that is traced only for the panel (`tracing` left off) records nothing outside a profiled request.
406
+ With the profiler off, nothing is registered and the clients are built exactly as configured:
407
+ the panel costs nothing.
408
+
390
409
  ## Use in an application
391
410
 
392
411
  Everything adding this package to an application on
@@ -519,6 +538,11 @@ The hierarchy is the [contract's](../xtr-http-client-contracts#errors), re-expor
519
538
  An unsuccessful status names itself: `HTTP 404 returned for "<url>".`, the status line the
520
539
  server sent, or the `title` and `detail` of the body when that body is a JSON error document.
521
540
 
541
+ ## Upgrading
542
+
543
+ What changes for an application moving to 7.0, and how to keep the old behaviour where that is
544
+ still possible: [Upgrading to 7.0](../../UPGRADING.md#xtr-http-client).
545
+
522
546
  ## Development
523
547
 
524
548
  Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
@@ -1,9 +1,9 @@
1
1
  [project]
2
2
  name = "xtr-http-client"
3
- version = "6.8.0"
3
+ version = "7.0.0"
4
4
  description = "An async HTTP client behind one interface: typed request options, bodies read on demand, scoped clients, a private-network guard and a mock client for tests."
5
5
  readme = "README.md"
6
- requires-python = ">=3.11"
6
+ requires-python = ">=3.14.4"
7
7
  license = "MIT"
8
8
  license-files = ["LICENSE"]
9
9
  keywords = [
@@ -18,22 +18,18 @@ classifiers = [
18
18
  "Development Status :: 3 - Alpha",
19
19
  "Framework :: AsyncIO",
20
20
  "Intended Audience :: Developers",
21
- "Programming Language :: Python :: 3.11",
22
- "Programming Language :: Python :: 3.12",
23
- "Programming Language :: Python :: 3.13",
24
21
  "Programming Language :: Python :: 3.14",
25
22
  "Topic :: Internet :: WWW/HTTP",
26
23
  "Typing :: Typed",
27
24
  ]
28
25
  dependencies = [
29
- "typing-extensions>=4.4",
30
- "anyio>=4.0",
31
26
  "httpx>=0.27",
32
- "xtr-http-client-contracts>=6.0,<7",
33
- "xtr-http-message>=6.0,<7",
34
- "xtr-http-message-contracts>=6.0,<7",
35
- "xtr-logging-contracts>=6.0,<7",
36
- "xtr-service-contracts>=6.0,<7",
27
+ "idna>=3.11",
28
+ "xtr-http-client-contracts>=7.0,<8",
29
+ "xtr-http-message>=7.0,<8",
30
+ "xtr-http-message-contracts>=7.0,<8",
31
+ "xtr-logging-contracts>=7.0,<8",
32
+ "xtr-service-contracts>=7.0,<8",
37
33
  ]
38
34
 
39
35
  [[project.authors]]
@@ -51,18 +47,19 @@ http_client = "xtr_http_client.recipe"
51
47
 
52
48
  [dependency-groups]
53
49
  dev = [
54
- "basedpyright>=1.21",
55
- "ruff>=0.8",
56
- "pytest>=8",
57
- "pytest-cov>=5",
58
- "anyio>=4.0",
59
- "h2>=4.0",
50
+ "basedpyright>=1.31.7",
51
+ "ruff>=0.14.7",
52
+ "pytest>=8.4",
53
+ "pytest-cov>=7.1",
54
+ "anyio>=4.10",
55
+ "h2>=4.4",
60
56
  "ty>=0.0.83",
61
- "xtr-clock>=6.0,<7",
62
- "xtr-dependency-injection>=6.0,<7",
63
- "xtr-http-foundation>=6.0,<7",
64
- "xtr-messenger>=6.0,<7",
65
- "xtr-rate-limiter>=6.0,<7",
57
+ "xtr-http-kernel>=7.0,<8",
58
+ "xtr-clock>=7.0,<8",
59
+ "xtr-dependency-injection>=7.0,<8",
60
+ "xtr-http-foundation>=7.0,<8",
61
+ "xtr-messenger>=7.0,<8",
62
+ "xtr-rate-limiter>=7.0,<8",
66
63
  ]
67
64
 
68
65
  [build-system]
@@ -71,7 +68,7 @@ build-backend = "uv_build"
71
68
 
72
69
  [tool.basedpyright]
73
70
  typeCheckingMode = "all"
74
- pythonVersion = "3.11"
71
+ pythonVersion = "3.14"
75
72
  pythonPlatform = "All"
76
73
  include = [
77
74
  "src",
@@ -97,7 +94,7 @@ extraPaths = ["."]
97
94
  reportPrivateUsage = "none"
98
95
 
99
96
  [tool.ruff]
100
- target-version = "py311"
97
+ target-version = "py314"
101
98
  line-length = 100
102
99
  src = [
103
100
  "src",
@@ -1,12 +1,12 @@
1
1
  [project]
2
2
  name = "xtr-http-client"
3
- version = "6.8.0"
3
+ version = "7.0.0"
4
4
  description = "An async HTTP client behind one interface: typed request options, bodies read on demand, scoped clients, a private-network guard and a mock client for tests."
5
5
  readme = "README.md"
6
6
  authors = [
7
7
  { name = "Xterr", email = "me@xterr.dev" }
8
8
  ]
9
- requires-python = ">=3.11"
9
+ requires-python = ">=3.14.4"
10
10
  license = "MIT"
11
11
  license-files = ["LICENSE"]
12
12
  keywords = ["http", "http-client", "request", "response", "mock", "asyncio"]
@@ -14,24 +14,20 @@ classifiers = [
14
14
  "Development Status :: 3 - Alpha",
15
15
  "Framework :: AsyncIO",
16
16
  "Intended Audience :: Developers",
17
- "Programming Language :: Python :: 3.11",
18
- "Programming Language :: Python :: 3.12",
19
- "Programming Language :: Python :: 3.13",
20
17
  "Programming Language :: Python :: 3.14",
21
18
  "Topic :: Internet :: WWW/HTTP",
22
19
  "Typing :: Typed",
23
20
  ]
24
- # `anyio` for the deadlines and task groups a streamed transfer is driven by;
25
- # `httpx` for the one backend that opens the connections, named nowhere else.
21
+ # `httpx` for the one backend that opens the connections, named nowhere else;
22
+ # `idna` for the IDNA 2008 form a host is resolved by.
26
23
  dependencies = [
27
- "typing-extensions>=4.4",
28
- "anyio>=4.0",
29
24
  "httpx>=0.27",
30
- "xtr-http-client-contracts>=6.0,<7",
31
- "xtr-http-message>=6.0,<7",
32
- "xtr-http-message-contracts>=6.0,<7",
33
- "xtr-logging-contracts>=6.0,<7",
34
- "xtr-service-contracts>=6.0,<7",
25
+ "idna>=3.11",
26
+ "xtr-http-client-contracts>=7.0,<8",
27
+ "xtr-http-message>=7.0,<8",
28
+ "xtr-http-message-contracts>=7.0,<8",
29
+ "xtr-logging-contracts>=7.0,<8",
30
+ "xtr-service-contracts>=7.0,<8",
35
31
  ]
36
32
 
37
33
  [project.optional-dependencies]
@@ -51,20 +47,22 @@ http_client = "xtr_http_client.recipe"
51
47
 
52
48
  [dependency-groups]
53
49
  dev = [
54
- "basedpyright>=1.21",
55
- "ruff>=0.8",
56
- "pytest>=8",
57
- "pytest-cov>=5",
58
- "anyio>=4.0",
59
- "h2>=4.0",
50
+ "basedpyright>=1.31.7",
51
+ "ruff>=0.14.7",
52
+ "pytest>=8.4",
53
+ "pytest-cov>=7.1",
54
+ "anyio>=4.10",
55
+ "h2>=4.4",
60
56
  "ty>=0.0.83",
61
- "xtr-clock>=6.0,<7",
62
- "xtr-dependency-injection>=6.0,<7",
57
+ # The profiler panel's collector, tested against the http kernel's profiler.
58
+ "xtr-http-kernel>=7.0,<8",
59
+ "xtr-clock>=7.0,<8",
60
+ "xtr-dependency-injection>=7.0,<8",
63
61
  # The networks NoPrivateNetworkHttpClient refuses; optional for an application.
64
- "xtr-http-foundation>=6.0,<7",
62
+ "xtr-http-foundation>=7.0,<8",
65
63
  # A worker resets the clients between the messages it handles in the kernel tests.
66
- "xtr-messenger>=6.0,<7",
67
- "xtr-rate-limiter>=6.0,<7",
64
+ "xtr-messenger>=7.0,<8",
65
+ "xtr-rate-limiter>=7.0,<8",
68
66
  ]
69
67
 
70
68
  [build-system]
@@ -76,7 +74,7 @@ build-backend = "uv_build"
76
74
  # ─────────────────────────────────────────────────────────────────
77
75
  [tool.basedpyright]
78
76
  typeCheckingMode = "all"
79
- pythonVersion = "3.11"
77
+ pythonVersion = "3.14"
80
78
  pythonPlatform = "All"
81
79
  include = ["src", "tests"]
82
80
  exclude = ["**/__pycache__", "**/.venv", "**/build", "**/dist", ".tmp"]
@@ -102,7 +100,7 @@ reportPrivateUsage = "none"
102
100
  # ruff — select = ["ALL"], minimal justified ignores
103
101
  # ─────────────────────────────────────────────────────────────────
104
102
  [tool.ruff]
105
- target-version = "py311"
103
+ target-version = "py314"
106
104
  line-length = 100
107
105
  src = ["src", "tests"]
108
106
 
@@ -28,6 +28,9 @@ client, so the calling code sees one type however deep the stack is.
28
28
  `MockHttpClient`.
29
29
  - Options are the contract's `RequestOptions`. An option it does not know is refused, never
30
30
  ignored.
31
+ - With xtr-http-kernel's profiler on (the web profiler turns it on in dev), `HttpClientBundle`
32
+ registers the `http_client` panel (`HttpClientDataCollector`) and traces every client into it:
33
+ each request a profiled request made, credentials masked. With the profiler off it costs nothing.
31
34
  - In an application, with xtr-dependency-injection installed beside the package (it is not a
32
35
  dependency): activate `HttpClientBundle` and inject `HttpClientInterface`. Without the
33
36
  container, build the clients yourself; only `xtr_http_client.bundle` needs it.
@@ -113,7 +116,8 @@ client = RetryableHttpClient(inner, GenericRetryStrategy([500, 502, 503]), max_r
113
116
 
114
117
  - `max_retries` is how many **further** attempts are allowed; a request may change it with the
115
118
  `max_retries` option. The response reports `get_info("retry_count")`.
116
- - A `Retry-After` header wins over the strategy's delay.
119
+ - `GenericRetryStrategy` obeys a `Retry-After` header over its backoff, capped by `max_delay_ms`,
120
+ or by `max_retry_after_ms` (60 s) when that is `0`; a custom strategy reads `context.response`.
117
121
  - A body that cannot be produced twice is sent once — a second attempt would send a different
118
122
  request.
119
123
  - `base_uri` naming several makes each attempt move one along them.
@@ -137,13 +141,19 @@ guarded = NoPrivateNetworkHttpClient(inner, allow_list=("10.1.2.3",))
137
141
  ```
138
142
 
139
143
  - **Throttling** books a token before each request and waits for its turn, so the pace holds
140
- under concurrency. Requests sharing a limiter share the pace; `with_options()` keeps it.
144
+ under concurrency. Requests sharing a limiter share the pace; `with_options()` keeps it, and
145
+ `reset()` does not forget it.
141
146
  - **Tracing** fills `client.get_traced_requests()` as requests go on — a `TracedRequest` has
142
147
  `method`, `url`, `options`, `info`, `content`. `extra={"trace_content": False}` drops the
143
148
  bodies (`content` is then `False`); `disabled=lambda: ...` records nothing while it says so.
149
+ Credentials (`auth_basic`, `auth_bearer`, `Authorization` / `Cookie` / `Proxy-Authorization`
150
+ headers) are recorded as `"***"`, and only the last `MAX_TRACES` (1000) requests are kept.
144
151
  - **The guard** is the SSRF answer for a URL that came from outside. It checks what the host
145
152
  *resolves to*, refuses the whole request if any address is refused, pins the request to the
146
153
  checked address through `resolve`, and follows redirects itself so no hop goes unchecked.
154
+ A hop to another origin carries no credentials: the `Authorization`/`Cookie`/`Proxy-Authorization`
155
+ headers, `auth_bearer`, `auth_basic` and `query` stay behind (the hop sends them empty).
156
+ A `resolve` pin holds for the request that carries it only.
147
157
  Refusal is `TransportError`: `Host "<host>" is blocked for "<url>".`. Without `subnets` it
148
158
  refuses `IpUtils.PRIVATE_SUBNETS`; `allow_list` opens one receiver. Addresses and CIDR
149
159
  networks only — never host names.