enthusiast-source-shopware 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.1
2
+ Name: enthusiast-source-shopware
3
+ Version: 0.1.0
4
+ Summary:
5
+ Author: Mateusz Porebski
6
+ Author-email: mateusz.porebski@upsidelab.io
7
+ Requires-Python: >=3.9,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.9
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Requires-Dist: enthusiast-common (>=0.2,<1)
14
+ Description-Content-Type: text/markdown
15
+
16
+ # Enthusiast Source Shopware
17
+
18
+ This Enthusiast plugin enables the import of product data directly from Shopware.
19
+
@@ -0,0 +1,3 @@
1
+ # Enthusiast Source Shopware
2
+
3
+ This Enthusiast plugin enables the import of product data directly from Shopware.
@@ -0,0 +1 @@
1
+ from .source import ShopwareProductSource as ShopwareProductSource
@@ -0,0 +1,182 @@
1
+ import requests
2
+
3
+ from enthusiast_common import ProductDetails, ProductSourcePlugin
4
+
5
+
6
+ class ShopwareProductSource(ProductSourcePlugin):
7
+ def __init__(self, data_set_id, config: dict):
8
+ """
9
+ Initialize the plugin with the parameters to access source.
10
+
11
+ Args:
12
+ data_set_id (int): identifier of a data set that products are assigned to.
13
+ config (dict): Parameters such as shop url or access token to configure a plugin.
14
+ """
15
+ super().__init__(data_set_id, config)
16
+
17
+ # Source specific parameters.
18
+ self._base_url = config.get("base_url", '')
19
+ self._currency_iso_code = config.get("currency_iso_code", '')
20
+ self._client_id = config.get("client_id", '')
21
+ self._api_key = config.get("api_key", '')
22
+
23
+ self._access_token = None
24
+ self._categories = None
25
+ self._currency_id = None
26
+ self._properties = None
27
+ self._property_options = None
28
+
29
+
30
+ def _get_product(self, product_details):
31
+ """Translates product definition received from Shopware into Enthusiast product.
32
+
33
+ Args:
34
+ product_details: a product returned by Shopware API
35
+ Returns:
36
+ ProductDetails: product definition used by ECL to sync a product.
37
+ """
38
+ return ProductDetails(
39
+ entry_id=product_details.get("id"),
40
+ name=product_details.get("attributes", {}).get("name"),
41
+ slug=product_details.get("attributes", {}).get("name").replace(" ", "-"),
42
+ description=product_details.get("attributes", {}).get("description", "-"),
43
+ sku=product_details.get("attributes", {}).get("productNumber"),
44
+ price=self._product_price(product_details),
45
+ properties=self._product_properties(product_details),
46
+ categories=str([self._product_category(category_id) for category_id in
47
+ product_details.get("attributes", {}).get("categoryTree")])
48
+ )
49
+
50
+ def _get_access_token(self):
51
+ if self._access_token:
52
+ return self._access_token
53
+ body = {
54
+ 'grant_type': 'client_credentials',
55
+ "client_id": self._client_id,
56
+ "client_secret": self._api_key
57
+ }
58
+ response = requests.post(f'{self._base_url}/api/oauth/token', body)
59
+ if response.status_code != 200:
60
+ raise Exception("Failed to acquire access token. Please verify the client ID and API key.")
61
+
62
+ self._access_token = response.json().get('access_token')
63
+ return self._access_token
64
+
65
+ def _build_headers(self):
66
+ return {
67
+ "Authorization": f"Bearer {self._get_access_token()}"
68
+ }
69
+
70
+ def _fetch_products(self):
71
+ response = requests.get(f'{self._base_url}/api/product', headers=self._build_headers())
72
+ if response.status_code != 200:
73
+ raise Exception("Failed to fetch products")
74
+
75
+ data = response.json().get('data', [])
76
+
77
+ # filtering out product variants
78
+ data = filter(lambda o: o.get('type') == 'product' and
79
+ o.get('attributes', {}).get('productNumber') and
80
+ '.' not in o.get('attributes', {}).get('productNumber'),
81
+ data)
82
+ return list(data)
83
+
84
+ def _fetch_categories(self):
85
+ if self._categories:
86
+ return self._categories
87
+
88
+ response = requests.get(f'{self._base_url}/api/category', headers=self._build_headers())
89
+ if response.status_code != 200:
90
+ raise Exception("Failed to fetch categories")
91
+
92
+ self._categories = {
93
+ category_data.get('id'): category_data.get('attributes', {}).get('name')
94
+ for category_data in response.json().get('data')
95
+ }
96
+
97
+ return self._categories
98
+
99
+ def _fetch_currency_id(self):
100
+ if self._currency_id:
101
+ return self._currency_id
102
+
103
+ response = requests.get(f'{self._base_url}/api/currency', headers=self._build_headers())
104
+ if response.status_code != 200:
105
+ raise Exception("Failed to fetch currency id")
106
+
107
+ data = response.json().get('data', [])
108
+ self._currency_id = next(
109
+ (c.get('id') for c in data if c.get('attributes', {}).get('isoCode') == self._currency_iso_code),
110
+ None
111
+ )
112
+
113
+ return self._currency_id
114
+
115
+ def _fetch_properties(self):
116
+ if self._properties:
117
+ return self._properties
118
+
119
+ response = requests.get(f'{self._base_url}/api/property-group', headers=self._build_headers())
120
+ if response.status_code != 200:
121
+ raise Exception("Failed to fetch properties")
122
+
123
+ data = response.json().get('data', [])
124
+ self._properties = {
125
+ property_data.get('id'): property_data.get('attributes', {}).get('name')
126
+ for property_data in data
127
+ }
128
+
129
+ return self._properties
130
+
131
+ def _fetch_property_options(self):
132
+ if self._property_options:
133
+ return self._property_options
134
+
135
+ response = requests.get(f'{self._base_url}/api/property-group-option', headers=self._build_headers())
136
+ if response.status_code != 200:
137
+ raise Exception("Failed to fetch property options")
138
+
139
+ data = response.json().get('data', [])
140
+ self._property_options = {
141
+ property_option_data.get('id'): { 'name': property_option_data.get('attributes', {}).get('name'),
142
+ 'property_id': property_option_data.get('attributes', {}).get('groupId') }
143
+ for property_option_data in data
144
+ }
145
+
146
+ return self._property_options
147
+
148
+ def _product_category(self, category_id):
149
+ return self._fetch_categories().get(category_id)
150
+
151
+ def _product_price(self, product_details):
152
+ price = next((price.get('gross') for price in product_details.get('attributes', {}).get('price') if
153
+ price.get('currencyId') == self._fetch_currency_id()), None)
154
+ return float(price) if price else None
155
+
156
+ def _product_properties(self, product_details):
157
+ if not product_details.get('attributes', {}).get('propertyIds'):
158
+ return '-'
159
+ product_properties = {}
160
+
161
+ for property_option_id in product_details.get('attributes', {}).get('propertyIds'):
162
+ property_option = self._fetch_property_options().get(property_option_id)
163
+ property_name = self._fetch_properties().get(property_option.get('property_id'))
164
+
165
+ if property_name not in product_properties.keys():
166
+ product_properties[property_name] = []
167
+ product_properties[property_name].append(property_option.get('name'))
168
+
169
+ return ' | '.join([f'{property_name} -> {str(property_options)}'
170
+ for property_name, property_options in product_properties.items()])
171
+
172
+ def fetch(self) -> list[ProductDetails]:
173
+ """Fetch product list.
174
+
175
+ Returns:
176
+ list[ProductDetails]: A list of products.
177
+ """
178
+ products = []
179
+ for product_details in self._fetch_products():
180
+ products.append(self._get_product(product_details))
181
+
182
+ return products
@@ -0,0 +1,15 @@
1
+ [tool.poetry]
2
+ name = "enthusiast-source-shopware"
3
+ version = "0.1.0"
4
+ description = ""
5
+ authors = ["Mateusz Porebski <mateusz.porebski@upsidelab.io>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.9"
10
+ enthusiast-common = ">=0.2,<1"
11
+
12
+
13
+ [build-system]
14
+ requires = ["poetry-core"]
15
+ build-backend = "poetry.core.masonry.api"