@@ -89,14 +89,33 @@ $client = new \GuzzleHttp\Client([
89
89
]);
90
90
```
91
91
92
+ ``` javascript [activate:NodeJS]
93
+
94
+ // Install the node-fetch library by following the official documentation:
95
+ // https://www.npmjs.com/package/node-fetch
96
+ import fetch from ' node-fetch' ;
97
+
98
+ const pimUrl = ' https://url-of-your-pim.com' ;
99
+ const accessToken = ' your_app_token' ; // Token provided during oAuth steps
100
+
101
+ // Set your client for querying Akeneo API as follows
102
+ async function get (url , accessToken ) {
103
+ return await fetch (url, {
104
+ headers: {
105
+ ' Authorization' : ` Bearer ${ accessToken} `
106
+ }
107
+ });
108
+ }
109
+ ```
110
+
92
111
### 1 - Collect families and attribute codes
93
112
94
113
Get families and attribute codes by requesting the PIM API
95
114
96
115
``` php [activate:PHP]
97
116
98
- const API_URL = '/api/rest/v1/families?search={"has_products":[{"operator":"=","value":true}]}' ;
99
-
117
+ const MAX_ITEMS = 100 ;
118
+ const API_URL = '/api/rest/v1/families?search={"has_products":[{"operator":"=","value":true}]}&limit=' . MAX_ITEMS;
100
119
// Make an authenticated call to the API
101
120
$response = $client->get(API_URL);
102
121
@@ -122,6 +141,38 @@ saveFamilies($families);
122
141
saveAttributesCodes($attributeCodes);
123
142
```
124
143
144
+ ``` javascript [activate:NodeJS]
145
+
146
+ const maxItems = 100 ;
147
+ const apiUrl = ' api/rest/v1/families?search={"has_products":[{"operator":"=","value":true}]}&limit=' + maxItems;
148
+
149
+ const response = await get (` ${ pimUrl} /${ apiUrl} ` , accessToken);
150
+
151
+ let data = await response .json ();
152
+
153
+ const families = data[' _embedded' ][' items' ];
154
+
155
+ while (data[' _links' ].hasOwnProperty (' next' )) {
156
+ const response = await get (data[' _links' ][' next' ][' href' ], accessToken);
157
+
158
+ data = await response .json ();
159
+
160
+ let newFamilies = data[' _embedded' ][' items' ];
161
+ families .push (... newFamilies);
162
+ }
163
+
164
+ // Collect attributes from all families
165
+ const attributeCodes = families .reduce (
166
+ (acc , family ) => [... acc, ... family .attributes ],
167
+ []
168
+ );
169
+ const uniqueAttributeCodes = [... new Set (attributeCodes)];
170
+
171
+ // Save families and attribute codes into stores
172
+ saveFamilies (families);
173
+ saveAttributeCodes (uniqueAttributeCodes);
174
+ ```
175
+
125
176
Store family codes in a <b >family_code_list</b > and attribute codes in a separate list (<b >attribute_code_list</b >). We will deal with <b >attribute_code_list</b > later in this tutorial.
126
177
127
178
::: tips
@@ -172,6 +223,40 @@ saveFamilyVariants($indexedFamilyVariants);
172
223
173
224
```
174
225
226
+ ``` javascript [activate:NodeJS]
227
+
228
+ const maxItems = 100 ;
229
+
230
+ // Get family codes from storage
231
+ const familyCodes = await getFamilyCodes ();
232
+
233
+ let familyVariants = [];
234
+ for (const code of familyCodes) {
235
+ const apiUrl = ` api/rest/v1/families/${ code} /variants?limit=` + maxItems;
236
+ const response = await get (` ${ pimUrl} /${ apiUrl} ` , accessToken);
237
+ let data = await response .json ();
238
+ let newVariants = data[' _embedded' ][' items' ];
239
+ familyVariants .push (... newVariants);
240
+
241
+ while (data[' _links' ].hasOwnProperty (' next' )) {
242
+ const response = await get (data[' _links' ][' next' ][' href' ], accessToken);
243
+ data = await response .json ();
244
+ newVariants = data[' _embedded' ][' items' ];
245
+ familyVariants .push (... newVariants);
246
+ }
247
+ }
248
+
249
+ // Only keep fields needed
250
+ let indexedFamilyVariants = {};
251
+ for (const familyVariant of familyVariants) {
252
+ indexedFamilyVariants[familyVariant[' code' ]] = familyVariant;
253
+ }
254
+
255
+ // Save family variants into storage
256
+ saveFamilyVariants (indexedFamilyVariants);
257
+ ```
258
+
259
+
175
260
### 3 - Collect attributes
176
261
177
262
Remember your <b >attribute_code_list</b >? It’s (already) time to use it to retrieve attribute information
@@ -206,8 +291,47 @@ foreach ($rawAttributes as $rawAttribute) {
206
291
saveAttributes($attributes);
207
292
```
208
293
294
+ ``` javascript [activate:NodeJS]
295
+
296
+ const maxItems = 100 ;
297
+
298
+ // Get attributes codes from storage
299
+ const attributeCodes = await getAttributeCodes ();
300
+
301
+ let chunks = [];
302
+ for (let key = 0 ; key < attributeCodes .length ; key += maxItems) {
303
+ chunks .push (attributeCodes .slice (key, key + maxItems));
304
+ }
305
+
306
+ let rawAttributes = [];
307
+ for (const item of chunks) {
308
+ const apiUrl = ` api/rest/v1/attributes?search={"code":[{"operator":"IN","value":${ JSON .stringify (item)} }]}&limit=` + maxItems;
309
+ const response = await fetch (` ${ pimUrl} /${ apiUrl} ` , {
310
+ headers: {
311
+ ' Authorization' : ` Bearer ${ accessToken} `
312
+ }
313
+ });
314
+ let data = await response .json ();
315
+ let newRawAttributes = data[' _embedded' ][' items' ]
316
+ rawAttributes .push (... newRawAttributes);
317
+ }
318
+
319
+ // Only keep fields needed
320
+ let attributes = {};
321
+ for (const rawAttribute of rawAttributes) {
322
+ attributes[rawAttribute[' code' ]] = {
323
+ ' code' : rawAttribute[' code' ],
324
+ ' type' : rawAttribute[' type' ],
325
+ // Add additional fields if needed
326
+ };
327
+ }
328
+
329
+ // save attributes into storage
330
+ saveAttributes (attributes);
331
+ ```
332
+
209
333
Retrieved attribute list follows this structure:
210
- ``` php
334
+ ``` php [activate:PHP]
211
335
212
336
// Output
213
337
[
@@ -218,6 +342,18 @@ Retrieved attribute list follows this structure:
218
342
]
219
343
```
220
344
345
+ ``` javascript [activate:NodeJS]
346
+
347
+ // Output
348
+ {
349
+ " attribute_code" :
350
+ {
351
+ " code" : " attribute_code" ,
352
+ " type" : " pim_catalog_text"
353
+ }
354
+ }
355
+ ```
356
+
221
357
::: warning
222
358
attribute_code_list may be significant, very big! If you get an <a href =" https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15 " target =" _blank " rel =" noopener noreferrer " >HTTP 414 error</a >
223
359
, you probably hit these boundaries. A workaround is to split your attribute_code_list into different parts and call them independently.
0 commit comments