|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# pyformat: disable |
| 16 | + |
| 17 | +"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.""" |
| 18 | + |
| 19 | +from .asynctypes import ( |
| 20 | + AsyncAfterErrorHook, |
| 21 | + AsyncAfterParseErrorHook, |
| 22 | + AsyncAfterSuccessHook, |
| 23 | + AsyncBeforeRequestHook, |
| 24 | +) |
| 25 | +from .types import ( |
| 26 | + AfterErrorContext, |
| 27 | + AfterErrorHook, |
| 28 | + AfterParseErrorContext, |
| 29 | + AfterParseErrorHook, |
| 30 | + AfterSuccessContext, |
| 31 | + AfterSuccessHook, |
| 32 | + BeforeRequestContext, |
| 33 | + BeforeRequestHook, |
| 34 | +) |
| 35 | +import asyncio |
| 36 | +import httpx |
| 37 | +from typing import Optional, Tuple, Union |
| 38 | + |
| 39 | +# ============================================================================ |
| 40 | +# Sync to Async Adapters |
| 41 | +# ============================================================================ |
| 42 | +# PERFORMANCE CONSIDERATIONS: |
| 43 | +# - Sync-to-Async adapters use asyncio.to_thread() / run_in_executor() to run |
| 44 | +# sync hooks in a thread pool. This adds threading overhead and context |
| 45 | +# switching costs. |
| 46 | +# - Async-to-Sync adapters use asyncio.run() which creates a new event loop |
| 47 | +# for each invocation. This has significant overhead. |
| 48 | +# - For optimal performance, register hooks that match the execution context: |
| 49 | +# * Use native async hooks (AsyncBeforeRequestHook, etc.) for async methods |
| 50 | +# * Use native sync hooks (BeforeRequestHook, etc.) for sync methods |
| 51 | +# - Adapters are provided for convenience and backward compatibility, but |
| 52 | +# native implementations are strongly recommended for production use. |
| 53 | +# ============================================================================ |
| 54 | + |
| 55 | + |
| 56 | +class SyncToAsyncBeforeRequestAdapter(AsyncBeforeRequestHook): |
| 57 | + """Adapts a synchronous BeforeRequestHook to work in async contexts. |
| 58 | +
|
| 59 | + PERFORMANCE WARNING: Uses asyncio.to_thread() to run the sync hook in a thread pool. |
| 60 | + This introduces: |
| 61 | + - Thread pool overhead for spawning/managing worker threads |
| 62 | + - Context switching between the async event loop and worker threads |
| 63 | + - Memory overhead for thread-local storage |
| 64 | + - Potential thread pool exhaustion under high concurrency |
| 65 | +
|
| 66 | + For performance-critical applications, prefer registering native AsyncBeforeRequestHook |
| 67 | + implementations instead of relying on this adapter. |
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, sync_hook: BeforeRequestHook): |
| 71 | + self._sync_hook = sync_hook |
| 72 | + |
| 73 | + async def before_request( |
| 74 | + self, hook_ctx: BeforeRequestContext, request: httpx.Request |
| 75 | + ) -> Union[httpx.Request, Exception]: |
| 76 | + return await asyncio.to_thread( |
| 77 | + self._sync_hook.before_request, hook_ctx, request |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +class SyncToAsyncAfterSuccessAdapter(AsyncAfterSuccessHook): |
| 82 | + """Adapts a synchronous AfterSuccessHook to work in async contexts. |
| 83 | +
|
| 84 | + PERFORMANCE WARNING: Uses asyncio.to_thread() to run the sync hook in a thread pool. |
| 85 | + This introduces threading overhead and context switching costs. Prefer native |
| 86 | + AsyncAfterSuccessHook implementations for better performance. |
| 87 | + """ |
| 88 | + |
| 89 | + def __init__(self, sync_hook: AfterSuccessHook): |
| 90 | + self._sync_hook = sync_hook |
| 91 | + |
| 92 | + async def after_success( |
| 93 | + self, hook_ctx: AfterSuccessContext, response: httpx.Response |
| 94 | + ) -> Union[httpx.Response, Exception]: |
| 95 | + return await asyncio.to_thread( |
| 96 | + self._sync_hook.after_success, hook_ctx, response |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +class SyncToAsyncAfterErrorAdapter(AsyncAfterErrorHook): |
| 101 | + """Adapts a synchronous AfterErrorHook to work in async contexts. |
| 102 | +
|
| 103 | + PERFORMANCE WARNING: Uses asyncio.to_thread() to run the sync hook in a thread pool. |
| 104 | + This introduces threading overhead and context switching costs. Prefer native |
| 105 | + AsyncAfterErrorHook implementations for better performance. |
| 106 | + """ |
| 107 | + |
| 108 | + def __init__(self, sync_hook: AfterErrorHook): |
| 109 | + self._sync_hook = sync_hook |
| 110 | + |
| 111 | + async def after_error( |
| 112 | + self, |
| 113 | + hook_ctx: AfterErrorContext, |
| 114 | + response: Optional[httpx.Response], |
| 115 | + error: Optional[Exception], |
| 116 | + ) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]: |
| 117 | + return await asyncio.to_thread( |
| 118 | + self._sync_hook.after_error, hook_ctx, response, error |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +class SyncToAsyncAfterParseErrorAdapter(AsyncAfterParseErrorHook): |
| 123 | + """Adapts a synchronous AfterParseErrorHook to work in async contexts.""" |
| 124 | + |
| 125 | + def __init__(self, sync_hook: AfterParseErrorHook): |
| 126 | + self._sync_hook = sync_hook |
| 127 | + |
| 128 | + async def after_parse_error( |
| 129 | + self, |
| 130 | + hook_ctx: AfterParseErrorContext, |
| 131 | + response: httpx.Response, |
| 132 | + error: Exception, |
| 133 | + ) -> Exception: |
| 134 | + return await asyncio.to_thread( |
| 135 | + self._sync_hook.after_parse_error, hook_ctx, response, error |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +# ============================================================================ |
| 140 | +# Async to Sync Adapters |
| 141 | +# ============================================================================ |
| 142 | + |
| 143 | + |
| 144 | +class AsyncToSyncBeforeRequestAdapter(BeforeRequestHook): |
| 145 | + """Adapts an async BeforeRequestHook to work in sync contexts. |
| 146 | +
|
| 147 | + PERFORMANCE WARNING: Uses asyncio.run() which creates and tears down a new event |
| 148 | + loop for each invocation. This has significant overhead: |
| 149 | + - Event loop creation/destruction costs |
| 150 | + - Loss of connection pooling and keep-alive benefits |
| 151 | + - Cannot leverage existing event loop infrastructure |
| 152 | + - Not suitable for high-frequency operations |
| 153 | +
|
| 154 | + This adapter is primarily for compatibility in mixed sync/async codebases. |
| 155 | + Strongly prefer native sync hooks (BeforeRequestHook) for production use. |
| 156 | + """ |
| 157 | + |
| 158 | + def __init__(self, async_hook: AsyncBeforeRequestHook): |
| 159 | + self._async_hook = async_hook |
| 160 | + |
| 161 | + def before_request( |
| 162 | + self, hook_ctx: BeforeRequestContext, request: httpx.Request |
| 163 | + ) -> Union[httpx.Request, Exception]: |
| 164 | + return asyncio.run(self._async_hook.before_request(hook_ctx, request)) |
| 165 | + |
| 166 | + |
| 167 | +class AsyncToSyncAfterSuccessAdapter(AfterSuccessHook): |
| 168 | + """Adapts an async AfterSuccessHook to work in sync contexts. |
| 169 | +
|
| 170 | + PERFORMANCE WARNING: Uses asyncio.run() which creates and tears down a new event |
| 171 | + loop for each invocation. This has significant overhead. Strongly prefer native |
| 172 | + sync hooks (AfterSuccessHook) for production use. |
| 173 | + """ |
| 174 | + |
| 175 | + def __init__(self, async_hook: AsyncAfterSuccessHook): |
| 176 | + self._async_hook = async_hook |
| 177 | + |
| 178 | + def after_success( |
| 179 | + self, hook_ctx: AfterSuccessContext, response: httpx.Response |
| 180 | + ) -> Union[httpx.Response, Exception]: |
| 181 | + return asyncio.run(self._async_hook.after_success(hook_ctx, response)) |
| 182 | + |
| 183 | + |
| 184 | +class AsyncToSyncAfterErrorAdapter(AfterErrorHook): |
| 185 | + """Adapts an async AfterErrorHook to work in sync contexts. |
| 186 | +
|
| 187 | + PERFORMANCE WARNING: Uses asyncio.run() which creates and tears down a new event |
| 188 | + loop for each invocation. This has significant overhead. Strongly prefer native |
| 189 | + sync hooks (AfterErrorHook) for production use. |
| 190 | + """ |
| 191 | + |
| 192 | + def __init__(self, async_hook: AsyncAfterErrorHook): |
| 193 | + self._async_hook = async_hook |
| 194 | + |
| 195 | + def after_error( |
| 196 | + self, |
| 197 | + hook_ctx: AfterErrorContext, |
| 198 | + response: Optional[httpx.Response], |
| 199 | + error: Optional[Exception], |
| 200 | + ) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]: |
| 201 | + return asyncio.run(self._async_hook.after_error(hook_ctx, response, error)) |
| 202 | + |
| 203 | + |
| 204 | +class AsyncToSyncAfterParseErrorAdapter(AfterParseErrorHook): |
| 205 | + """Adapts an async AfterParseErrorHook to work in sync contexts.""" |
| 206 | + |
| 207 | + def __init__(self, async_hook: AsyncAfterParseErrorHook): |
| 208 | + self._async_hook = async_hook |
| 209 | + |
| 210 | + def after_parse_error( |
| 211 | + self, |
| 212 | + hook_ctx: AfterParseErrorContext, |
| 213 | + response: httpx.Response, |
| 214 | + error: Exception, |
| 215 | + ) -> Exception: |
| 216 | + return asyncio.run( |
| 217 | + self._async_hook.after_parse_error(hook_ctx, response, error) |
| 218 | + ) |
0 commit comments