capo-sagemaker-runtime-http2 0.5.0__tar.gz → 0.7.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 (56) hide show
  1. {capo_sagemaker_runtime_http2-0.5.0/src/capo_sagemaker_runtime_http2.egg-info → capo_sagemaker_runtime_http2-0.7.0}/PKG-INFO +2 -2
  2. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/pyproject.toml +2 -2
  3. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_auth/_sigv4.py +9 -9
  4. capo_sagemaker_runtime_http2-0.7.0/src/capo_sagemaker_runtime_http2/_aws.py +28 -0
  5. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_operations/amazon_sage_maker_runtime_http2/invoke_endpoint_with_bidirectional_stream.py +13 -10
  6. capo_sagemaker_runtime_http2-0.7.0/src/capo_sagemaker_runtime_http2/_services/_pipeline.py +522 -0
  7. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_services/async_sage_maker_runtime_http2.py +45 -7
  8. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_services/sage_maker_runtime_http2.py +45 -7
  9. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/_base.py +8 -3
  10. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/input_validation_error.py +11 -3
  11. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/internal_server_error.py +13 -3
  12. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/internal_stream_failure.py +11 -3
  13. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/model_error.py +13 -3
  14. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/model_stream_error.py +13 -3
  15. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/service_unavailable_error.py +11 -3
  16. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/invoke_endpoint_with_bidirectional_stream_input.py +10 -1
  17. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/invoke_endpoint_with_bidirectional_stream_output.py +10 -1
  18. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/request_payload_part.py +8 -1
  19. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/request_stream_event.py +6 -3
  20. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/response_payload_part.py +8 -1
  21. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/response_stream_event.py +8 -5
  22. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0/src/capo_sagemaker_runtime_http2.egg-info}/PKG-INFO +2 -2
  23. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2.egg-info/SOURCES.txt +1 -0
  24. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2.egg-info/requires.txt +1 -1
  25. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/tests/test_sigv4.py +21 -6
  26. capo_sagemaker_runtime_http2-0.5.0/src/capo_sagemaker_runtime_http2/_services/_pipeline.py +0 -266
  27. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/LICENSE +0 -0
  28. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/README.md +0 -0
  29. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/setup.cfg +0 -0
  30. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/__init__.py +0 -0
  31. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_async.py +0 -0
  32. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_auth/_identity.py +0 -0
  33. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_auth/_providers.py +0 -0
  34. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_auth/_signers.py +0 -0
  35. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_auth/_zapros_handler.py +0 -0
  36. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_body.py +0 -0
  37. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_iter.py +0 -0
  38. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_pagination.py +0 -0
  39. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_protocol/__init__.py +0 -0
  40. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_protocol/errors.py +0 -0
  41. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_protocol/eventstream.py +0 -0
  42. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_protocol/serialize.py +0 -0
  43. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_protocol/xml.py +0 -0
  44. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_rule_engine/__init__.py +0 -0
  45. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_aws_partition.py +0 -0
  46. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_endpoint_rule_set.py +0 -0
  47. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_endpoint_runtime.py +0 -0
  48. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/_services/_aws_config.py +0 -0
  49. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/errors/__init__.py +0 -0
  50. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/py.typed +0 -0
  51. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/_prelude/blob.py +0 -0
  52. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/_prelude/timestamp.py +0 -0
  53. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2/types/sensitive_blob.py +0 -0
  54. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2.egg-info/dependency_links.txt +0 -0
  55. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/src/capo_sagemaker_runtime_http2.egg-info/top_level.txt +0 -0
  56. {capo_sagemaker_runtime_http2-0.5.0 → capo_sagemaker_runtime_http2-0.7.0}/tests/test_body.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: capo-sagemaker-runtime-http2
3
- Version: 0.5.0
3
+ Version: 0.7.0
4
4
  Summary: Python SDK for AWS Sagemaker Runtime Http2.
5
5
  Classifier: Development Status :: 4 - Beta
6
6
  Classifier: Intended Audience :: Developers
@@ -17,7 +17,7 @@ Classifier: Typing :: Typed
17
17
  Requires-Python: >=3.10
18
18
  Description-Content-Type: text/markdown
19
19
  License-File: LICENSE
20
- Requires-Dist: zapros>=0.13.0
20
+ Requires-Dist: zapros>=0.21.0
21
21
  Provides-Extra: anyio
22
22
  Requires-Dist: anyio>=3.0; extra == "anyio"
23
23
  Provides-Extra: sso
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "capo-sagemaker-runtime-http2"
3
- version = "0.5.0"
3
+ version = "0.7.0"
4
4
  description = "Python SDK for AWS Sagemaker Runtime Http2."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -19,7 +19,7 @@ classifiers = [
19
19
  "Typing :: Typed",
20
20
  ]
21
21
  dependencies = [
22
- "zapros>=0.13.0",
22
+ "zapros>=0.21.0",
23
23
  ]
24
24
 
25
25
  [project.optional-dependencies]
@@ -17,7 +17,7 @@ import hmac
17
17
  import re
18
18
  from collections.abc import AsyncIterator, Iterator
19
19
  from typing import Any, Literal, TypedDict, cast
20
- from urllib.parse import quote, unquote
20
+ from urllib.parse import quote
21
21
 
22
22
  import zapros
23
23
  from pywhatwgurl import URLSearchParams
@@ -134,11 +134,11 @@ def _remove_dot_segments(path: str) -> str:
134
134
  def _canonical_path(path: str, *, double_encode: bool, normalize: bool) -> str:
135
135
  """Build CanonicalURI.
136
136
 
137
- Per the SigV4 spec, every segment is URI-encoded; standard services
138
- normalize dot segments and URI-encode each segment **twice**. The
139
- rules-engine ``disableNormalizePath`` / ``disableDoubleEncoding`` flags
140
- turn those steps off — S3-family services set both, so the path is
141
- signed exactly as provided.
137
+ ``path`` is the path as sent on the wire, so it is already URI-encoded
138
+ once. Standard services normalize dot segments and URI-encode each
139
+ segment a second time. The rules-engine ``disableNormalizePath`` /
140
+ ``disableDoubleEncoding`` flags turn those steps off — S3-family
141
+ services set both, so the path is signed exactly as provided.
142
142
  """
143
143
  if not path:
144
144
  return "/"
@@ -148,9 +148,9 @@ def _canonical_path(path: str, *, double_encode: bool, normalize: bool) -> str:
148
148
  path = _remove_dot_segments(path)
149
149
  if not double_encode:
150
150
  return path
151
- decoded = unquote(path)
152
- first = quote(decoded, safe="/~")
153
- return quote(first, safe="/~")
151
+ # Never decode first: that would turn a "%2F" inside a segment (an ARN in
152
+ # a path label) into a separator and leave it out of the second encoding.
153
+ return quote(path, safe="/~")
154
154
 
155
155
 
156
156
  def _canonical_query(query: str) -> str:
@@ -0,0 +1,28 @@
1
+ """Error codes and HTTP statuses that are retried whether or not the model marks them @retryable."""
2
+
3
+ # Transient: retried with the normal backoff.
4
+ KNOWN_RETRYABLE_STATUSES = frozenset({500, 502, 503, 504})
5
+ KNOWN_RETRYABLE_CODES = frozenset(
6
+ {"PriorRequestNotComplete", "RequestTimeout", "RequestTimeoutException"}
7
+ )
8
+
9
+ # Throttling: retried with the longer throttling backoff.
10
+ KNOWN_THROTTLING_STATUSES = frozenset({429})
11
+ KNOWN_THROTTLING_CODES = frozenset(
12
+ {
13
+ "BandwidthLimitExceeded",
14
+ "EC2ThrottledException",
15
+ "LimitExceededException",
16
+ "PriorRequestNotComplete",
17
+ "ProvisionedThroughputExceededException",
18
+ "RequestLimitExceeded",
19
+ "RequestThrottled",
20
+ "RequestThrottledException",
21
+ "SlowDown",
22
+ "ThrottledException",
23
+ "Throttling",
24
+ "ThrottlingException",
25
+ "TooManyRequestsException",
26
+ "TransactionInProgressException",
27
+ }
28
+ )
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import functools
5
6
  import json
6
7
  from typing import Any, cast
7
8
  from urllib.parse import quote
@@ -47,27 +48,27 @@ def handle_error(response: zapros.Response) -> Never:
47
48
  match code:
48
49
  case "InputValidationError":
49
50
  raise capo_sagemaker_runtime_http2.errors.input_validation_error.InputValidationError.from_json(
50
- data, message
51
+ data, message, response=response
51
52
  )
52
53
  case "InternalServerError":
53
54
  raise capo_sagemaker_runtime_http2.errors.internal_server_error.InternalServerError.from_json(
54
- data, message
55
+ data, message, response=response
55
56
  )
56
57
  case "InternalStreamFailure":
57
58
  raise capo_sagemaker_runtime_http2.errors.internal_stream_failure.InternalStreamFailure.from_json(
58
- data, message
59
+ data, message, response=response
59
60
  )
60
61
  case "ModelError":
61
62
  raise capo_sagemaker_runtime_http2.errors.model_error.ModelError.from_json(
62
- data, message
63
+ data, message, response=response
63
64
  )
64
65
  case "ModelStreamError":
65
66
  raise capo_sagemaker_runtime_http2.errors.model_stream_error.ModelStreamError.from_json(
66
- data, message
67
+ data, message, response=response
67
68
  )
68
69
  case "ServiceUnavailableError":
69
70
  raise capo_sagemaker_runtime_http2.errors.service_unavailable_error.ServiceUnavailableError.from_json(
70
- data, message
71
+ data, message, response=response
71
72
  )
72
73
  case _:
73
74
  raise UnknownServiceError(code=code, message=message, response=response)
@@ -77,8 +78,9 @@ def handle_response(
77
78
  response: zapros.Response,
78
79
  ) -> capo_sagemaker_runtime_http2.types.invoke_endpoint_with_bidirectional_stream_output.InvokeEndpointWithBidirectionalStreamOutput:
79
80
  _message_decoder = MessageDecoder()
80
- _union_deser = (
81
- capo_sagemaker_runtime_http2.types.response_stream_event.deserialize_event_json
81
+ _union_deser = functools.partial(
82
+ capo_sagemaker_runtime_http2.types.response_stream_event.deserialize_event_json,
83
+ response=response,
82
84
  )
83
85
  _iter = cast(Any, response.iter_bytes())
84
86
  out: capo_sagemaker_runtime_http2.types.invoke_endpoint_with_bidirectional_stream_output.InvokeEndpointWithBidirectionalStreamOutput = {
@@ -95,8 +97,9 @@ async def async_handle_response(
95
97
  response: zapros.Response,
96
98
  ) -> capo_sagemaker_runtime_http2.types.invoke_endpoint_with_bidirectional_stream_output.InvokeEndpointWithBidirectionalStreamOutput:
97
99
  _message_decoder = MessageDecoder()
98
- _union_deser = (
99
- capo_sagemaker_runtime_http2.types.response_stream_event.deserialize_event_json
100
+ _union_deser = functools.partial(
101
+ capo_sagemaker_runtime_http2.types.response_stream_event.deserialize_event_json,
102
+ response=response,
100
103
  )
101
104
  _iter = cast(Any, response.async_iter_bytes())
102
105
  out: capo_sagemaker_runtime_http2.types.invoke_endpoint_with_bidirectional_stream_output.InvokeEndpointWithBidirectionalStreamOutput = {
@@ -0,0 +1,522 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import random
5
+ import threading
6
+ import time
7
+ from collections.abc import AsyncIterator, Iterator, Sequence
8
+ from dataclasses import dataclass
9
+ from typing import (
10
+ TYPE_CHECKING,
11
+ Any,
12
+ Awaitable,
13
+ Callable,
14
+ Generic,
15
+ TypeVar,
16
+ cast,
17
+ )
18
+
19
+ from typing_extensions import Never
20
+ from zapros import (
21
+ AsyncClient,
22
+ Client,
23
+ ConnectionError,
24
+ ConnectTimeoutError,
25
+ PoolTimeoutError,
26
+ Response,
27
+ SSLError,
28
+ TimeoutError,
29
+ )
30
+
31
+ from capo_sagemaker_runtime_http2._async import anysleep
32
+ from capo_sagemaker_runtime_http2._aws import (
33
+ KNOWN_RETRYABLE_CODES,
34
+ KNOWN_RETRYABLE_STATUSES,
35
+ KNOWN_THROTTLING_CODES,
36
+ KNOWN_THROTTLING_STATUSES,
37
+ )
38
+ from capo_sagemaker_runtime_http2._body import Body
39
+ from capo_sagemaker_runtime_http2._iter import StaticAnyIterator
40
+ from capo_sagemaker_runtime_http2.errors import ServiceError, UnknownServiceError
41
+
42
+ if TYPE_CHECKING:
43
+ from capo_sagemaker_runtime_http2._auth._identity import Credentials
44
+ from capo_sagemaker_runtime_http2._auth._providers import IdentityProvider
45
+
46
+ logger = logging.getLogger("capo_sagemaker_runtime_http2.operation")
47
+
48
+ TInput = TypeVar("TInput")
49
+ TOutput = TypeVar("TOutput")
50
+
51
+
52
+ class RetryQuota:
53
+ """Retry tokens shared by all operations of one client: retries spend them, successes refund them."""
54
+
55
+ CAPACITY: int = 500
56
+ TRANSIENT_COST: int = 14
57
+ THROTTLING_COST: int = 5
58
+ NO_RETRY_INCREMENT: int = 1
59
+
60
+ def __init__(self) -> None:
61
+ self._available: int = self.CAPACITY
62
+
63
+ def acquire(self, is_throttling: bool) -> int | None:
64
+ cost = self.THROTTLING_COST if is_throttling else self.TRANSIENT_COST
65
+ if cost > self._available:
66
+ return None
67
+ self._available -= cost
68
+ return cost
69
+
70
+ def release(self, last_retry_cost: int | None) -> None:
71
+ refund = self.NO_RETRY_INCREMENT if last_retry_cost is None else last_retry_cost
72
+ self._available = min(self.CAPACITY, self._available + refund)
73
+
74
+
75
+ class ThreadSafeRetryQuota(RetryQuota):
76
+ """A ``RetryQuota`` that sync clients can share across threads."""
77
+
78
+ def __init__(self) -> None:
79
+ super().__init__()
80
+ self._lock: threading.Lock = threading.Lock()
81
+
82
+ def acquire(self, is_throttling: bool) -> int | None:
83
+ with self._lock:
84
+ return super().acquire(is_throttling)
85
+
86
+ def release(self, last_retry_cost: int | None) -> None:
87
+ with self._lock:
88
+ super().release(last_retry_cost)
89
+
90
+
91
+ @dataclass
92
+ class OperationOptions:
93
+ client: Client
94
+ retry_quota: RetryQuota
95
+ use_dual_stack: bool | None = None
96
+ use_fips: bool | None = None
97
+ endpoint: str | None = None
98
+ region: str | None = None
99
+ retry_max_attempts: int | None = None
100
+ credentials_provider: IdentityProvider[Credentials] | None = None
101
+ anonymous: bool | None = None
102
+
103
+
104
+ @dataclass
105
+ class AsyncOperationOptions:
106
+ client: AsyncClient
107
+ retry_quota: RetryQuota
108
+ use_dual_stack: bool | None = None
109
+ use_fips: bool | None = None
110
+ endpoint: str | None = None
111
+ region: str | None = None
112
+ retry_max_attempts: int | None = None
113
+ credentials_provider: IdentityProvider[Credentials] | None = None
114
+ anonymous: bool | None = None
115
+
116
+
117
+ @dataclass
118
+ class OperationRequest(Generic[TInput]):
119
+ input: TInput
120
+ options: OperationOptions
121
+
122
+
123
+ @dataclass
124
+ class AsyncOperationRequest(Generic[TInput]):
125
+ input: TInput
126
+ options: AsyncOperationOptions
127
+
128
+
129
+ @dataclass
130
+ class OperationResponse(Generic[TOutput]):
131
+ output: TOutput
132
+ response: Response
133
+
134
+
135
+ @dataclass
136
+ class AsyncOperationResponse(Generic[TOutput]):
137
+ output: TOutput
138
+ response: Response
139
+
140
+
141
+ NextFn = Callable[
142
+ [OperationRequest[TInput]],
143
+ OperationResponse[TOutput],
144
+ ]
145
+
146
+ AsyncNextFn = Callable[
147
+ [AsyncOperationRequest[TInput]],
148
+ Awaitable[AsyncOperationResponse[TOutput]],
149
+ ]
150
+
151
+ Interceptor = Callable[
152
+ [OperationRequest[TInput], NextFn[TInput, TOutput]],
153
+ OperationResponse[TOutput],
154
+ ]
155
+
156
+ AsyncInterceptor = Callable[
157
+ [AsyncOperationRequest[TInput], AsyncNextFn[TInput, TOutput]],
158
+ Awaitable[AsyncOperationResponse[TOutput]],
159
+ ]
160
+
161
+
162
+ @dataclass
163
+ class OperationLogging:
164
+ """What the logging interceptor needs to know about one operation call."""
165
+
166
+ operation: str
167
+ # Return a copy that is safe to log; None when the shape has nothing to hide.
168
+ redact_input: Callable[[Any], dict[str, Any]] | None = None
169
+ redact_output: Callable[[Any], dict[str, Any]] | None = None
170
+
171
+
172
+ def execute_pipeline(
173
+ request: OperationRequest[TInput],
174
+ handler: NextFn[TInput, TOutput],
175
+ interceptors: Sequence[Interceptor[TInput, TOutput]],
176
+ ) -> OperationResponse[TOutput]:
177
+ def make_chain(index: int) -> NextFn[TInput, TOutput]:
178
+ if index < len(interceptors):
179
+ interceptor = interceptors[index]
180
+
181
+ def next_fn(req: OperationRequest[TInput]) -> OperationResponse[TOutput]:
182
+ return interceptor(req, make_chain(index + 1))
183
+
184
+ return next_fn
185
+ return handler
186
+
187
+ return make_chain(0)(request)
188
+
189
+
190
+ async def aexecute_pipeline(
191
+ request: AsyncOperationRequest[TInput],
192
+ handler: AsyncNextFn[TInput, TOutput],
193
+ interceptors: Sequence[AsyncInterceptor[TInput, TOutput]],
194
+ ) -> AsyncOperationResponse[TOutput]:
195
+ def make_chain(index: int) -> AsyncNextFn[TInput, TOutput]:
196
+ if index < len(interceptors):
197
+ interceptor = interceptors[index]
198
+
199
+ async def next_fn(
200
+ req: AsyncOperationRequest[TInput],
201
+ ) -> AsyncOperationResponse[TOutput]:
202
+ return await interceptor(req, make_chain(index + 1))
203
+
204
+ return next_fn
205
+ return handler
206
+
207
+ return await make_chain(0)(request)
208
+
209
+
210
+ def raise_error(response: Response, handle_error: Callable[[Response], Never]) -> Never:
211
+ try:
212
+ handle_error(response)
213
+ except ServiceError as exc:
214
+ _apply_retry_rules(exc, response)
215
+ raise
216
+ except Exception as exc:
217
+ # The error body could not be decoded (an HTML page from a proxy, a
218
+ # truncated read): the status code is all there is to go on.
219
+ error = UnknownServiceError(code=None, message=None, response=response)
220
+ _apply_retry_rules(error, response)
221
+ raise error from exc
222
+
223
+
224
+ def _apply_retry_rules(exc: ServiceError, response: Response) -> None:
225
+ # A retry can be safe even when the error is not marked @retryable: a known
226
+ # throttling or transient status or error code, or a Retry-After header.
227
+ if (
228
+ response.status in KNOWN_THROTTLING_STATUSES
229
+ or exc.code in KNOWN_THROTTLING_CODES
230
+ ):
231
+ exc.is_throttling_error = True
232
+ exc.is_retryable = True
233
+ if (
234
+ response.status in KNOWN_RETRYABLE_STATUSES
235
+ or exc.code in KNOWN_RETRYABLE_CODES
236
+ or "retry-after" in response.headers
237
+ ):
238
+ exc.is_retryable = True
239
+
240
+
241
+ def _is_retryable(exc: Exception) -> bool:
242
+ if isinstance(exc, ServiceError):
243
+ return exc.is_retryable
244
+ if isinstance(exc, SSLError):
245
+ return False
246
+ if isinstance(exc, (ConnectionError, TimeoutError)):
247
+ return True
248
+ return False
249
+
250
+
251
+ def _is_pre_transmission(exc: Exception) -> bool:
252
+ # True when the request body was certainly not written before the error,
253
+ # so the same stream can be reused as-is. A bare ConnectionError is
254
+ # ambiguous ("cannot be established or is lost"), so it does not qualify.
255
+ return isinstance(exc, (ConnectTimeoutError, PoolTimeoutError))
256
+
257
+
258
+ def _prepare_retry(inp: object) -> bool:
259
+ if not isinstance(inp, dict):
260
+ return True
261
+ for value in inp.values():
262
+ if isinstance(value, Body):
263
+ value = cast(Body[Iterator[bytes]], value)
264
+ if value.rebuild() is None:
265
+ return False
266
+ elif isinstance(value, StaticAnyIterator):
267
+ continue
268
+ elif isinstance(value, (Iterator, AsyncIterator)):
269
+ return False
270
+ return True
271
+
272
+
273
+ async def _aprepare_retry(inp: object) -> bool:
274
+ if not isinstance(inp, dict):
275
+ return True
276
+ for value in inp.values():
277
+ if isinstance(value, Body):
278
+ value = cast(Body[AsyncIterator[bytes]], value)
279
+ if await value.arebuild() is None:
280
+ return False
281
+ elif isinstance(value, StaticAnyIterator):
282
+ continue
283
+ elif isinstance(value, (Iterator, AsyncIterator)):
284
+ return False
285
+ return True
286
+
287
+
288
+ def _retry_delay(attempt: int, is_throttling: bool) -> float:
289
+ base = 1.0 if is_throttling else 0.5
290
+ delay = base * (2.0 ** min(attempt - 1, 10))
291
+ delay = min(20.0, delay)
292
+ return random.uniform(0.0, delay)
293
+
294
+
295
+ def retry() -> Interceptor[TInput, TOutput]:
296
+ def interceptor(
297
+ request: OperationRequest[TInput], next: NextFn[TInput, TOutput]
298
+ ) -> OperationResponse[TOutput]:
299
+ max_attempts = request.options.retry_max_attempts or 3
300
+ quota = request.options.retry_quota
301
+ last_retry_cost: int | None = None
302
+ for attempt in range(1, max_attempts + 1):
303
+ try:
304
+ response = next(request)
305
+ except Exception as exc:
306
+ if not _is_retryable(exc) or attempt == max_attempts:
307
+ raise
308
+ if not _is_pre_transmission(exc) and not _prepare_retry(request.input):
309
+ logger.debug(
310
+ "attempt %d/%d failed: %r, not retrying because the request body cannot be replayed",
311
+ attempt,
312
+ max_attempts,
313
+ exc,
314
+ extra={
315
+ "attempt": attempt,
316
+ "max_attempts": max_attempts,
317
+ "error": exc,
318
+ },
319
+ )
320
+ raise
321
+ is_throttling = (
322
+ isinstance(exc, ServiceError) and exc.is_throttling_error
323
+ )
324
+ cost = quota.acquire(is_throttling)
325
+ if cost is None:
326
+ logger.debug(
327
+ "attempt %d/%d failed: %r, not retrying because the retry quota is exhausted",
328
+ attempt,
329
+ max_attempts,
330
+ exc,
331
+ extra={
332
+ "attempt": attempt,
333
+ "max_attempts": max_attempts,
334
+ "error": exc,
335
+ },
336
+ )
337
+ raise
338
+ last_retry_cost = cost
339
+ delay = _retry_delay(attempt, is_throttling)
340
+ logger.debug(
341
+ "attempt %d/%d failed: %r, retrying in %.2fs",
342
+ attempt,
343
+ max_attempts,
344
+ exc,
345
+ delay,
346
+ extra={
347
+ "attempt": attempt,
348
+ "max_attempts": max_attempts,
349
+ "error": exc,
350
+ "delay": delay,
351
+ },
352
+ )
353
+ time.sleep(delay)
354
+ else:
355
+ quota.release(last_retry_cost)
356
+ return response
357
+ raise AssertionError("unreachable")
358
+
359
+ return interceptor
360
+
361
+
362
+ def aretry() -> AsyncInterceptor[TInput, TOutput]:
363
+ async def interceptor(
364
+ request: AsyncOperationRequest[TInput], next: AsyncNextFn[TInput, TOutput]
365
+ ) -> AsyncOperationResponse[TOutput]:
366
+ max_attempts = request.options.retry_max_attempts or 3
367
+ quota = request.options.retry_quota
368
+ last_retry_cost: int | None = None
369
+ for attempt in range(1, max_attempts + 1):
370
+ try:
371
+ response = await next(request)
372
+ except Exception as exc:
373
+ if not _is_retryable(exc) or attempt == max_attempts:
374
+ raise
375
+ if not _is_pre_transmission(exc) and not await _aprepare_retry(
376
+ request.input
377
+ ):
378
+ logger.debug(
379
+ "attempt %d/%d failed: %r, not retrying because the request body cannot be replayed",
380
+ attempt,
381
+ max_attempts,
382
+ exc,
383
+ extra={
384
+ "attempt": attempt,
385
+ "max_attempts": max_attempts,
386
+ "error": exc,
387
+ },
388
+ )
389
+ raise
390
+ is_throttling = (
391
+ isinstance(exc, ServiceError) and exc.is_throttling_error
392
+ )
393
+ cost = quota.acquire(is_throttling)
394
+ if cost is None:
395
+ logger.debug(
396
+ "attempt %d/%d failed: %r, not retrying because the retry quota is exhausted",
397
+ attempt,
398
+ max_attempts,
399
+ exc,
400
+ extra={
401
+ "attempt": attempt,
402
+ "max_attempts": max_attempts,
403
+ "error": exc,
404
+ },
405
+ )
406
+ raise
407
+ last_retry_cost = cost
408
+ delay = _retry_delay(attempt, is_throttling)
409
+ logger.debug(
410
+ "attempt %d/%d failed: %r, retrying in %.2fs",
411
+ attempt,
412
+ max_attempts,
413
+ exc,
414
+ delay,
415
+ extra={
416
+ "attempt": attempt,
417
+ "max_attempts": max_attempts,
418
+ "error": exc,
419
+ "delay": delay,
420
+ },
421
+ )
422
+ await anysleep(delay)
423
+ else:
424
+ quota.release(last_retry_cost)
425
+ return response
426
+ raise AssertionError("unreachable")
427
+
428
+ return interceptor
429
+
430
+
431
+ def log_operation(operation_logging: OperationLogging) -> Interceptor[TInput, TOutput]:
432
+ operation = operation_logging.operation
433
+ redact_input = operation_logging.redact_input
434
+ redact_output = operation_logging.redact_output
435
+
436
+ def interceptor(
437
+ request: OperationRequest[TInput], next: NextFn[TInput, TOutput]
438
+ ) -> OperationResponse[TOutput]:
439
+ debug = logger.isEnabledFor(logging.DEBUG)
440
+ if debug:
441
+ input_ = (
442
+ request.input if redact_input is None else redact_input(request.input)
443
+ )
444
+ logger.debug(
445
+ "%s input: %r",
446
+ operation,
447
+ input_,
448
+ extra={"operation": operation, "input": input_},
449
+ )
450
+ try:
451
+ response = next(request)
452
+ except Exception as exc:
453
+ logger.debug(
454
+ "%s failed: %r",
455
+ operation,
456
+ exc,
457
+ extra={"operation": operation, "error": exc},
458
+ )
459
+ raise
460
+ if debug:
461
+ output = (
462
+ response.output
463
+ if redact_output is None
464
+ else redact_output(response.output)
465
+ )
466
+ logger.debug(
467
+ "%s output: %r",
468
+ operation,
469
+ output,
470
+ extra={"operation": operation, "output": output},
471
+ )
472
+ return response
473
+
474
+ return interceptor
475
+
476
+
477
+ def alog_operation(
478
+ operation_logging: OperationLogging,
479
+ ) -> AsyncInterceptor[TInput, TOutput]:
480
+ operation = operation_logging.operation
481
+ redact_input = operation_logging.redact_input
482
+ redact_output = operation_logging.redact_output
483
+
484
+ async def interceptor(
485
+ request: AsyncOperationRequest[TInput], next: AsyncNextFn[TInput, TOutput]
486
+ ) -> AsyncOperationResponse[TOutput]:
487
+ debug = logger.isEnabledFor(logging.DEBUG)
488
+ if debug:
489
+ input_ = (
490
+ request.input if redact_input is None else redact_input(request.input)
491
+ )
492
+ logger.debug(
493
+ "%s input: %r",
494
+ operation,
495
+ input_,
496
+ extra={"operation": operation, "input": input_},
497
+ )
498
+ try:
499
+ response = await next(request)
500
+ except Exception as exc:
501
+ logger.debug(
502
+ "%s failed: %r",
503
+ operation,
504
+ exc,
505
+ extra={"operation": operation, "error": exc},
506
+ )
507
+ raise
508
+ if debug:
509
+ output = (
510
+ response.output
511
+ if redact_output is None
512
+ else redact_output(response.output)
513
+ )
514
+ logger.debug(
515
+ "%s output: %r",
516
+ operation,
517
+ output,
518
+ extra={"operation": operation, "output": output},
519
+ )
520
+ return response
521
+
522
+ return interceptor