(
self,
hook_ctx: HookContext,
request: httpx.Request,
is_error_status_code: Callable[[int], bool],
stream: bool = False,
retry_config: Optional[Tuple[RetryConfig, List[str]]] = None,
)
| 237 | ) |
| 238 | |
| 239 | def do_request( |
| 240 | self, |
| 241 | hook_ctx: HookContext, |
| 242 | request: httpx.Request, |
| 243 | is_error_status_code: Callable[[int], bool], |
| 244 | stream: bool = False, |
| 245 | retry_config: Optional[Tuple[RetryConfig, List[str]]] = None, |
| 246 | ) -> httpx.Response: |
| 247 | client = self.sdk_configuration.client |
| 248 | logger = self.sdk_configuration.debug_logger |
| 249 | |
| 250 | hooks = self.sdk_configuration.__dict__["_hooks"] |
| 251 | |
| 252 | def do(): |
| 253 | http_res = None |
| 254 | try: |
| 255 | req = hooks.before_request(BeforeRequestContext(hook_ctx), request) |
| 256 | if "timeout" in request.extensions and "timeout" not in req.extensions: |
| 257 | req.extensions["timeout"] = request.extensions["timeout"] |
| 258 | logger.debug( |
| 259 | "Request:\nMethod: %s\nURL: %s\nHeaders: %s\nBody: %s", |
| 260 | req.method, |
| 261 | req.url, |
| 262 | req.headers, |
| 263 | get_body_content(req), |
| 264 | ) |
| 265 | |
| 266 | if client is None: |
| 267 | raise ValueError("client is required") |
| 268 | |
| 269 | http_res = client.send(req, stream=stream) |
| 270 | except Exception as e: |
| 271 | _, e = hooks.after_error(AfterErrorContext(hook_ctx), None, e) |
| 272 | if e is not None: |
| 273 | logger.debug("Request Exception", exc_info=True) |
| 274 | raise e |
| 275 | |
| 276 | if http_res is None: |
| 277 | logger.debug("Raising no response SDK error") |
| 278 | raise errors.NoResponseError("No response received") |
| 279 | |
| 280 | logger.debug( |
| 281 | "Response:\nStatus Code: %s\nURL: %s\nHeaders: %s\nBody: %s", |
| 282 | http_res.status_code, |
| 283 | http_res.url, |
| 284 | http_res.headers, |
| 285 | "<streaming response>" if stream else http_res.text, |
| 286 | ) |
| 287 | |
| 288 | return http_res |
| 289 | |
| 290 | if retry_config is not None: |
| 291 | http_res = utils.retry(do, utils.Retries(retry_config[0], retry_config[1])) |
| 292 | else: |
| 293 | http_res = do() |
| 294 | |
| 295 | if is_error_status_code(http_res.status_code): |
| 296 | result, err = hooks.after_error(AfterErrorContext(hook_ctx), http_res, None) |
no test coverage detected