|
| 1 | +from typing import Any, Dict, Optional, Union |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +from ...client import Client |
| 6 | +from ...models.conversion_params import ConversionParams |
| 7 | +from ...models.error import Error |
| 8 | +from ...models.file_conversion import FileConversion |
| 9 | +from ...types import Response |
| 10 | + |
| 11 | + |
| 12 | +def _get_kwargs( |
| 13 | + body: ConversionParams, |
| 14 | + *, |
| 15 | + client: Client, |
| 16 | +) -> Dict[str, Any]: |
| 17 | + url = "{}/file/conversion".format( |
| 18 | + client.base_url, |
| 19 | + ) # noqa: E501 |
| 20 | + |
| 21 | + headers: Dict[str, Any] = client.get_headers() |
| 22 | + cookies: Dict[str, Any] = client.get_cookies() |
| 23 | + |
| 24 | + return { |
| 25 | + "url": url, |
| 26 | + "headers": headers, |
| 27 | + "cookies": cookies, |
| 28 | + "timeout": client.get_timeout(), |
| 29 | + "content": body.model_dump_json(), |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +def _parse_response( |
| 34 | + *, response: httpx.Response |
| 35 | +) -> Optional[Union[FileConversion, Error]]: |
| 36 | + if response.status_code == 201: |
| 37 | + response_201 = FileConversion(**response.json()) |
| 38 | + return response_201 |
| 39 | + if response.status_code == 400: |
| 40 | + response_4XX = Error(**response.json()) |
| 41 | + return response_4XX |
| 42 | + if response.status_code == 500: |
| 43 | + response_5XX = Error(**response.json()) |
| 44 | + return response_5XX |
| 45 | + return Error(**response.json()) |
| 46 | + |
| 47 | + |
| 48 | +def _build_response( |
| 49 | + *, response: httpx.Response |
| 50 | +) -> Response[Optional[Union[FileConversion, Error]]]: |
| 51 | + return Response( |
| 52 | + status_code=response.status_code, |
| 53 | + content=response.content, |
| 54 | + headers=response.headers, |
| 55 | + parsed=_parse_response(response=response), |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def sync_detailed( |
| 60 | + body: ConversionParams, |
| 61 | + *, |
| 62 | + client: Client, |
| 63 | +) -> Response[Optional[Union[FileConversion, Error]]]: |
| 64 | + kwargs = _get_kwargs( |
| 65 | + body=body, |
| 66 | + client=client, |
| 67 | + ) |
| 68 | + |
| 69 | + response = httpx.post( |
| 70 | + verify=client.verify_ssl, |
| 71 | + **kwargs, |
| 72 | + ) |
| 73 | + |
| 74 | + return _build_response(response=response) |
| 75 | + |
| 76 | + |
| 77 | +def sync( |
| 78 | + body: ConversionParams, |
| 79 | + *, |
| 80 | + client: Client, |
| 81 | +) -> Optional[Union[FileConversion, Error]]: |
| 82 | + """This takes a HTTP multipart body with these fields in any order: |
| 83 | +
|
| 84 | + - The input and output format options (as JSON), name is 'body'. - The files to convert, in raw binary. Must supply filenames. |
| 85 | +
|
| 86 | + This starts a conversion job and returns the `id` of the operation. You can use the `id` returned from the request to get status information about the async operation from the `/async/operations/{id}` endpoint.""" # noqa: E501 |
| 87 | + |
| 88 | + return sync_detailed( |
| 89 | + body=body, |
| 90 | + client=client, |
| 91 | + ).parsed |
| 92 | + |
| 93 | + |
| 94 | +async def asyncio_detailed( |
| 95 | + body: ConversionParams, |
| 96 | + *, |
| 97 | + client: Client, |
| 98 | +) -> Response[Optional[Union[FileConversion, Error]]]: |
| 99 | + kwargs = _get_kwargs( |
| 100 | + body=body, |
| 101 | + client=client, |
| 102 | + ) |
| 103 | + |
| 104 | + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: |
| 105 | + response = await _client.post(**kwargs) |
| 106 | + |
| 107 | + return _build_response(response=response) |
| 108 | + |
| 109 | + |
| 110 | +async def asyncio( |
| 111 | + body: ConversionParams, |
| 112 | + *, |
| 113 | + client: Client, |
| 114 | +) -> Optional[Union[FileConversion, Error]]: |
| 115 | + """This takes a HTTP multipart body with these fields in any order: |
| 116 | +
|
| 117 | + - The input and output format options (as JSON), name is 'body'. - The files to convert, in raw binary. Must supply filenames. |
| 118 | +
|
| 119 | + This starts a conversion job and returns the `id` of the operation. You can use the `id` returned from the request to get status information about the async operation from the `/async/operations/{id}` endpoint.""" # noqa: E501 |
| 120 | + |
| 121 | + return ( |
| 122 | + await asyncio_detailed( |
| 123 | + body=body, |
| 124 | + client=client, |
| 125 | + ) |
| 126 | + ).parsed |
0 commit comments