|
1 | | -import json |
2 | 1 | from collections.abc import AsyncIterator, Iterator |
3 | 2 | from typing import Self |
4 | 3 | from urllib.parse import urljoin |
@@ -113,73 +112,146 @@ def create( |
113 | 112 | return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
114 | 113 |
|
115 | 114 |
|
116 | | -class CreateWithIconMixin[Model]: |
117 | | - """Create resource with icon mixin.""" |
| 115 | +class CreateFileMixin[Model]: |
| 116 | + """Create file mixin.""" |
118 | 117 |
|
119 | | - def create( |
| 118 | + def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model: # noqa: WPS110 |
| 119 | + """Create logo. |
| 120 | +
|
| 121 | + Create a file resource by specifying a file image. |
| 122 | +
|
| 123 | + Args: |
| 124 | + resource_data: Resource data. |
| 125 | + file: File image. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + Model: Created resource. |
| 129 | + """ |
| 130 | + files = {} |
| 131 | + |
| 132 | + if file: |
| 133 | + files[self._upload_file_key] = file # type: ignore[attr-defined] |
| 134 | + |
| 135 | + response = self.http_client.request( # type: ignore[attr-defined] |
| 136 | + "post", |
| 137 | + self.path, # type: ignore[attr-defined] |
| 138 | + json=resource_data, |
| 139 | + files=files, |
| 140 | + json_file_key=self._upload_data_key, # type: ignore[attr-defined] |
| 141 | + force_multipart=True, |
| 142 | + ) |
| 143 | + |
| 144 | + return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
| 145 | + |
| 146 | + |
| 147 | +class UpdateFileMixin[Model]: |
| 148 | + """Update file mixin.""" |
| 149 | + |
| 150 | + def update( |
120 | 151 | self, |
| 152 | + resource_id: str, |
121 | 153 | resource_data: ResourceData, |
122 | | - icon: FileTypes, |
123 | | - data_key: str, |
124 | | - icon_key: str, |
| 154 | + file: FileTypes | None = None, # noqa: WPS110 |
125 | 155 | ) -> Model: |
126 | | - """Create resource with icon. |
| 156 | + """Update file. |
| 157 | +
|
| 158 | + Update a file resource by specifying a file. |
127 | 159 |
|
128 | 160 | Args: |
| 161 | + resource_id: Resource ID. |
129 | 162 | resource_data: Resource data. |
130 | | - data_key: Key for the resource data. |
131 | | - icon: Icon image in jpg, png, GIF, etc. |
132 | | - icon_key: Key for the icon. |
| 163 | + file: File image. |
133 | 164 |
|
134 | 165 | Returns: |
135 | | - Created resource. |
| 166 | + Model: Updated resource. |
136 | 167 | """ |
137 | | - files: dict[str, FileTypes] = {} |
138 | | - files[data_key] = ( |
139 | | - None, |
140 | | - json.dumps(resource_data), |
141 | | - APPLICATION_JSON, |
| 168 | + files = {} |
| 169 | + |
| 170 | + url = urljoin(f"{self.path}/", resource_id) # type: ignore[attr-defined] |
| 171 | + |
| 172 | + if file: |
| 173 | + files[self._upload_file_key] = file # type: ignore[attr-defined] |
| 174 | + |
| 175 | + response = self.http_client.request( # type: ignore[attr-defined] |
| 176 | + "put", |
| 177 | + url, |
| 178 | + json=resource_data, |
| 179 | + files=files, |
| 180 | + json_file_key=self._upload_data_key, # type: ignore[attr-defined] |
| 181 | + force_multipart=True, |
142 | 182 | ) |
143 | | - files[icon_key] = icon |
144 | | - response = self.http_client.request("post", self.path, files=files) # type: ignore[attr-defined] |
145 | 183 |
|
146 | 184 | return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
147 | 185 |
|
148 | 186 |
|
149 | | -class UpdateWithIconMixin[Model]: |
150 | | - """Update resource with icon mixin.""" |
| 187 | +class AsyncCreateFileMixin[Model]: |
| 188 | + """Asynchronous Create file mixin.""" |
151 | 189 |
|
152 | | - def update( |
| 190 | + async def create(self, resource_data: ResourceData, file: FileTypes | None = None) -> Model: # noqa: WPS110 |
| 191 | + """Create file. |
| 192 | +
|
| 193 | + Create a file resource by specifying a file. |
| 194 | +
|
| 195 | + Args: |
| 196 | + resource_data: Resource data. |
| 197 | + file: File image. |
| 198 | +
|
| 199 | + Returns: |
| 200 | + Model: Created resource. |
| 201 | + """ |
| 202 | + files = {} |
| 203 | + |
| 204 | + if file: |
| 205 | + files[self._upload_file_key] = file # type: ignore[attr-defined] |
| 206 | + |
| 207 | + response = await self.http_client.request( # type: ignore[attr-defined] |
| 208 | + "post", |
| 209 | + self.path, # type: ignore[attr-defined] |
| 210 | + json=resource_data, |
| 211 | + files=files, |
| 212 | + json_file_key=self._upload_data_key, # type: ignore[attr-defined] |
| 213 | + force_multipart=True, |
| 214 | + ) |
| 215 | + |
| 216 | + return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
| 217 | + |
| 218 | + |
| 219 | +class AsyncUpdateFileMixin[Model]: |
| 220 | + """Asynchronous Update file mixin.""" |
| 221 | + |
| 222 | + async def update( |
153 | 223 | self, |
154 | 224 | resource_id: str, |
155 | 225 | resource_data: ResourceData, |
156 | | - icon: FileTypes, |
157 | | - data_key: str, |
158 | | - icon_key: str, |
| 226 | + file: FileTypes | None = None, # noqa: WPS110 |
159 | 227 | ) -> Model: |
160 | | - """Update resource with icon. |
| 228 | + """Update file. |
| 229 | +
|
| 230 | + Update a file resource by specifying a file. |
161 | 231 |
|
162 | 232 | Args: |
163 | 233 | resource_id: Resource ID. |
164 | 234 | resource_data: Resource data. |
165 | | - data_key: Key for the resource data. |
166 | | - icon: Icon image in jpg, png, GIF, etc. |
167 | | - icon_key: Key for the icon. |
| 235 | + file: File image. |
168 | 236 |
|
169 | 237 | Returns: |
170 | | - Updated resource. |
| 238 | + Model: Updated resource. |
171 | 239 | """ |
172 | | - files: dict[str, FileTypes] = {} |
173 | | - files[data_key] = ( |
174 | | - None, |
175 | | - json.dumps(resource_data), |
176 | | - APPLICATION_JSON, |
177 | | - ) |
178 | | - files[icon_key] = icon |
| 240 | + files = {} |
179 | 241 |
|
180 | 242 | url = urljoin(f"{self.path}/", resource_id) # type: ignore[attr-defined] |
181 | 243 |
|
182 | | - response = self.http_client.request("put", url, files=files) # type: ignore[attr-defined] |
| 244 | + if file: |
| 245 | + files[self._upload_file_key] = file # type: ignore[attr-defined] |
| 246 | + |
| 247 | + response = await self.http_client.request( # type: ignore[attr-defined] |
| 248 | + "put", |
| 249 | + url, |
| 250 | + json=resource_data, |
| 251 | + files=files, |
| 252 | + json_file_key=self._upload_data_key, # type: ignore[attr-defined] |
| 253 | + force_multipart=True, |
| 254 | + ) |
183 | 255 |
|
184 | 256 | return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
185 | 257 |
|
@@ -286,77 +358,6 @@ async def create( |
286 | 358 | return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
287 | 359 |
|
288 | 360 |
|
289 | | -class AsyncCreateWithIconMixin[Model]: |
290 | | - """Create resource with icon mixin.""" |
291 | | - |
292 | | - async def create( |
293 | | - self, |
294 | | - resource_data: ResourceData, |
295 | | - icon: FileTypes, |
296 | | - data_key: str, |
297 | | - icon_key: str, |
298 | | - ) -> Model: |
299 | | - """Create resource with icon. |
300 | | -
|
301 | | - Args: |
302 | | - resource_data: Resource data. |
303 | | - data_key: Key for the resource data. |
304 | | - icon: Icon image in jpg, png, GIF, etc. |
305 | | - icon_key: Key for the icon. |
306 | | -
|
307 | | - Returns: |
308 | | - Created resource. |
309 | | - """ |
310 | | - files: dict[str, FileTypes] = {} |
311 | | - files[data_key] = ( |
312 | | - None, |
313 | | - json.dumps(resource_data), |
314 | | - APPLICATION_JSON, |
315 | | - ) |
316 | | - files[icon_key] = icon |
317 | | - response = await self.http_client.request("post", self.path, files=files) # type: ignore[attr-defined] |
318 | | - |
319 | | - return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
320 | | - |
321 | | - |
322 | | -class AsyncUpdateWithIconMixin[Model]: |
323 | | - """Update resource with icon mixin.""" |
324 | | - |
325 | | - async def update( |
326 | | - self, |
327 | | - resource_id: str, |
328 | | - resource_data: ResourceData, |
329 | | - icon: FileTypes, |
330 | | - data_key: str, |
331 | | - icon_key: str, |
332 | | - ) -> Model: |
333 | | - """Update resource with icon. |
334 | | -
|
335 | | - Args: |
336 | | - resource_id: Resource ID. |
337 | | - resource_data: Resource data. |
338 | | - data_key: Key for the resource data. |
339 | | - icon: Icon image in jpg, png, GIF, etc. |
340 | | - icon_key: Key for the icon. |
341 | | -
|
342 | | - Returns: |
343 | | - Updated resource. |
344 | | - """ |
345 | | - files: dict[str, FileTypes] = {} |
346 | | - files[data_key] = ( |
347 | | - None, |
348 | | - json.dumps(resource_data), |
349 | | - APPLICATION_JSON, |
350 | | - ) |
351 | | - files[icon_key] = icon |
352 | | - |
353 | | - url = urljoin(f"{self.path}/", resource_id) # type: ignore[attr-defined] |
354 | | - |
355 | | - response = await self.http_client.request("put", url, files=files) # type: ignore[attr-defined] |
356 | | - |
357 | | - return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return] |
358 | | - |
359 | | - |
360 | 361 | class GetMixin[Model]: |
361 | 362 | """Get resource mixin.""" |
362 | 363 |
|
|
0 commit comments