|
| 1 | +import json |
| 2 | + |
1 | 3 | from mpt_api_client.http import AsyncService, Service |
2 | 4 | from mpt_api_client.http.mixins import ( |
3 | 5 | AsyncCollectionMixin, |
4 | | - AsyncManagedResourceMixin, |
| 6 | + AsyncModifiableResourceMixin, |
5 | 7 | CollectionMixin, |
6 | | - ManagedResourceMixin, |
| 8 | + ModifiableResourceMixin, |
7 | 9 | ) |
| 10 | +from mpt_api_client.http.types import FileTypes |
8 | 11 | from mpt_api_client.models import Model, ResourceData |
9 | 12 | from mpt_api_client.resources.catalog.mixins import ( |
10 | 13 | AsyncPublishableMixin, |
@@ -54,13 +57,38 @@ class ProductsServiceConfig: |
54 | 57 |
|
55 | 58 | class ProductsService( |
56 | 59 | PublishableMixin[Product], |
57 | | - ManagedResourceMixin[Product], |
| 60 | + ModifiableResourceMixin[Product], |
58 | 61 | CollectionMixin[Product], |
59 | 62 | Service[Product], |
60 | 63 | ProductsServiceConfig, |
61 | 64 | ): |
62 | 65 | """Products service.""" |
63 | 66 |
|
| 67 | + def create( |
| 68 | + self, |
| 69 | + resource_data: ResourceData, |
| 70 | + icon: FileTypes, |
| 71 | + ) -> Product: |
| 72 | + """Create product with icon. |
| 73 | +
|
| 74 | + Args: |
| 75 | + resource_data: Product data. |
| 76 | + icon: Icon image in jpg, png, GIF, etc. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Created resource. |
| 80 | + """ |
| 81 | + files: dict[str, FileTypes] = {} |
| 82 | + files["product"] = ( |
| 83 | + None, |
| 84 | + json.dumps(resource_data), |
| 85 | + "application/json", |
| 86 | + ) |
| 87 | + files["icon"] = icon |
| 88 | + response = self.http_client.request("post", self.path, files=files) |
| 89 | + |
| 90 | + return self._model_class.from_response(response) |
| 91 | + |
64 | 92 | def item_groups(self, product_id: str) -> ItemGroupsService: |
65 | 93 | """Return item_groups service.""" |
66 | 94 | return ItemGroupsService( |
@@ -108,13 +136,37 @@ def update_settings(self, product_id: str, settings: ResourceData) -> Product: |
108 | 136 |
|
109 | 137 | class AsyncProductsService( |
110 | 138 | AsyncPublishableMixin[Product], |
111 | | - AsyncManagedResourceMixin[Product], |
| 139 | + AsyncModifiableResourceMixin[Product], |
112 | 140 | AsyncCollectionMixin[Product], |
113 | 141 | AsyncService[Product], |
114 | 142 | ProductsServiceConfig, |
115 | 143 | ): |
116 | 144 | """Products service.""" |
117 | 145 |
|
| 146 | + async def create( |
| 147 | + self, |
| 148 | + resource_data: ResourceData, |
| 149 | + icon: FileTypes, |
| 150 | + ) -> Product: |
| 151 | + """Create product with icon. |
| 152 | +
|
| 153 | + Args: |
| 154 | + resource_data: Product data. |
| 155 | + icon: Icon image in jpg, png, GIF, etc. |
| 156 | +
|
| 157 | + Returns: |
| 158 | + Created resource. |
| 159 | + """ |
| 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) |
| 169 | + |
118 | 170 | def item_groups(self, product_id: str) -> AsyncItemGroupsService: |
119 | 171 | """Return item_groups service.""" |
120 | 172 | return AsyncItemGroupsService( |
|
0 commit comments