For the following Typespec definition about "multipart/form-data":
...
model MultiPartRequest {
id: string;
profileImage: bytes;
}
op basic(@header contentType: "multipart/form-data", @body body: MultiPartRequest): NoContentResponse;
Compared with content-type, the only difference is that we don't generate overload for IO since core can't handle IO which may contain IO, too.
@overload
def basic(self, body: JSON, **kwargs: Any) -> None:
...
@overload
def basic(self, body: _models.MultiPartRequest, **kwargs: Any) -> None:
...
@distributed_trace
def basic(self, body: Union[_models.MultiPartRequest, JSON], **kwargs: Any) -> None:
...Users could use SDK with the following ways:
...
with open("image.png", "rb") as file:
clent.basic({"id": "123", "profileImage": file})
# or
# clent.basic({"id": "123", "profileImage": file.read()})
def multipart_form_data_file(file: Union[IOBase, bytes]) -> Union[IOBase, Tuple[str, bytes, str]]:
if isinstance(file, IOBase):
return file
return (str(time.time()), file, "application/octet-stream")
def basic(self, body: Union[_models.MultiPartRequest, JSON], **kwargs: Any) -> None:
...
_files = {k: multipart_form_data_file(v) for k, v in body.items() if isinstance(v, (IOBase, bytes))}
_data = {k: v for k, v in body.items() if not isinstance(v, (IOBase, bytes))}
_request = build_multi_part_basic_request(
data=_data,
files=_files,
headers=_headers,
params=_params,
)
...nit: I make a test locally and it passes with up logic and we don't need change for current azure-core version.
Why use
time.time()as the (file) name? There is a very high probability of name conflicts.