|
| 1 | +from typing import Any, Dict, Optional, Union, cast |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +from ...client import Client |
| 6 | +from ...models.http_validation_error import HTTPValidationError |
| 7 | +from ...types import Response |
| 8 | + |
| 9 | + |
| 10 | +def _get_kwargs( |
| 11 | + *, |
| 12 | + client: Client, |
| 13 | + json_body: str, |
| 14 | +) -> Dict[str, Any]: |
| 15 | + url = "{}/tests/json_body/string".format(client.base_url) |
| 16 | + |
| 17 | + headers: Dict[str, Any] = client.get_headers() |
| 18 | + cookies: Dict[str, Any] = client.get_cookies() |
| 19 | + |
| 20 | + json_json_body = json_body |
| 21 | + |
| 22 | + return { |
| 23 | + "url": url, |
| 24 | + "headers": headers, |
| 25 | + "cookies": cookies, |
| 26 | + "timeout": client.get_timeout(), |
| 27 | + "json": json_json_body, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]: |
| 32 | + if response.status_code == 200: |
| 33 | + response_200 = cast(str, response.json()) |
| 34 | + return response_200 |
| 35 | + if response.status_code == 422: |
| 36 | + response_422 = HTTPValidationError.from_dict(response.json()) |
| 37 | + |
| 38 | + return response_422 |
| 39 | + return None |
| 40 | + |
| 41 | + |
| 42 | +def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: |
| 43 | + return Response( |
| 44 | + status_code=response.status_code, |
| 45 | + content=response.content, |
| 46 | + headers=response.headers, |
| 47 | + parsed=_parse_response(response=response), |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def sync_detailed( |
| 52 | + *, |
| 53 | + client: Client, |
| 54 | + json_body: str, |
| 55 | +) -> Response[Union[HTTPValidationError, str]]: |
| 56 | + kwargs = _get_kwargs( |
| 57 | + client=client, |
| 58 | + json_body=json_body, |
| 59 | + ) |
| 60 | + |
| 61 | + response = httpx.post( |
| 62 | + verify=client.verify_ssl, |
| 63 | + **kwargs, |
| 64 | + ) |
| 65 | + |
| 66 | + return _build_response(response=response) |
| 67 | + |
| 68 | + |
| 69 | +def sync( |
| 70 | + *, |
| 71 | + client: Client, |
| 72 | + json_body: str, |
| 73 | +) -> Optional[Union[HTTPValidationError, str]]: |
| 74 | + """ """ |
| 75 | + |
| 76 | + return sync_detailed( |
| 77 | + client=client, |
| 78 | + json_body=json_body, |
| 79 | + ).parsed |
| 80 | + |
| 81 | + |
| 82 | +async def asyncio_detailed( |
| 83 | + *, |
| 84 | + client: Client, |
| 85 | + json_body: str, |
| 86 | +) -> Response[Union[HTTPValidationError, str]]: |
| 87 | + kwargs = _get_kwargs( |
| 88 | + client=client, |
| 89 | + json_body=json_body, |
| 90 | + ) |
| 91 | + |
| 92 | + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: |
| 93 | + response = await _client.post(**kwargs) |
| 94 | + |
| 95 | + return _build_response(response=response) |
| 96 | + |
| 97 | + |
| 98 | +async def asyncio( |
| 99 | + *, |
| 100 | + client: Client, |
| 101 | + json_body: str, |
| 102 | +) -> Optional[Union[HTTPValidationError, str]]: |
| 103 | + """ """ |
| 104 | + |
| 105 | + return ( |
| 106 | + await asyncio_detailed( |
| 107 | + client=client, |
| 108 | + json_body=json_body, |
| 109 | + ) |
| 110 | + ).parsed |
0 commit comments