|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Any, Dict, Optional |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from ... import errors |
| 7 | +from ...client import Client |
| 8 | +from ...types import Response |
| 9 | + |
| 10 | + |
| 11 | +def _get_kwargs( |
| 12 | + *, |
| 13 | + client: Client, |
| 14 | +) -> Dict[str, Any]: |
| 15 | + url = "{}/tests/description-with-backslash".format(client.base_url) |
| 16 | + |
| 17 | + headers: Dict[str, str] = client.get_headers() |
| 18 | + cookies: Dict[str, Any] = client.get_cookies() |
| 19 | + |
| 20 | + return { |
| 21 | + "method": "get", |
| 22 | + "url": url, |
| 23 | + "headers": headers, |
| 24 | + "cookies": cookies, |
| 25 | + "timeout": client.get_timeout(), |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]: |
| 30 | + if response.status_code == HTTPStatus.OK: |
| 31 | + return None |
| 32 | + if client.raise_on_unexpected_status: |
| 33 | + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}") |
| 34 | + else: |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +def _build_response(*, client: Client, response: httpx.Response) -> Response[Any]: |
| 39 | + return Response( |
| 40 | + status_code=HTTPStatus(response.status_code), |
| 41 | + content=response.content, |
| 42 | + headers=response.headers, |
| 43 | + parsed=_parse_response(client=client, response=response), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def sync_detailed( |
| 48 | + *, |
| 49 | + client: Client, |
| 50 | +) -> Response[Any]: |
| 51 | + r""" Test description with \ |
| 52 | +
|
| 53 | + Test description with \ |
| 54 | +
|
| 55 | + Raises: |
| 56 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 57 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Response[Any] |
| 61 | + """ |
| 62 | + |
| 63 | + kwargs = _get_kwargs( |
| 64 | + client=client, |
| 65 | + ) |
| 66 | + |
| 67 | + response = httpx.request( |
| 68 | + verify=client.verify_ssl, |
| 69 | + **kwargs, |
| 70 | + ) |
| 71 | + |
| 72 | + return _build_response(client=client, response=response) |
| 73 | + |
| 74 | + |
| 75 | +async def asyncio_detailed( |
| 76 | + *, |
| 77 | + client: Client, |
| 78 | +) -> Response[Any]: |
| 79 | + r""" Test description with \ |
| 80 | +
|
| 81 | + Test description with \ |
| 82 | +
|
| 83 | + Raises: |
| 84 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 85 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + Response[Any] |
| 89 | + """ |
| 90 | + |
| 91 | + kwargs = _get_kwargs( |
| 92 | + client=client, |
| 93 | + ) |
| 94 | + |
| 95 | + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: |
| 96 | + response = await _client.request(**kwargs) |
| 97 | + |
| 98 | + return _build_response(client=client, response=response) |
0 commit comments