-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_file_mixin.py
More file actions
83 lines (63 loc) · 2.3 KB
/
update_file_mixin.py
File metadata and controls
83 lines (63 loc) · 2.3 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from mpt_api_client.http.types import FileTypes
from mpt_api_client.http.url_utils import join_url_path
from mpt_api_client.models import ResourceData
class UpdateFileMixin[Model]:
"""Update file mixin."""
def update(
self,
resource_id: str,
resource_data: ResourceData,
file: FileTypes | None = None, # noqa: WPS110
) -> Model:
"""Update file.
Update a file resource by specifying a file.
Args:
resource_id: Resource ID.
resource_data: Resource data.
file: File image.
Returns:
Model: Updated resource.
"""
files = {}
url = join_url_path(self.path, resource_id) # type: ignore[attr-defined]
if file:
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = self.http_client.request( # type: ignore[attr-defined]
"put",
url,
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 AsyncUpdateFileMixin[Model]:
"""Asynchronous Update file mixin."""
async def update(
self,
resource_id: str,
resource_data: ResourceData,
file: FileTypes | None = None, # noqa: WPS110
) -> Model:
"""Update file.
Update a file resource by specifying a file.
Args:
resource_id: Resource ID.
resource_data: Resource data.
file: File image.
Returns:
Model: Updated resource.
"""
files = {}
url = join_url_path(self.path, resource_id) # type: ignore[attr-defined]
if file:
files[self._upload_file_key] = file # type: ignore[attr-defined]
response = await self.http_client.request( # type: ignore[attr-defined]
"put",
url,
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]