diff --git a/package-lock.json b/package-lock.json index fcece6a..00cf969 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1337,6 +1337,11 @@ "path-exists": "^3.0.0" } }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, "lodash._reinterpolate": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz", @@ -1906,6 +1911,16 @@ "uuid": "^3.3.2" } }, + "requestretry": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/requestretry/-/requestretry-4.0.2.tgz", + "integrity": "sha512-ZGdO1ZXUQAeCB9xOS2keSN501y7T1t0zPOD58jTAOwamt6qkcBMaGdRBHEOMQRnDtT5fn7S99F0dwADUqCmYqg==", + "requires": { + "extend": "^3.0.2", + "lodash": "^4.17.10", + "when": "^3.7.7" + } + }, "resolve": { "version": "1.11.1", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.11.1.tgz", @@ -2310,6 +2325,11 @@ "extsprintf": "^1.2.0" } }, + "when": { + "version": "3.7.8", + "resolved": "https://registry.npmjs.org/when/-/when-3.7.8.tgz", + "integrity": "sha1-xxMLan6gRpPoQs3J56Hyqjmjn4I=" + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index 1f41bc2..e8d6ddd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sudoctoolkit", "productName": "SudocToolkit", - "version": "1.0.0", + "version": "1.1.0", "description": "Utiliser les web services de l'Abes sans être développeur", "author": "Géraldine Geoffroy", "main": "main.js", @@ -24,6 +24,7 @@ "jquery": "^3.4.1", "progress-stream": "^2.0.0", "request": "^2.88.0", + "requestretry": "^4.0.2", "underscore": "^1.9.1", "update-electron-app": "^1.5.0" } diff --git a/renderer-process/renderer.js b/renderer-process/renderer.js index a76a9af..72e6937 100644 --- a/renderer-process/renderer.js +++ b/renderer-process/renderer.js @@ -3,7 +3,7 @@ const electron = require('electron') const dialog = electron.remote.dialog const BrowserWindow = electron.remote.BrowserWindow const path = require('path') -const request = require('request') +const request = require('requestretry') const childProcess = require('child_process') const fs = require('fs') @@ -105,17 +105,13 @@ var str = progress({ getValue(record[chooseKey].toString()).then( function(val) { record = filterTypes(record,arrColumns) + console.log(record) if(val === undefined) {record[columnResultName]="2 ou +"} else { record[columnResultName]=val } callback(null, record) - }, - function(error) { - record = filterTypes(record,arrColumns) - record[columnResultName]="0" - callback(null, record) - } + } ) }) ) @@ -185,62 +181,103 @@ var str = progress({ return result; } //sudoc web services - function getValue(id){ +function getValue(id){ return new Promise(function(resolve, reject){ switch(selectedOption) { case "isbn2ppncount": - request('https://www.sudoc.fr/services/isbn2ppn/'+id+'&format=text/json', function (error, response, body) { - try { - if(Array.isArray(JSON.parse(body).sudoc.query.result)) { - resolve(JSON.parse(body).sudoc.query.result.length);} - else { - resolve("1"); - } - } catch(e) { - reject(e); + request({ + url: 'https://www.sudoc.fr/services/isbn2ppn/'+id+'&format=text/json', + json: true, + maxAttempts: 5, + retryDelay: 5000, + retryStrategy: request.RetryStrategies.HTTPOrNetworkError + }, function(err, response, body){ + if (response) { + if(Array.isArray(body.sudoc.query.result)) { + resolve(body.sudoc.query.result.length) + } + else { + resolve("1") + } } - }); + else { + resolve("0") + } + }) break; case "isbn2ppn": - request('https://www.sudoc.fr/services/isbn2ppn/'+id+'&format=text/json', function (error, response, body) { - try { - resolve(JSON.parse(body).sudoc.query.result.ppn) - } catch(e) { - reject(e); - } - }); + request({ + url: 'https://www.sudoc.fr/services/isbn2ppn/'+id+'&format=text/json', + json: true, + maxAttempts: 5, + retryDelay: 5000, + retryStrategy: request.RetryStrategies.HTTPOrNetworkError + }, function(err, response, body){ + if (response) { + resolve(body.sudoc.query.result.ppn) + } + else { + resolve("0") + } + }) break; case "issn2ppn": - request('https://www.sudoc.fr/services/issn2ppn/'+id+'&format=text/json', function (error, response, body) { - try { - resolve(JSON.parse(body).sudoc.query.result.ppn) - } catch(e) { - reject(e); - } - }); + request({ + url: 'https://www.sudoc.fr/services/issn2ppn/'+id+'&format=text/json', + json: true, + maxAttempts: 5, + retryDelay: 5000, + retryStrategy: request.RetryStrategies.HTTPOrNetworkError + }, function(err, response, body){ + if (response) { + resolve(body.sudoc.query.result.ppn) + } + else { + resolve("0") + } + }) break; case "merged": - request('https://www.sudoc.fr/services/merged/'+id+'&format=text/json', function (error, response, body) { - try { - resolve(JSON.parse(body).sudoc.query.result.ppn); - } catch(e) { - reject(e); + request({ + url: 'https://www.sudoc.fr/services/merged/'+id+'&format=text/json', + json: true, + maxAttempts: 5, + retryDelay: 5000, + retryStrategy: request.RetryStrategies.HTTPOrNetworkError + }, function(err, response, body){ + if (response) { + if(!body.sudoc) { + resolve("0") + } + else { + resolve(body.sudoc.query.result.ppn) + } } - }); + else { + resolve("0") + } + }) break; case "multiwhere": - request('https://www.sudoc.fr/services/multiwhere/'+id+'&format=text/json', function (error, response, body) { - console.log(JSON.parse(body)) - try { - if(Array.isArray(JSON.parse(body).sudoc.query.result.library)) { - resolve(JSON.parse(body).sudoc.query.result.library.length);} - else { - resolve("1"); - } - } catch(e) { - reject(e); + request({ + url: 'https://www.sudoc.fr/services/multiwhere/'+id+'&format=text/json', + json: true, + maxAttempts: 5, + retryDelay: 5000, + retryStrategy: request.RetryStrategies.HTTPOrNetworkError + }, function(err, response, body){ + if (response) { + if(Array.isArray(body.sudoc.query.result.library)) { + resolve(body.sudoc.query.result.library.length) + } + else { + resolve("1") + } } - }); + else { + resolve("0") + } + }); break; } });