|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import Dict, List |
| 5 | +from typing import Dict, List, Mapping, cast |
6 | 6 |
|
7 | 7 | import httpx |
8 | 8 |
|
|
25 | 25 | ) |
26 | 26 | from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven, FileTypes |
27 | 27 | from ..._utils import ( |
| 28 | + extract_files, |
28 | 29 | maybe_transform, |
| 30 | + deepcopy_minimal, |
29 | 31 | async_maybe_transform, |
30 | 32 | ) |
31 | 33 | from ..._compat import cached_property |
@@ -393,15 +395,21 @@ def upload_file( |
393 | 395 | """ |
394 | 396 | if not id: |
395 | 397 | raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") |
| 398 | + body = deepcopy_minimal( |
| 399 | + { |
| 400 | + "file": file, |
| 401 | + "path": path, |
| 402 | + } |
| 403 | + ) |
| 404 | + files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) |
| 405 | + # It should be noted that the actual Content-Type header that will be |
| 406 | + # sent to the server will contain a `boundary` parameter, e.g. |
| 407 | + # multipart/form-data; boundary=---abc-- |
| 408 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
396 | 409 | return self._post( |
397 | 410 | f"/v1/devboxes/{id}/upload_file", |
398 | | - body=maybe_transform( |
399 | | - { |
400 | | - "file": file, |
401 | | - "path": path, |
402 | | - }, |
403 | | - devbox_upload_file_params.DevboxUploadFileParams, |
404 | | - ), |
| 411 | + body=maybe_transform(body, devbox_upload_file_params.DevboxUploadFileParams), |
| 412 | + files=files, |
405 | 413 | options=make_request_options( |
406 | 414 | extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
407 | 415 | ), |
@@ -804,15 +812,21 @@ async def upload_file( |
804 | 812 | """ |
805 | 813 | if not id: |
806 | 814 | raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") |
| 815 | + body = deepcopy_minimal( |
| 816 | + { |
| 817 | + "file": file, |
| 818 | + "path": path, |
| 819 | + } |
| 820 | + ) |
| 821 | + files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) |
| 822 | + # It should be noted that the actual Content-Type header that will be |
| 823 | + # sent to the server will contain a `boundary` parameter, e.g. |
| 824 | + # multipart/form-data; boundary=---abc-- |
| 825 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
807 | 826 | return await self._post( |
808 | 827 | f"/v1/devboxes/{id}/upload_file", |
809 | | - body=await async_maybe_transform( |
810 | | - { |
811 | | - "file": file, |
812 | | - "path": path, |
813 | | - }, |
814 | | - devbox_upload_file_params.DevboxUploadFileParams, |
815 | | - ), |
| 828 | + body=await async_maybe_transform(body, devbox_upload_file_params.DevboxUploadFileParams), |
| 829 | + files=files, |
816 | 830 | options=make_request_options( |
817 | 831 | extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
818 | 832 | ), |
|
0 commit comments