Skip to content

Commit 69704b7

Browse files
authored
feat(API-1898-API-1902): add node js snippets for "How to get families, family variants, and attributes" and "How to get PIM product information"
1 parent 26c925d commit 69704b7

File tree

2 files changed

+462
-4
lines changed

2 files changed

+462
-4
lines changed

content/tutorials/guides/how-to-get-families-and-attributes.md

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,33 @@ $client = new \GuzzleHttp\Client([
8989
]);
9090
```
9191

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+
92111
### 1 - Collect families and attribute codes
93112

94113
Get families and attribute codes by requesting the PIM API
95114

96115
```php [activate:PHP]
97116

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;
100119
// Make an authenticated call to the API
101120
$response = $client->get(API_URL);
102121

@@ -122,6 +141,38 @@ saveFamilies($families);
122141
saveAttributesCodes($attributeCodes);
123142
```
124143

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+
125176
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.
126177

127178
::: tips
@@ -172,6 +223,40 @@ saveFamilyVariants($indexedFamilyVariants);
172223

173224
```
174225

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+
175260
### 3 - Collect attributes
176261

177262
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) {
206291
saveAttributes($attributes);
207292
```
208293

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+
209333
Retrieved attribute list follows this structure:
210-
```php
334+
```php [activate:PHP]
211335

212336
// Output
213337
[
@@ -218,6 +342,18 @@ Retrieved attribute list follows this structure:
218342
]
219343
```
220344

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+
221357
::: warning
222358
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>
223359
, you probably hit these boundaries. A workaround is to split your attribute_code_list into different parts and call them independently.

0 commit comments

Comments
 (0)