-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_file_mixin.py
More file actions
66 lines (48 loc) · 1.98 KB
/
create_file_mixin.py
File metadata and controls
66 lines (48 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from mpt_api_client.http.types import FileTypes
from mpt_api_client.models import ResourceData
class CreateFileMixin[Model]:
"""Create file mixin."""
def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model: # noqa: WPS110
"""Create logo.
Create a file resource by specifying a file image.
Args:
resource_data: Resource data.
file: File image.
Returns:
Model: Created resource.
"""
files = {}
if file:
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = self.http_client.request( # type: ignore[attr-defined]
"post",
self.path, # type: ignore[attr-defined]
json=resource_data,
files=files,
json_file_key=self._upload_data_key, # type: ignore[attr-defined]
force_multipart=True,
)
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
class AsyncCreateFileMixin[Model]:
"""Asynchronous Create file mixin."""
async def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model: # noqa: WPS110
"""Create file.
Create a file resource by specifying a file.
Args:
resource_data: Resource data.
file: File image.
Returns:
Model: Created resource.
"""
files = {}
if file:
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = await self.http_client.request( # type: ignore[attr-defined]
"post",
self.path, # type: ignore[attr-defined]
json=resource_data,
files=files,
json_file_key=self._upload_data_key, # type: ignore[attr-defined]
force_multipart=True,
)
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]