|
| 1 | +from http import HTTPStatus |
| 2 | +from typing import Any, Dict, Optional, Union |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from ... import errors |
| 7 | +from ...client import Client |
| 8 | +from ...models.data_upload_params import DataUploadParams |
| 9 | +from ...models.data_upload_response import DataUploadResponse |
| 10 | +from ...models.error import Error |
| 11 | +from ...types import UNSET, Response, Unset |
| 12 | + |
| 13 | + |
| 14 | +def _get_kwargs( |
| 15 | + data_source_id: str, |
| 16 | + *, |
| 17 | + client: Client, |
| 18 | + json_body: DataUploadParams, |
| 19 | + preview: Union[Unset, None, bool] = UNSET, |
| 20 | + preview_rows: Union[Unset, None, int] = UNSET, |
| 21 | +) -> Dict[str, Any]: |
| 22 | + url = "{}/datasources/{dataSourceId}/data".format(client.base_url, dataSourceId=data_source_id) |
| 23 | + |
| 24 | + headers: Dict[str, str] = client.get_headers() |
| 25 | + cookies: Dict[str, Any] = client.get_cookies() |
| 26 | + |
| 27 | + params: Dict[str, Any] = {} |
| 28 | + params["preview"] = preview |
| 29 | + |
| 30 | + params["previewRows"] = preview_rows |
| 31 | + |
| 32 | + params = {k: v for k, v in params.items() if v is not UNSET and v is not None} |
| 33 | + |
| 34 | + json_json_body = json_body.to_dict() |
| 35 | + |
| 36 | + # Set the proxies if the client has proxies set. |
| 37 | + proxies = None |
| 38 | + if hasattr(client, "proxies") and client.proxies is not None: |
| 39 | + https_proxy = client.proxies.get("https") |
| 40 | + if https_proxy: |
| 41 | + proxies = https_proxy |
| 42 | + else: |
| 43 | + http_proxy = client.proxies.get("http") |
| 44 | + if http_proxy: |
| 45 | + proxies = http_proxy |
| 46 | + |
| 47 | + return { |
| 48 | + "method": "patch", |
| 49 | + "url": url, |
| 50 | + "headers": headers, |
| 51 | + "cookies": cookies, |
| 52 | + "timeout": client.get_timeout(), |
| 53 | + "proxies": proxies, |
| 54 | + "json": json_json_body, |
| 55 | + "params": params, |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Union[DataUploadResponse, Error]]: |
| 60 | + if response.status_code == HTTPStatus.OK: |
| 61 | + response_200 = DataUploadResponse.from_dict(response.json()) |
| 62 | + |
| 63 | + return response_200 |
| 64 | + if response.status_code == HTTPStatus.BAD_REQUEST: |
| 65 | + response_400 = Error.from_dict(response.json()) |
| 66 | + |
| 67 | + return response_400 |
| 68 | + if response.status_code == HTTPStatus.FORBIDDEN: |
| 69 | + response_403 = Error.from_dict(response.json()) |
| 70 | + |
| 71 | + return response_403 |
| 72 | + if response.status_code == HTTPStatus.NOT_FOUND: |
| 73 | + response_404 = Error.from_dict(response.json()) |
| 74 | + |
| 75 | + return response_404 |
| 76 | + if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY: |
| 77 | + response_422 = Error.from_dict(response.json()) |
| 78 | + |
| 79 | + return response_422 |
| 80 | + if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: |
| 81 | + response_500 = Error.from_dict(response.json()) |
| 82 | + |
| 83 | + return response_500 |
| 84 | + if client.raise_on_unexpected_status: |
| 85 | + raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code} ({response})") |
| 86 | + else: |
| 87 | + return None |
| 88 | + |
| 89 | + |
| 90 | +def _build_response(*, client: Client, response: httpx.Response) -> Response[Union[DataUploadResponse, Error]]: |
| 91 | + return Response( |
| 92 | + status_code=HTTPStatus(response.status_code), |
| 93 | + content=response.content, |
| 94 | + headers=response.headers, |
| 95 | + parsed=_parse_response(client=client, response=response), |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def sync_detailed( |
| 100 | + data_source_id: str, |
| 101 | + *, |
| 102 | + client: Client, |
| 103 | + json_body: DataUploadParams, |
| 104 | + preview: Union[Unset, None, bool] = UNSET, |
| 105 | + preview_rows: Union[Unset, None, int] = UNSET, |
| 106 | +) -> Response[Union[DataUploadResponse, Error]]: |
| 107 | + """Load data into a data source |
| 108 | +
|
| 109 | + Args: |
| 110 | + data_source_id (str): |
| 111 | + preview (Union[Unset, None, bool]): |
| 112 | + preview_rows (Union[Unset, None, int]): |
| 113 | + json_body (DataUploadParams): parameters used when uploading data to a data source |
| 114 | +
|
| 115 | + Raises: |
| 116 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 117 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + Response[Union[DataUploadResponse, Error]] |
| 121 | + """ |
| 122 | + |
| 123 | + kwargs = _get_kwargs( |
| 124 | + data_source_id=data_source_id, |
| 125 | + client=client, |
| 126 | + json_body=json_body, |
| 127 | + preview=preview, |
| 128 | + preview_rows=preview_rows, |
| 129 | + ) |
| 130 | + |
| 131 | + response = httpx.request( |
| 132 | + verify=client.verify_ssl, |
| 133 | + **kwargs, |
| 134 | + ) |
| 135 | + |
| 136 | + return _build_response(client=client, response=response) |
| 137 | + |
| 138 | + |
| 139 | +def sync( |
| 140 | + data_source_id: str, |
| 141 | + *, |
| 142 | + client: Client, |
| 143 | + json_body: DataUploadParams, |
| 144 | + preview: Union[Unset, None, bool] = UNSET, |
| 145 | + preview_rows: Union[Unset, None, int] = UNSET, |
| 146 | +) -> Optional[Union[DataUploadResponse, Error]]: |
| 147 | + """Load data into a data source |
| 148 | +
|
| 149 | + Args: |
| 150 | + data_source_id (str): |
| 151 | + preview (Union[Unset, None, bool]): |
| 152 | + preview_rows (Union[Unset, None, int]): |
| 153 | + json_body (DataUploadParams): parameters used when uploading data to a data source |
| 154 | +
|
| 155 | + Raises: |
| 156 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 157 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Response[Union[DataUploadResponse, Error]] |
| 161 | + """ |
| 162 | + |
| 163 | + return sync_detailed( |
| 164 | + data_source_id=data_source_id, |
| 165 | + client=client, |
| 166 | + json_body=json_body, |
| 167 | + preview=preview, |
| 168 | + preview_rows=preview_rows, |
| 169 | + ).parsed |
| 170 | + |
| 171 | + |
| 172 | +async def asyncio_detailed( |
| 173 | + data_source_id: str, |
| 174 | + *, |
| 175 | + client: Client, |
| 176 | + json_body: DataUploadParams, |
| 177 | + preview: Union[Unset, None, bool] = UNSET, |
| 178 | + preview_rows: Union[Unset, None, int] = UNSET, |
| 179 | +) -> Response[Union[DataUploadResponse, Error]]: |
| 180 | + """Load data into a data source |
| 181 | +
|
| 182 | + Args: |
| 183 | + data_source_id (str): |
| 184 | + preview (Union[Unset, None, bool]): |
| 185 | + preview_rows (Union[Unset, None, int]): |
| 186 | + json_body (DataUploadParams): parameters used when uploading data to a data source |
| 187 | +
|
| 188 | + Raises: |
| 189 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 190 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 191 | +
|
| 192 | + Returns: |
| 193 | + Response[Union[DataUploadResponse, Error]] |
| 194 | + """ |
| 195 | + |
| 196 | + kwargs = _get_kwargs( |
| 197 | + data_source_id=data_source_id, |
| 198 | + client=client, |
| 199 | + json_body=json_body, |
| 200 | + preview=preview, |
| 201 | + preview_rows=preview_rows, |
| 202 | + ) |
| 203 | + |
| 204 | + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: |
| 205 | + response = await _client.request(**kwargs) |
| 206 | + |
| 207 | + return _build_response(client=client, response=response) |
| 208 | + |
| 209 | + |
| 210 | +async def asyncio( |
| 211 | + data_source_id: str, |
| 212 | + *, |
| 213 | + client: Client, |
| 214 | + json_body: DataUploadParams, |
| 215 | + preview: Union[Unset, None, bool] = UNSET, |
| 216 | + preview_rows: Union[Unset, None, int] = UNSET, |
| 217 | +) -> Optional[Union[DataUploadResponse, Error]]: |
| 218 | + """Load data into a data source |
| 219 | +
|
| 220 | + Args: |
| 221 | + data_source_id (str): |
| 222 | + preview (Union[Unset, None, bool]): |
| 223 | + preview_rows (Union[Unset, None, int]): |
| 224 | + json_body (DataUploadParams): parameters used when uploading data to a data source |
| 225 | +
|
| 226 | + Raises: |
| 227 | + errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. |
| 228 | + httpx.TimeoutException: If the request takes longer than Client.timeout. |
| 229 | +
|
| 230 | + Returns: |
| 231 | + Response[Union[DataUploadResponse, Error]] |
| 232 | + """ |
| 233 | + |
| 234 | + return ( |
| 235 | + await asyncio_detailed( |
| 236 | + data_source_id=data_source_id, |
| 237 | + client=client, |
| 238 | + json_body=json_body, |
| 239 | + preview=preview, |
| 240 | + preview_rows=preview_rows, |
| 241 | + ) |
| 242 | + ).parsed |
0 commit comments