capo-codeguruprofiler 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. capo_codeguruprofiler/__init__.py +74 -0
  2. capo_codeguruprofiler/_async.py +25 -0
  3. capo_codeguruprofiler/_auth/_identity.py +16 -0
  4. capo_codeguruprofiler/_auth/_providers.py +886 -0
  5. capo_codeguruprofiler/_auth/_signers.py +88 -0
  6. capo_codeguruprofiler/_auth/_sigv4.py +434 -0
  7. capo_codeguruprofiler/_auth/_zapros_handler.py +80 -0
  8. capo_codeguruprofiler/_iter.py +113 -0
  9. capo_codeguruprofiler/_operations/code_guru_profiler/add_notification_channels.py +185 -0
  10. capo_codeguruprofiler/_operations/code_guru_profiler/batch_get_frame_metric_data.py +202 -0
  11. capo_codeguruprofiler/_operations/code_guru_profiler/configure_agent.py +177 -0
  12. capo_codeguruprofiler/_operations/code_guru_profiler/create_profiling_group.py +180 -0
  13. capo_codeguruprofiler/_operations/code_guru_profiler/delete_profiling_group.py +165 -0
  14. capo_codeguruprofiler/_operations/code_guru_profiler/describe_profiling_group.py +169 -0
  15. capo_codeguruprofiler/_operations/code_guru_profiler/get_findings_report_account_summary.py +164 -0
  16. capo_codeguruprofiler/_operations/code_guru_profiler/get_notification_configuration.py +168 -0
  17. capo_codeguruprofiler/_operations/code_guru_profiler/get_policy.py +161 -0
  18. capo_codeguruprofiler/_operations/code_guru_profiler/get_profile.py +196 -0
  19. capo_codeguruprofiler/_operations/code_guru_profiler/get_recommendations.py +196 -0
  20. capo_codeguruprofiler/_operations/code_guru_profiler/list_findings_reports.py +197 -0
  21. capo_codeguruprofiler/_operations/code_guru_profiler/list_profile_times.py +196 -0
  22. capo_codeguruprofiler/_operations/code_guru_profiler/list_profiling_groups.py +160 -0
  23. capo_codeguruprofiler/_operations/code_guru_profiler/list_tags_for_resource.py +158 -0
  24. capo_codeguruprofiler/_operations/code_guru_profiler/post_agent_profile.py +168 -0
  25. capo_codeguruprofiler/_operations/code_guru_profiler/put_permission.py +182 -0
  26. capo_codeguruprofiler/_operations/code_guru_profiler/remove_notification_channel.py +169 -0
  27. capo_codeguruprofiler/_operations/code_guru_profiler/remove_permission.py +175 -0
  28. capo_codeguruprofiler/_operations/code_guru_profiler/submit_feedback.py +170 -0
  29. capo_codeguruprofiler/_operations/code_guru_profiler/tag_resource.py +158 -0
  30. capo_codeguruprofiler/_operations/code_guru_profiler/untag_resource.py +156 -0
  31. capo_codeguruprofiler/_operations/code_guru_profiler/update_profiling_group.py +181 -0
  32. capo_codeguruprofiler/_pagination.py +21 -0
  33. capo_codeguruprofiler/_protocol/__init__.py +1 -0
  34. capo_codeguruprofiler/_protocol/errors.py +93 -0
  35. capo_codeguruprofiler/_protocol/eventstream.py +238 -0
  36. capo_codeguruprofiler/_protocol/serialize.py +47 -0
  37. capo_codeguruprofiler/_protocol/xml.py +33 -0
  38. capo_codeguruprofiler/_resources/code_guru_profiler/profiling_group.py +2257 -0
  39. capo_codeguruprofiler/_rule_engine/__init__.py +0 -0
  40. capo_codeguruprofiler/_rule_engine/_aws_partition.py +160 -0
  41. capo_codeguruprofiler/_rule_engine/_endpoint_rule_set.py +138 -0
  42. capo_codeguruprofiler/_rule_engine/_endpoint_runtime.py +389 -0
  43. capo_codeguruprofiler/_services/_aws_config.py +160 -0
  44. capo_codeguruprofiler/_services/_pipeline.py +198 -0
  45. capo_codeguruprofiler/_services/async_code_guru_profiler.py +377 -0
  46. capo_codeguruprofiler/_services/code_guru_profiler.py +373 -0
  47. capo_codeguruprofiler/errors/__init__.py +35 -0
  48. capo_codeguruprofiler/errors/_base.py +94 -0
  49. capo_codeguruprofiler/errors/conflict_exception.py +45 -0
  50. capo_codeguruprofiler/errors/internal_server_exception.py +47 -0
  51. capo_codeguruprofiler/errors/resource_not_found_exception.py +47 -0
  52. capo_codeguruprofiler/errors/service_quota_exceeded_exception.py +49 -0
  53. capo_codeguruprofiler/errors/throttling_exception.py +45 -0
  54. capo_codeguruprofiler/errors/validation_exception.py +45 -0
  55. capo_codeguruprofiler/py.typed +0 -0
  56. capo_codeguruprofiler/types/_prelude/blob.py +12 -0
  57. capo_codeguruprofiler/types/_prelude/timestamp.py +17 -0
  58. capo_codeguruprofiler/types/action_group.py +5 -0
  59. capo_codeguruprofiler/types/add_notification_channels_request.py +44 -0
  60. capo_codeguruprofiler/types/add_notification_channels_response.py +42 -0
  61. capo_codeguruprofiler/types/agent_configuration.py +58 -0
  62. capo_codeguruprofiler/types/agent_orchestration_config.py +28 -0
  63. capo_codeguruprofiler/types/agent_parameter_field.py +5 -0
  64. capo_codeguruprofiler/types/agent_parameters.py +27 -0
  65. capo_codeguruprofiler/types/agent_profile.py +15 -0
  66. capo_codeguruprofiler/types/aggregated_profile.py +15 -0
  67. capo_codeguruprofiler/types/aggregated_profile_time.py +45 -0
  68. capo_codeguruprofiler/types/aggregation_period.py +5 -0
  69. capo_codeguruprofiler/types/anomalies.py +29 -0
  70. capo_codeguruprofiler/types/anomaly.py +62 -0
  71. capo_codeguruprofiler/types/anomaly_instance.py +77 -0
  72. capo_codeguruprofiler/types/anomaly_instance_id.py +5 -0
  73. capo_codeguruprofiler/types/anomaly_instances.py +31 -0
  74. capo_codeguruprofiler/types/batch_get_frame_metric_data_request.py +56 -0
  75. capo_codeguruprofiler/types/batch_get_frame_metric_data_response.py +130 -0
  76. capo_codeguruprofiler/types/channel.py +58 -0
  77. capo_codeguruprofiler/types/channel_id.py +5 -0
  78. capo_codeguruprofiler/types/channel_uri.py +6 -0
  79. capo_codeguruprofiler/types/channels.py +29 -0
  80. capo_codeguruprofiler/types/client_token.py +5 -0
  81. capo_codeguruprofiler/types/compute_platform.py +5 -0
  82. capo_codeguruprofiler/types/configure_agent_request.py +50 -0
  83. capo_codeguruprofiler/types/configure_agent_response.py +43 -0
  84. capo_codeguruprofiler/types/create_profiling_group_request.py +81 -0
  85. capo_codeguruprofiler/types/create_profiling_group_response.py +45 -0
  86. capo_codeguruprofiler/types/delete_profiling_group_request.py +26 -0
  87. capo_codeguruprofiler/types/delete_profiling_group_response.py +18 -0
  88. capo_codeguruprofiler/types/describe_profiling_group_request.py +26 -0
  89. capo_codeguruprofiler/types/describe_profiling_group_response.py +45 -0
  90. capo_codeguruprofiler/types/event_publisher.py +5 -0
  91. capo_codeguruprofiler/types/event_publishers.py +19 -0
  92. capo_codeguruprofiler/types/feedback_type.py +5 -0
  93. capo_codeguruprofiler/types/findings_report_id.py +5 -0
  94. capo_codeguruprofiler/types/findings_report_summaries.py +35 -0
  95. capo_codeguruprofiler/types/findings_report_summary.py +73 -0
  96. capo_codeguruprofiler/types/fleet_instance_id.py +5 -0
  97. capo_codeguruprofiler/types/frame_metric.py +56 -0
  98. capo_codeguruprofiler/types/frame_metric_data.py +33 -0
  99. capo_codeguruprofiler/types/frame_metric_datum.py +56 -0
  100. capo_codeguruprofiler/types/frame_metric_value.py +5 -0
  101. capo_codeguruprofiler/types/frame_metric_values.py +30 -0
  102. capo_codeguruprofiler/types/frame_metrics.py +29 -0
  103. capo_codeguruprofiler/types/get_findings_report_account_summary_request.py +31 -0
  104. capo_codeguruprofiler/types/get_findings_report_account_summary_response.py +56 -0
  105. capo_codeguruprofiler/types/get_notification_configuration_request.py +26 -0
  106. capo_codeguruprofiler/types/get_notification_configuration_response.py +45 -0
  107. capo_codeguruprofiler/types/get_policy_request.py +26 -0
  108. capo_codeguruprofiler/types/get_policy_response.py +38 -0
  109. capo_codeguruprofiler/types/get_profile_request.py +39 -0
  110. capo_codeguruprofiler/types/get_profile_response.py +45 -0
  111. capo_codeguruprofiler/types/get_recommendations_request.py +34 -0
  112. capo_codeguruprofiler/types/get_recommendations_response.py +110 -0
  113. capo_codeguruprofiler/types/list_findings_reports_request.py +41 -0
  114. capo_codeguruprofiler/types/list_findings_reports_response.py +56 -0
  115. capo_codeguruprofiler/types/list_of_timestamps.py +33 -0
  116. capo_codeguruprofiler/types/list_profile_times_request.py +45 -0
  117. capo_codeguruprofiler/types/list_profile_times_response.py +50 -0
  118. capo_codeguruprofiler/types/list_profiling_groups_request.py +31 -0
  119. capo_codeguruprofiler/types/list_profiling_groups_response.py +77 -0
  120. capo_codeguruprofiler/types/list_tags_for_resource_request.py +24 -0
  121. capo_codeguruprofiler/types/list_tags_for_resource_response.py +34 -0
  122. capo_codeguruprofiler/types/locale.py +6 -0
  123. capo_codeguruprofiler/types/match.py +43 -0
  124. capo_codeguruprofiler/types/matches.py +29 -0
  125. capo_codeguruprofiler/types/max_depth.py +5 -0
  126. capo_codeguruprofiler/types/max_results.py +5 -0
  127. capo_codeguruprofiler/types/metadata.py +27 -0
  128. capo_codeguruprofiler/types/metadata_field.py +5 -0
  129. capo_codeguruprofiler/types/metric.py +54 -0
  130. capo_codeguruprofiler/types/metric_type.py +5 -0
  131. capo_codeguruprofiler/types/notification_configuration.py +36 -0
  132. capo_codeguruprofiler/types/order_by.py +5 -0
  133. capo_codeguruprofiler/types/pagination_token.py +5 -0
  134. capo_codeguruprofiler/types/pattern.py +95 -0
  135. capo_codeguruprofiler/types/percentage.py +5 -0
  136. capo_codeguruprofiler/types/period.py +5 -0
  137. capo_codeguruprofiler/types/post_agent_profile_request.py +51 -0
  138. capo_codeguruprofiler/types/post_agent_profile_response.py +18 -0
  139. capo_codeguruprofiler/types/principal.py +5 -0
  140. capo_codeguruprofiler/types/principals.py +17 -0
  141. capo_codeguruprofiler/types/profile_time.py +36 -0
  142. capo_codeguruprofiler/types/profile_times.py +29 -0
  143. capo_codeguruprofiler/types/profiling_group_arn.py +5 -0
  144. capo_codeguruprofiler/types/profiling_group_description.py +132 -0
  145. capo_codeguruprofiler/types/profiling_group_descriptions.py +37 -0
  146. capo_codeguruprofiler/types/profiling_group_name.py +5 -0
  147. capo_codeguruprofiler/types/profiling_group_names.py +19 -0
  148. capo_codeguruprofiler/types/profiling_status.py +83 -0
  149. capo_codeguruprofiler/types/put_permission_request.py +54 -0
  150. capo_codeguruprofiler/types/put_permission_response.py +38 -0
  151. capo_codeguruprofiler/types/recommendation.py +108 -0
  152. capo_codeguruprofiler/types/recommendations.py +31 -0
  153. capo_codeguruprofiler/types/remove_notification_channel_request.py +29 -0
  154. capo_codeguruprofiler/types/remove_notification_channel_response.py +42 -0
  155. capo_codeguruprofiler/types/remove_permission_request.py +32 -0
  156. capo_codeguruprofiler/types/remove_permission_response.py +38 -0
  157. capo_codeguruprofiler/types/revision_id.py +5 -0
  158. capo_codeguruprofiler/types/strings.py +14 -0
  159. capo_codeguruprofiler/types/submit_feedback_request.py +47 -0
  160. capo_codeguruprofiler/types/submit_feedback_response.py +18 -0
  161. capo_codeguruprofiler/types/tag_keys.py +14 -0
  162. capo_codeguruprofiler/types/tag_resource_request.py +40 -0
  163. capo_codeguruprofiler/types/tag_resource_response.py +18 -0
  164. capo_codeguruprofiler/types/tags_map.py +22 -0
  165. capo_codeguruprofiler/types/target_frame.py +14 -0
  166. capo_codeguruprofiler/types/target_frames.py +29 -0
  167. capo_codeguruprofiler/types/thread_states.py +14 -0
  168. capo_codeguruprofiler/types/timestamp.py +20 -0
  169. capo_codeguruprofiler/types/timestamp_structure.py +37 -0
  170. capo_codeguruprofiler/types/unprocessed_end_time_map.py +33 -0
  171. capo_codeguruprofiler/types/untag_resource_request.py +27 -0
  172. capo_codeguruprofiler/types/untag_resource_response.py +18 -0
  173. capo_codeguruprofiler/types/update_profiling_group_request.py +50 -0
  174. capo_codeguruprofiler/types/update_profiling_group_response.py +45 -0
  175. capo_codeguruprofiler/types/user_feedback.py +31 -0
  176. capo_codeguruprofiler-0.1.0.dist-info/METADATA +101 -0
  177. capo_codeguruprofiler-0.1.0.dist-info/RECORD +180 -0
  178. capo_codeguruprofiler-0.1.0.dist-info/WHEEL +5 -0
  179. capo_codeguruprofiler-0.1.0.dist-info/licenses/LICENSE +21 -0
  180. capo_codeguruprofiler-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,160 @@
1
+ from __future__ import annotations
2
+
3
+ import configparser
4
+ import functools
5
+ import os
6
+ from dataclasses import dataclass
7
+ from pathlib import Path
8
+ from typing import TypeVar
9
+
10
+ from capo_codeguruprofiler._services._pipeline import (
11
+ AsyncInterceptor,
12
+ AsyncNextFn,
13
+ AsyncOperationRequest,
14
+ AsyncOperationResponse,
15
+ Interceptor,
16
+ NextFn,
17
+ OperationRequest,
18
+ OperationResponse,
19
+ )
20
+
21
+ ENDPOINT_ENV_VAR = "AWS_ENDPOINT_URL_CODE_GURU_PROFILER"
22
+ SERVICE_ID = "code_guru_profiler"
23
+ TInput = TypeVar("TInput")
24
+ TOutput = TypeVar("TOutput")
25
+
26
+
27
+ @dataclass(frozen=True)
28
+ class AwsSettings:
29
+ region: str | None
30
+ endpoint: str | None
31
+ use_fips: bool
32
+ use_dual_stack: bool
33
+ max_attempts: int
34
+
35
+
36
+ def _env_bool(name: str) -> bool | None:
37
+ val = os.environ.get(name)
38
+ if val is None:
39
+ return None
40
+ return val.strip().lower() in ("true", "1")
41
+
42
+
43
+ def _profile_bool(section: dict[str, str], key: str) -> bool | None:
44
+ val = section.get(key)
45
+ if val is None:
46
+ return None
47
+ return val.strip().lower() in ("true", "1")
48
+
49
+
50
+ def config_file() -> Path:
51
+ raw_cfg = os.environ.get("AWS_CONFIG_FILE")
52
+ return Path(raw_cfg).expanduser() if raw_cfg else Path.home() / ".aws" / "config"
53
+
54
+
55
+ def active_profile() -> str:
56
+ return (
57
+ os.environ.get("AWS_PROFILE")
58
+ or os.environ.get("AWS_DEFAULT_PROFILE")
59
+ or "default"
60
+ )
61
+
62
+
63
+ def _load_profile(
64
+ profile: str | None = None,
65
+ ) -> tuple[dict[str, str], dict[str, dict[str, str]]]:
66
+ profile = profile or active_profile()
67
+ cfg_file = config_file()
68
+ merged: dict[str, str] = {}
69
+ services: dict[str, dict[str, str]] = {}
70
+ if cfg_file.is_file():
71
+ cfg = configparser.ConfigParser(interpolation=None)
72
+ cfg.read(cfg_file)
73
+ key = "default" if profile == "default" else f"profile {profile}"
74
+ if cfg.has_section(key):
75
+ merged.update(dict(cfg.items(key)))
76
+ services_name = merged.get("services")
77
+ if services_name and cfg.has_section(f"services {services_name}"):
78
+ raw = dict(cfg.items(f"services {services_name}"))
79
+ for svc, block in raw.items():
80
+ parsed: dict[str, str] = {}
81
+ for line in block.splitlines():
82
+ stripped = line.strip()
83
+ if "=" in stripped:
84
+ k, _, val = stripped.partition("=")
85
+ parsed[k.strip()] = val.strip()
86
+ services[svc] = parsed
87
+ return merged, services
88
+
89
+
90
+ @functools.cache
91
+ def load_aws_settings() -> AwsSettings:
92
+ profile, services = _load_profile()
93
+ region = os.environ.get("AWS_REGION") or profile.get("region")
94
+ endpoint = (
95
+ os.environ.get(ENDPOINT_ENV_VAR)
96
+ or os.environ.get("AWS_ENDPOINT_URL")
97
+ or services.get(SERVICE_ID, {}).get("endpoint_url")
98
+ or profile.get("endpoint_url")
99
+ )
100
+ use_fips: bool | None = _env_bool("AWS_USE_FIPS_ENDPOINT")
101
+ if use_fips is None:
102
+ use_fips = _profile_bool(profile, "use_fips_endpoint")
103
+ if use_fips is None:
104
+ use_fips = False
105
+ use_dual_stack: bool | None = _env_bool("AWS_USE_DUALSTACK_ENDPOINT")
106
+ if use_dual_stack is None:
107
+ use_dual_stack = _profile_bool(profile, "use_dualstack_endpoint")
108
+ if use_dual_stack is None:
109
+ use_dual_stack = False
110
+ max_attempts_raw = os.environ.get("AWS_MAX_ATTEMPTS") or profile.get("max_attempts")
111
+ max_attempts = int(max_attempts_raw) if max_attempts_raw else 3
112
+ return AwsSettings(
113
+ region=region,
114
+ endpoint=endpoint,
115
+ use_fips=use_fips,
116
+ use_dual_stack=use_dual_stack,
117
+ max_attempts=max_attempts,
118
+ )
119
+
120
+
121
+ def aws_config() -> Interceptor[TInput, TOutput]:
122
+ def interceptor(
123
+ request: OperationRequest[TInput], next: NextFn[TInput, TOutput]
124
+ ) -> OperationResponse[TOutput]:
125
+ settings = load_aws_settings()
126
+ options = request.options
127
+ if options.region is None:
128
+ options.region = settings.region
129
+ if options.endpoint is None:
130
+ options.endpoint = settings.endpoint
131
+ if options.use_fips is None:
132
+ options.use_fips = settings.use_fips
133
+ if options.use_dual_stack is None:
134
+ options.use_dual_stack = settings.use_dual_stack
135
+ if options.retry_max_attempts is None:
136
+ options.retry_max_attempts = settings.max_attempts
137
+ return next(request)
138
+
139
+ return interceptor
140
+
141
+
142
+ def aaws_config() -> AsyncInterceptor[TInput, TOutput]:
143
+ async def interceptor(
144
+ request: AsyncOperationRequest[TInput], next: AsyncNextFn[TInput, TOutput]
145
+ ) -> AsyncOperationResponse[TOutput]:
146
+ settings = load_aws_settings()
147
+ options = request.options
148
+ if options.region is None:
149
+ options.region = settings.region
150
+ if options.endpoint is None:
151
+ options.endpoint = settings.endpoint
152
+ if options.use_fips is None:
153
+ options.use_fips = settings.use_fips
154
+ if options.use_dual_stack is None:
155
+ options.use_dual_stack = settings.use_dual_stack
156
+ if options.retry_max_attempts is None:
157
+ options.retry_max_attempts = settings.max_attempts
158
+ return await next(request)
159
+
160
+ return interceptor
@@ -0,0 +1,198 @@
1
+ from __future__ import annotations
2
+
3
+ import random
4
+ import time
5
+ from collections.abc import Sequence
6
+ from dataclasses import dataclass
7
+ from typing import TYPE_CHECKING, Awaitable, Callable, Generic, TypeVar
8
+
9
+ from zapros import (
10
+ AsyncClient,
11
+ Client,
12
+ ConnectionError,
13
+ Response,
14
+ SSLError,
15
+ TimeoutError,
16
+ )
17
+
18
+ from capo_codeguruprofiler._async import anysleep
19
+ from capo_codeguruprofiler.errors import ServiceError
20
+
21
+ if TYPE_CHECKING:
22
+ from capo_codeguruprofiler._auth._identity import Credentials
23
+ from capo_codeguruprofiler._auth._providers import IdentityProvider
24
+
25
+ TInput = TypeVar("TInput")
26
+ TOutput = TypeVar("TOutput")
27
+
28
+
29
+ @dataclass
30
+ class OperationOptions:
31
+ client: Client
32
+ use_dual_stack: bool | None = None
33
+ use_fips: bool | None = None
34
+ region: str | None = None
35
+ endpoint: str | None = None
36
+ retry_max_attempts: int | None = None
37
+ credentials_provider: IdentityProvider[Credentials] | None = None
38
+
39
+
40
+ @dataclass
41
+ class AsyncOperationOptions:
42
+ client: AsyncClient
43
+ use_dual_stack: bool | None = None
44
+ use_fips: bool | None = None
45
+ region: str | None = None
46
+ endpoint: str | None = None
47
+ retry_max_attempts: int | None = None
48
+ credentials_provider: IdentityProvider[Credentials] | None = None
49
+
50
+
51
+ @dataclass
52
+ class OperationRequest(Generic[TInput]):
53
+ input: TInput
54
+ options: OperationOptions
55
+
56
+
57
+ @dataclass
58
+ class AsyncOperationRequest(Generic[TInput]):
59
+ input: TInput
60
+ options: AsyncOperationOptions
61
+
62
+
63
+ @dataclass
64
+ class OperationResponse(Generic[TOutput]):
65
+ output: TOutput
66
+ response: Response
67
+
68
+
69
+ @dataclass
70
+ class AsyncOperationResponse(Generic[TOutput]):
71
+ output: TOutput
72
+ response: Response
73
+
74
+
75
+ NextFn = Callable[
76
+ [OperationRequest[TInput]],
77
+ OperationResponse[TOutput],
78
+ ]
79
+
80
+ AsyncNextFn = Callable[
81
+ [AsyncOperationRequest[TInput]],
82
+ Awaitable[AsyncOperationResponse[TOutput]],
83
+ ]
84
+
85
+ Interceptor = Callable[
86
+ [OperationRequest[TInput], NextFn[TInput, TOutput]],
87
+ OperationResponse[TOutput],
88
+ ]
89
+
90
+ AsyncInterceptor = Callable[
91
+ [AsyncOperationRequest[TInput], AsyncNextFn[TInput, TOutput]],
92
+ Awaitable[AsyncOperationResponse[TOutput]],
93
+ ]
94
+
95
+
96
+ def execute_pipeline(
97
+ request: OperationRequest[TInput],
98
+ handler: NextFn[TInput, TOutput],
99
+ interceptors: Sequence[Interceptor[TInput, TOutput]],
100
+ ) -> OperationResponse[TOutput]:
101
+ def make_chain(index: int) -> NextFn[TInput, TOutput]:
102
+ if index < len(interceptors):
103
+ interceptor = interceptors[index]
104
+
105
+ def next_fn(req: OperationRequest[TInput]) -> OperationResponse[TOutput]:
106
+ return interceptor(req, make_chain(index + 1))
107
+
108
+ return next_fn
109
+ return handler
110
+
111
+ return make_chain(0)(request)
112
+
113
+
114
+ async def aexecute_pipeline(
115
+ request: AsyncOperationRequest[TInput],
116
+ handler: AsyncNextFn[TInput, TOutput],
117
+ interceptors: Sequence[AsyncInterceptor[TInput, TOutput]],
118
+ ) -> AsyncOperationResponse[TOutput]:
119
+ def make_chain(index: int) -> AsyncNextFn[TInput, TOutput]:
120
+ if index < len(interceptors):
121
+ interceptor = interceptors[index]
122
+
123
+ async def next_fn(
124
+ req: AsyncOperationRequest[TInput],
125
+ ) -> AsyncOperationResponse[TOutput]:
126
+ return await interceptor(req, make_chain(index + 1))
127
+
128
+ return next_fn
129
+ return handler
130
+
131
+ return await make_chain(0)(request)
132
+
133
+
134
+ def _is_retryable(exc: Exception) -> bool:
135
+ if isinstance(exc, ServiceError):
136
+ return exc.is_retryable
137
+ if isinstance(exc, SSLError):
138
+ return False
139
+ if isinstance(exc, (ConnectionError, TimeoutError)):
140
+ return True
141
+ return False
142
+
143
+
144
+ def _retry_delay(attempt: int, is_throttling: bool) -> float:
145
+ base = 1.0 if is_throttling else 0.5
146
+ delay = base * (2.0 ** min(attempt - 1, 10))
147
+ delay = min(20.0, delay)
148
+ return random.uniform(0.0, delay)
149
+
150
+
151
+ def retry() -> Interceptor[TInput, TOutput]:
152
+ def interceptor(
153
+ request: OperationRequest[TInput], next: NextFn[TInput, TOutput]
154
+ ) -> OperationResponse[TOutput]:
155
+ max_attempts = request.options.retry_max_attempts or 3
156
+ last_exc: Exception | None = None
157
+ for attempt in range(1, max_attempts + 1):
158
+ try:
159
+ return next(request)
160
+ except Exception as exc:
161
+ if not _is_retryable(exc):
162
+ raise
163
+ last_exc = exc
164
+ if attempt < max_attempts:
165
+ is_throttling = (
166
+ isinstance(exc, ServiceError) and exc.is_throttling_error
167
+ )
168
+ time.sleep(_retry_delay(attempt, is_throttling))
169
+
170
+ assert last_exc
171
+ raise last_exc
172
+
173
+ return interceptor
174
+
175
+
176
+ def aretry() -> AsyncInterceptor[TInput, TOutput]:
177
+ async def interceptor(
178
+ request: AsyncOperationRequest[TInput], next: AsyncNextFn[TInput, TOutput]
179
+ ) -> AsyncOperationResponse[TOutput]:
180
+ max_attempts = request.options.retry_max_attempts or 3
181
+ last_exc: Exception | None = None
182
+ for attempt in range(1, max_attempts + 1):
183
+ try:
184
+ return await next(request)
185
+ except Exception as exc:
186
+ if not _is_retryable(exc):
187
+ raise
188
+ last_exc = exc
189
+ if attempt < max_attempts:
190
+ is_throttling = (
191
+ isinstance(exc, ServiceError) and exc.is_throttling_error
192
+ )
193
+ await anysleep(_retry_delay(attempt, is_throttling))
194
+
195
+ assert last_exc
196
+ raise last_exc
197
+
198
+ return interceptor