|
| 1 | +import json |
| 2 | +from typing import override |
| 3 | + |
| 4 | +from httpx import Response |
| 5 | +from httpx._types import FileTypes |
| 6 | + |
| 7 | +from mpt_api_client.http import AsyncService, CreateMixin, DeleteMixin, Service |
| 8 | +from mpt_api_client.http.mixins import ( |
| 9 | + AsyncCreateMixin, |
| 10 | + AsyncDeleteMixin, |
| 11 | + AsyncUpdateMixin, |
| 12 | + UpdateMixin, |
| 13 | +) |
| 14 | +from mpt_api_client.models import FileModel, Model, ResourceData |
| 15 | + |
| 16 | + |
| 17 | +def _json_to_file_payload(resource_data: ResourceData) -> bytes: |
| 18 | + return json.dumps( |
| 19 | + resource_data, ensure_ascii=False, separators=(",", ":"), allow_nan=False |
| 20 | + ).encode("utf-8") |
| 21 | + |
| 22 | + |
| 23 | +class Media(Model): |
| 24 | + """Media resource.""" |
| 25 | + |
| 26 | + |
| 27 | +class MediaServiceConfig: |
| 28 | + """Media service configuration.""" |
| 29 | + |
| 30 | + _endpoint = "/public/v1/catalog/products/{product_id}/media" |
| 31 | + _model_class = Media |
| 32 | + _collection_key = "data" |
| 33 | + |
| 34 | + |
| 35 | +class MediaService( |
| 36 | + CreateMixin[Media], |
| 37 | + DeleteMixin, |
| 38 | + UpdateMixin[Media], |
| 39 | + Service[Media], |
| 40 | + MediaServiceConfig, |
| 41 | +): |
| 42 | + """Media service.""" |
| 43 | + |
| 44 | + @override |
| 45 | + def create( |
| 46 | + self, |
| 47 | + resource_data: ResourceData | None = None, |
| 48 | + files: dict[str, FileTypes] | None = None, # noqa: WPS221 |
| 49 | + ) -> Media: |
| 50 | + """Create Media resource. |
| 51 | +
|
| 52 | + Currently are two types of media resources available image and video. |
| 53 | +
|
| 54 | + Video: |
| 55 | + resource_data: |
| 56 | + { |
| 57 | + "name": "SomeMediaFile", |
| 58 | + "description":"Some media description", |
| 59 | + "mediaType": "Video", |
| 60 | + "url": http://www.somemedia.com/somevideo.avi, |
| 61 | + "displayOrder": 1 |
| 62 | + } |
| 63 | + files: Add an image with the video thumbnail |
| 64 | +
|
| 65 | + Image: |
| 66 | + resource_data: |
| 67 | + { |
| 68 | + "name": "SomeMediaFile", |
| 69 | + "description":"Some media description", |
| 70 | + "mediaType": "Video", |
| 71 | + "displayOrder": 1 |
| 72 | + } |
| 73 | + files: The image itself |
| 74 | +
|
| 75 | + Args: |
| 76 | + resource_data: Resource data. |
| 77 | + files: Files data. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + Media resource. |
| 81 | + """ |
| 82 | + files = files or {} |
| 83 | + |
| 84 | + # Note: This is a workaround to fulfill MPT API request format |
| 85 | + # |
| 86 | + # HTTPx does not support sending json and files in the same call |
| 87 | + # currently only supports sending form-data and files in the same call. |
| 88 | + # https://www.python-httpx.org/quickstart/#sending-multipart-file-uploads |
| 89 | + # |
| 90 | + # MPT API expects files and data to be submitted in a multipart form-data upload. |
| 91 | + # |
| 92 | + # Current workaround is to send the json data as an unnamed file. |
| 93 | + # This ends adding the json as payload multipart data. |
| 94 | + # |
| 95 | + # json.dumps is setup using the same params of httpx json encoder to produce the same |
| 96 | + # encodings. |
| 97 | + |
| 98 | + if resource_data: |
| 99 | + files["_media_data"] = ( |
| 100 | + None, |
| 101 | + _json_to_file_payload(resource_data), |
| 102 | + "application/json", |
| 103 | + ) |
| 104 | + |
| 105 | + response = self.http_client.post(self.endpoint, files=files) |
| 106 | + response.raise_for_status() |
| 107 | + return Media.from_response(response) |
| 108 | + |
| 109 | + def download(self, media_id: str) -> FileModel: |
| 110 | + """Download the media file for the given media ID. |
| 111 | +
|
| 112 | + Args: |
| 113 | + media_id: Media ID. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Media file. |
| 117 | + """ |
| 118 | + response: Response = self._resource_do_request( |
| 119 | + media_id, method="GET", headers={"Accept": "*"} |
| 120 | + ) |
| 121 | + return FileModel(response) |
| 122 | + |
| 123 | + |
| 124 | +class AsyncMediaService( |
| 125 | + AsyncCreateMixin[Media], |
| 126 | + AsyncDeleteMixin, |
| 127 | + AsyncUpdateMixin[Media], |
| 128 | + AsyncService[Media], |
| 129 | + MediaServiceConfig, |
| 130 | +): |
| 131 | + """Media service.""" |
| 132 | + |
| 133 | + @override |
| 134 | + async def create( |
| 135 | + self, |
| 136 | + resource_data: ResourceData | None = None, |
| 137 | + files: dict[str, FileTypes] | None = None, # noqa: WPS221 |
| 138 | + ) -> Media: |
| 139 | + """Create Media resource. |
| 140 | +
|
| 141 | + Args: |
| 142 | + resource_data: Resource data. |
| 143 | + files: Files data. |
| 144 | +
|
| 145 | + Returns: |
| 146 | + Media resource. |
| 147 | + """ |
| 148 | + files = files or {} |
| 149 | + |
| 150 | + # Note: This is a workaround to fulfill MPT API request format |
| 151 | + # |
| 152 | + # HTTPx does not support sending json and files in the same call |
| 153 | + # currently only supports sending form-data and files in the same call. |
| 154 | + # https://www.python-httpx.org/quickstart/#sending-multipart-file-uploads |
| 155 | + # |
| 156 | + # MPT API expects files and data to be submitted in a multipart form-data upload. |
| 157 | + # |
| 158 | + # Current workaround is to send the json data as an unnamed file. |
| 159 | + # This ends adding the json as payload multipart data. |
| 160 | + # |
| 161 | + # json.dumps is setup using the same params of httpx json encoder to produce the same |
| 162 | + # encodings. |
| 163 | + |
| 164 | + if resource_data: |
| 165 | + files["_media_data"] = ( |
| 166 | + None, |
| 167 | + _json_to_file_payload(resource_data), |
| 168 | + "application/json", |
| 169 | + ) |
| 170 | + |
| 171 | + response = await self.http_client.post(self.endpoint, files=files) |
| 172 | + response.raise_for_status() |
| 173 | + return Media.from_response(response) |
| 174 | + |
| 175 | + async def download(self, media_id: str) -> FileModel: |
| 176 | + """Download the media file for the given media ID. |
| 177 | +
|
| 178 | + Args: |
| 179 | + media_id: Media ID. |
| 180 | +
|
| 181 | + Returns: |
| 182 | + Media file. |
| 183 | + """ |
| 184 | + response = await self._resource_do_request(media_id, method="GET", headers={"Accept": "*"}) |
| 185 | + return FileModel(response) |
0 commit comments