hungergames/src/api/index.js

112 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-09-29 18:12:00 +02:00
require('es6-promise').polyfill();
require('isomorphic-fetch');
2017-11-21 22:11:58 +01:00
export const BASE_URL = 'https://world.openfoodfacts.org';
export const BASE_FETCH_PARAMS = {
/*
credentials: 'include',
headers: {
Authorization: `Basic ${btoa('off:off')}`,
},
*/
};
export const USER_ID = '';
export const PASSWORD = '';
2017-09-29 18:12:00 +02:00
2017-10-22 21:20:58 +02:00
function _fetchFromOFFApi(filters) {
2017-11-21 22:11:58 +01:00
let url = BASE_URL;
2017-10-22 21:20:58 +02:00
filters.forEach((filter) => {
url += `/${filter}`;
});
2017-11-21 22:11:58 +01:00
return fetch(`${url}.json`, BASE_FETCH_PARAMS);
}
function _sendToOFFApi(barcode, fields) {
let url = `${BASE_URL}/cgi/product_jqmp2.pl?code=${barcode}&user_id=${USER_ID}&password=${PASSWORD}`;
Object.keys(fields).forEach((field) => {
url += `&${field}=${fields[field]}`;
});
return fetch(url, BASE_FETCH_PARAMS);
2017-10-22 21:20:58 +02:00
}
2017-09-29 18:12:00 +02:00
function missingCategories() {
2017-10-22 21:20:58 +02:00
return _fetchFromOFFApi([
'state/product-name-completed',
'state/brands-completed',
'state/photos-validated',
'state/categories-to-be-completed',
])
// Parse JSON
2017-09-29 18:12:00 +02:00
.then(response => response.json())
2017-10-22 21:20:58 +02:00
// Keep only useful data
2017-09-29 18:12:00 +02:00
.then(response => response.products.map(product => ({
id: product.id,
name: product.product_name,
icon: product.image_front_url,
brands: product.brands,
2017-10-22 21:20:58 +02:00
})))
// Predict categories
.then(response => fetch(
'http://localhost:4242/predict',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
2017-09-29 18:12:00 +02:00
},
2017-10-22 21:20:58 +02:00
body: JSON.stringify(response),
2017-09-29 18:12:00 +02:00
},
2017-10-22 21:20:58 +02:00
))
// Parse JSON
.then(response => response.json())
// Parse data, strip products without any predicted category
.then(response => response.data.filter(
product => product.predictedCategories.length > 0),
)
// Augment predicted categories data
.then(response => response.map(
(product) => {
const augmentedProduct = product;
augmentedProduct.predictedCategories = product.predictedCategories.map(
category => ({ name: category, isOk: false }),
);
return augmentedProduct;
},
))
2017-09-29 18:12:00 +02:00
.catch(exc => console.error(`Unable to fetch products with missing categories: ${exc}.`));
}
2017-10-22 21:20:58 +02:00
function updateCategories(productId, categories) {
2017-11-21 22:11:58 +01:00
return _sendToOFFApi(productId, {
categories: categories.join(','),
});
}
2017-10-22 21:20:58 +02:00
function missingBrands() {
return _fetchFromOFFApi(['state/brands-to-be-completed'])
2017-10-22 21:20:58 +02:00
.then(response => response.json())
.then(response => response.products.map(product => ({
id: product.id,
name: product.product_name,
icon: product.image_front_url,
})))
.catch(exc => console.error(`Unable to fetch products with missing brands: ${exc}.`));
}
function missingProductName() {
return _fetchFromOFFApi(['state/product-name-to-be-completed'])
2017-10-22 21:20:58 +02:00
.then(response => response.json())
.then(response => response.products.map(product => ({
id: product.id,
brands: product.brands,
icon: product.image_front_url,
})))
.catch(exc => console.error(`Unable to fetch products with missing product name: ${exc}.`));
}
export { missingBrands, missingCategories, updateCategories, missingProductName };