|
1 | | -import json |
| 1 | +from typing import override |
2 | 2 |
|
3 | 3 | from mpt_api_client.http import AsyncService, Service |
4 | 4 | from mpt_api_client.http.mixins import ( |
5 | 5 | AsyncCollectionMixin, |
6 | | - AsyncModifiableResourceMixin, |
| 6 | + AsyncCreateWithIconMixin, |
| 7 | + AsyncDeleteMixin, |
| 8 | + AsyncGetMixin, |
| 9 | + AsyncUpdateWithIconMixin, |
7 | 10 | CollectionMixin, |
8 | | - ModifiableResourceMixin, |
| 11 | + CreateWithIconMixin, |
| 12 | + DeleteMixin, |
| 13 | + GetMixin, |
| 14 | + UpdateWithIconMixin, |
9 | 15 | ) |
10 | 16 | from mpt_api_client.http.types import FileTypes |
11 | 17 | from mpt_api_client.models import Model, ResourceData |
@@ -56,38 +62,71 @@ class ProductsServiceConfig: |
56 | 62 |
|
57 | 63 |
|
58 | 64 | class ProductsService( |
| 65 | + CreateWithIconMixin[Product], |
| 66 | + UpdateWithIconMixin[Product], |
59 | 67 | PublishableMixin[Product], |
60 | | - ModifiableResourceMixin[Product], |
| 68 | + GetMixin[Product], |
| 69 | + DeleteMixin, |
61 | 70 | CollectionMixin[Product], |
62 | 71 | Service[Product], |
63 | 72 | ProductsServiceConfig, |
64 | 73 | ): |
65 | 74 | """Products service.""" |
66 | 75 |
|
| 76 | + @override |
67 | 77 | def create( |
68 | 78 | self, |
69 | 79 | resource_data: ResourceData, |
70 | 80 | icon: FileTypes, |
| 81 | + data_key: str = "product", |
| 82 | + icon_key: str = "icon", |
71 | 83 | ) -> Product: |
72 | 84 | """Create product with icon. |
73 | 85 |
|
74 | 86 | Args: |
75 | 87 | resource_data: Product data. |
76 | 88 | icon: Icon image in jpg, png, GIF, etc. |
| 89 | + data_key: Key for the product data. |
| 90 | + icon_key: Key for the icon. |
77 | 91 |
|
78 | 92 | Returns: |
79 | 93 | Created resource. |
80 | 94 | """ |
81 | | - files: dict[str, FileTypes] = {} |
82 | | - files["product"] = ( |
83 | | - None, |
84 | | - json.dumps(resource_data), |
85 | | - "application/json", |
| 95 | + return super().create( |
| 96 | + resource_data=resource_data, |
| 97 | + icon=icon, |
| 98 | + data_key=data_key, |
| 99 | + icon_key=icon_key, |
86 | 100 | ) |
87 | | - files["icon"] = icon |
88 | | - response = self.http_client.request("post", self.path, files=files) |
89 | 101 |
|
90 | | - return self._model_class.from_response(response) |
| 102 | + @override |
| 103 | + def update( |
| 104 | + self, |
| 105 | + resource_id: str, |
| 106 | + resource_data: ResourceData, |
| 107 | + icon: FileTypes, |
| 108 | + data_key: str = "product", |
| 109 | + icon_key: str = "icon", |
| 110 | + ) -> Product: |
| 111 | + """Update product with icon. |
| 112 | +
|
| 113 | + Args: |
| 114 | + resource_id: Product ID. |
| 115 | + resource_data: Product data. |
| 116 | + icon: Icon image in jpg, png, GIF, etc. |
| 117 | + data_key: Key for the product data. |
| 118 | + icon_key: Key for the icon. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + Updated resource. |
| 122 | + """ |
| 123 | + return super().update( |
| 124 | + resource_id=resource_id, |
| 125 | + resource_data=resource_data, |
| 126 | + icon=icon, |
| 127 | + data_key=data_key, |
| 128 | + icon_key=icon_key, |
| 129 | + ) |
91 | 130 |
|
92 | 131 | def item_groups(self, product_id: str) -> ItemGroupsService: |
93 | 132 | """Return item_groups service.""" |
@@ -135,37 +174,71 @@ def update_settings(self, product_id: str, settings: ResourceData) -> Product: |
135 | 174 |
|
136 | 175 |
|
137 | 176 | class AsyncProductsService( |
| 177 | + AsyncCreateWithIconMixin[Product], |
| 178 | + AsyncUpdateWithIconMixin[Product], |
138 | 179 | AsyncPublishableMixin[Product], |
139 | | - AsyncModifiableResourceMixin[Product], |
| 180 | + AsyncGetMixin[Product], |
| 181 | + AsyncDeleteMixin, |
140 | 182 | AsyncCollectionMixin[Product], |
141 | 183 | AsyncService[Product], |
142 | 184 | ProductsServiceConfig, |
143 | 185 | ): |
144 | 186 | """Products service.""" |
145 | 187 |
|
| 188 | + @override |
146 | 189 | async def create( |
147 | 190 | self, |
148 | 191 | resource_data: ResourceData, |
149 | 192 | icon: FileTypes, |
| 193 | + data_key: str = "product", |
| 194 | + icon_key: str = "icon", |
150 | 195 | ) -> Product: |
151 | 196 | """Create product with icon. |
152 | 197 |
|
153 | 198 | Args: |
154 | 199 | resource_data: Product data. |
155 | 200 | icon: Icon image in jpg, png, GIF, etc. |
| 201 | + data_key: Key for the product data. |
| 202 | + icon_key: Key for the icon. |
156 | 203 |
|
157 | 204 | Returns: |
158 | 205 | Created resource. |
159 | 206 | """ |
160 | | - files: dict[str, FileTypes] = {} |
161 | | - files["product"] = ( |
162 | | - None, |
163 | | - json.dumps(resource_data), |
164 | | - "application/json", |
165 | | - ) |
166 | | - files["icon"] = icon |
167 | | - response = await self.http_client.request("post", self.path, files=files) |
168 | | - return self._model_class.from_response(response) |
| 207 | + return await super().create( |
| 208 | + resource_data=resource_data, |
| 209 | + data_key=data_key, |
| 210 | + icon=icon, |
| 211 | + icon_key=icon_key, |
| 212 | + ) |
| 213 | + |
| 214 | + @override |
| 215 | + async def update( |
| 216 | + self, |
| 217 | + resource_id: str, |
| 218 | + resource_data: ResourceData, |
| 219 | + icon: FileTypes, |
| 220 | + data_key: str = "product", |
| 221 | + icon_key: str = "icon", |
| 222 | + ) -> Product: |
| 223 | + """Update product with icon. |
| 224 | +
|
| 225 | + Args: |
| 226 | + resource_id: Product ID. |
| 227 | + resource_data: Product data. |
| 228 | + icon: Icon image in jpg, png, GIF, etc. |
| 229 | + data_key: Key for the product data. |
| 230 | + icon_key: Key for the icon. |
| 231 | +
|
| 232 | + Returns: |
| 233 | + Updated resource. |
| 234 | + """ |
| 235 | + return await super().update( |
| 236 | + resource_id=resource_id, |
| 237 | + resource_data=resource_data, |
| 238 | + data_key=data_key, |
| 239 | + icon=icon, |
| 240 | + icon_key=icon_key, |
| 241 | + ) |
169 | 242 |
|
170 | 243 | def item_groups(self, product_id: str) -> AsyncItemGroupsService: |
171 | 244 | """Return item_groups service.""" |
|
0 commit comments