Skip to content

Commit

Permalink
fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
rondinif committed May 11, 2019
1 parent 31f50ec commit 7f496d8
Show file tree
Hide file tree
Showing 33 changed files with 648 additions and 487 deletions.
31 changes: 21 additions & 10 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
// DIP: export Higher-order function factories : each of them returns a function as its result.

// Higher-order function: returns a function as its result.
function makeGetPromiseOfWikiDataApiResults(fetch) { return (uri, headers) => {
return fetch(encodeURI(uri), { headers }).then(body => body.json());
}; }
function makeGetPromiseOfWikiDataApiResults(fetch, log) {
return (uri, headers) => {
return fetch(encodeURI(uri), {
headers: headers // ,
// method: 'GET', ...mode, cache , see
}).then(body => body.json())
.catch(err => log.error(`ERROR FETCHING DATA: ${err.message}`));
};
}

// Higher-order function: returns a function as its result.
function makeGetPromiseOfSparqlResults(fetch) { return (serviceUri, sparql, headers) => {
const uri = `${serviceUri}/sparql?query=${sparql}`;
return fetch(encodeURI(uri), { headers }).then(body => body.json());
}; }
function makeGetPromiseOfSparqlResults(fetch, log) {
return (serviceUri, sparql, headers) => {
const uri = `${serviceUri}/sparql?query=${sparql}`;
return fetch(encodeURI(uri), { headers })
.then(body => body.json())
.catch(err => log.error(`ERROR FETCHING DATA: ${err.message}`));
};
}

const openDataPromisesFactories = {
makeWdSearchByAnyName: (ff, config, log) => name => {
Expand All @@ -33,11 +43,12 @@ const openDataPromisesFactories = {
/* wdSearchByAnyName:
dato un nome generico nome di pianta espresso in qualsiasi lingua
ritorna una lista di `wikidata entities`
[1.0.1 BUG FIX]: added `origin=*`
*/
function getPromiseOfWikiDataApiActionQuerySearchByName({ ff, config, log }, name) {
name = (name === undefined) ? "" : name;
const serviceUri = config.isUnderTest() ? 'http://127.0.0.1:6568' : 'https://www.wikidata.org';
const uri = `${serviceUri}/w/api.php?action=query&format=json&list=search&srsearch=${name}&srlimit=500`;
const uri = `${serviceUri}/w/api.php?action=query&format=json&origin=*&list=search&srsearch=${name}&srlimit=500`;
log.trace(uri);
const headers = { 'Accept': 'application/json' };
// ritorna la promise ottenta dal modulo di gestione delle richieste http asincone verso opendata
Expand Down Expand Up @@ -244,8 +255,8 @@ class Phyto {
* @param {Function} logger
*/
constructor(fetch, config, log) {
const _ff = makeGetPromiseOfWikiDataApiResults(fetch);
const _ffSparql = makeGetPromiseOfSparqlResults(fetch);
const _ff = makeGetPromiseOfWikiDataApiResults(fetch, log);
const _ffSparql = makeGetPromiseOfSparqlResults(fetch, log);

this._wdSearchByAnyName = openDataPromisesFactories.makeWdSearchByAnyName(_ff, config, log);
this._wdPlantsByAnyName = openDataPromisesFactories.makeWdPlantsByAnyName(_ff, config, log);
Expand Down
24 changes: 17 additions & 7 deletions src/lib/OpenDataAsyncFactories.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
// DIP: export Higher-order function factories : each of them returns a function as its result.

// Higher-order function: returns a function as its result.
export function makeGetPromiseOfWikiDataApiResults(fetch) { return (uri, headers) => {
return fetch(encodeURI(uri), { headers }).then(body => body.json());
}; }
export function makeGetPromiseOfWikiDataApiResults(fetch, log) {
return (uri, headers) => {
return fetch(encodeURI(uri), {
headers: headers // ,
// method: 'GET', ...mode, cache , see
}).then(body => body.json())
.catch(err => log.error(`ERROR FETCHING DATA: ${err.message}`));
};
}

// Higher-order function: returns a function as its result.
export function makeGetPromiseOfSparqlResults(fetch) { return (serviceUri, sparql, headers) => {
const uri = `${serviceUri}/sparql?query=${sparql}`;
return fetch(encodeURI(uri), { headers }).then(body => body.json());
}; }
export function makeGetPromiseOfSparqlResults(fetch, log) {
return (serviceUri, sparql, headers) => {
const uri = `${serviceUri}/sparql?query=${sparql}`;
return fetch(encodeURI(uri), { headers })
.then(body => body.json())
.catch(err => log.error(`ERROR FETCHING DATA: ${err.message}`));
};
}
3 changes: 2 additions & 1 deletion src/lib/OpenDataLogicAgent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export const openDataPromisesFactories = {
/* wdSearchByAnyName:
dato un nome generico nome di pianta espresso in qualsiasi lingua
ritorna una lista di `wikidata entities`
[1.0.1 BUG FIX]: added `origin=*`
*/
function getPromiseOfWikiDataApiActionQuerySearchByName({ ff, config, log }, name) {
name = (name === undefined) ? "" : name;
const serviceUri = config.isUnderTest() ? 'http://127.0.0.1:6568' : 'https://www.wikidata.org';
const uri = `${serviceUri}/w/api.php?action=query&format=json&list=search&srsearch=${name}&srlimit=500`
const uri = `${serviceUri}/w/api.php?action=query&format=json&origin=*&list=search&srsearch=${name}&srlimit=500`;
log.trace(uri);
const headers = { 'Accept': 'application/json' };
// ritorna la promise ottenta dal modulo di gestione delle richieste http asincone verso opendata
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class Phyto {
* @param {Function} logger
*/
constructor(fetch, config, log) {
const _ff = makeGetPromiseOfWikiDataApiResults(fetch);
const _ffSparql = makeGetPromiseOfSparqlResults(fetch);
const _ff = makeGetPromiseOfWikiDataApiResults(fetch, log);
const _ffSparql = makeGetPromiseOfSparqlResults(fetch, log);

this._wdSearchByAnyName = openDataPromisesFactories.makeWdSearchByAnyName(_ff, config, log);
this._wdPlantsByAnyName = openDataPromisesFactories.makeWdPlantsByAnyName(_ff, config, log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,77 @@
{
"scope": "http://127.0.0.1:6568",
"method": "GET",
"path": "/w/api.php?action=query&format=json&list=search&srsearch=bietola%20da%20coste&srlimit=500",
"path": "/w/api.php?action=query&format=json&origin=*&list=search&srsearch=bietola%20da%20coste&srlimit=500",
"body": "",
"status": 200,
"response": [
"1f8b0800000000000003ab564a4a2c49ce48cecf2dc8492d4955b25252d2512a2c4d2daa54b2aa562a4e4d2c4acec8cc4bcb07f14af24b127332324b8a95ac0c6a75a0924a56d1b1b5b50078de9ca847000000"
],
"rawHeaders": [
"Date",
"Tue, 16 Apr 2019 02:00:39 GMT",
"Thu, 09 May 2019 20:00:55 GMT",
"Content-Type",
"application/json; charset=utf-8",
"Content-Length",
"83",
"Connection",
"close",
"Server",
"mw1341.eqiad.wmnet",
"X-Powered-By",
"HHVM/3.18.6-dev",
"mw1282.eqiad.wmnet",
"X-Search-ID",
"7q8ogo81l01ztoacm29yupupd",
"Access-Control-Allow-Credentials",
"false",
"P3P",
"CP=\"This is not a P3P policy! See https://www.wikidata.org/wiki/Special:CentralAutoLogin/P3P for more info.\"",
"Cache-control",
"private, must-revalidate, max-age=0",
"X-Search-ID",
"wve5u4316l5isiz0g38dlzte",
"X-Content-Type-Options",
"nosniff",
"Content-Disposition",
"inline; filename=api-result.json",
"X-Frame-Options",
"DENY",
"MediaWiki-Login-Suppressed",
"true",
"Access-Control-Allow-Origin",
"*",
"X-Powered-By",
"HHVM/3.18.6-dev",
"Access-Control-Expose-Headers",
"MediaWiki-API-Error, Retry-After, X-Database-Lag, MediaWiki-Login-Suppressed",
"Cache-control",
"private, must-revalidate, max-age=0",
"X-Content-Type-Options",
"nosniff",
"Backend-Timing",
"D=72143 t=1555380039361910",
"D=176061 t=1557432055291175",
"Vary",
"Accept-Encoding,Treat-as-Untrusted,X-Forwarded-Proto,Cookie,Authorization,X-Seven",
"Content-Encoding",
"gzip",
"X-Varnish",
"471639871, 107007341, 802413232",
"890292081, 8488931, 149677265",
"Via",
"1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)",
"Accept-Ranges",
"bytes",
"Age",
"0",
"X-Cache",
"cp1083 pass, cp3042 pass, cp3030 pass",
"cp1077 pass, cp3040 pass, cp3030 pass",
"X-Cache-Status",
"pass",
"Server-Timing",
"cache;desc=\"pass\"",
"Strict-Transport-Security",
"max-age=106384710; includeSubDomains; preload",
"Set-Cookie",
"WMF-Last-Access=16-Apr-2019;Path=/;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access=09-May-2019;Path=/;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"WMF-Last-Access-Global=16-Apr-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access-Global=09-May-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"GeoIP=IT:45:Parma:44.80:10.33:v4; Path=/; secure; Domain=.wikidata.org",
"GeoIP=IT:::43.15:12.11:v4; Path=/; secure; Domain=.wikidata.org",
"X-Analytics",
"ns=-1;special=Badtitle;https=1;nocookies=1",
"X-Client-IP",
"146.241.122.176"
"146.241.64.248"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,77 @@
{
"scope": "http://127.0.0.1:6568",
"method": "GET",
"path": "/w/api.php?action=query&format=json&list=search&srsearch=%D0%90%D0%BD%D0%B3%D1%83%D1%80%D0%B8%D1%8F&srlimit=500",
"path": "/w/api.php?action=query&format=json&origin=*&list=search&srsearch=%D0%90%D0%BD%D0%B3%D1%83%D1%80%D0%B8%D1%8F&srlimit=500",
"body": "",
"status": 200,
"response": [
"1f8b08000000000000032d8c410ec2201045af62664d13a0b509dcc2c495c605d2a925a180651aa30d77971a777ffe9bff36b81bb2938d73f248081a80c173c5e50d7a838c66b1930b63dc2f8a64fce428831685fd21e8eb06a1569c0139f2bbe224db4e71d55755320f740368297b7594a2aedca7be84d57b06afb80c36ae817eeb1c5c4a5833e484d6613ec4f190bca97857cf98c9cca962c9856a78d788fecca5e65cb7ea02e556ca176503092ecd000000"
"1f8b08000000000000032d8c410ac2301045af5266ddc234da42720bc195e222a6531b4893d84c112db9bba9b8fbf3dffcb7c15db3994c98a323265000353c575adea03648a41733593f86fde2c0da4d9613a836d77f08eaba812f15d6c096ddae3889c351a2ec8b2aea07d9019410bdec445b56f6535efcea5c0dafb00c26ac9e7febe46d8c5432a448c652aac25845a70bded53325d6732c58602b1bec1a9467810a5175fd05f22de72fcc27b241cd000000"
],
"rawHeaders": [
"Date",
"Tue, 16 Apr 2019 02:00:39 GMT",
"Thu, 09 May 2019 20:00:56 GMT",
"Content-Type",
"application/json; charset=utf-8",
"Content-Length",
"179",
"Connection",
"close",
"Server",
"mw1223.eqiad.wmnet",
"X-Powered-By",
"HHVM/3.18.6-dev",
"mw1221.eqiad.wmnet",
"X-Search-ID",
"14jqzsx6vvyhqqvfnjhki439q",
"Access-Control-Allow-Credentials",
"false",
"P3P",
"CP=\"This is not a P3P policy! See https://www.wikidata.org/wiki/Special:CentralAutoLogin/P3P for more info.\"",
"Cache-control",
"private, must-revalidate, max-age=0",
"X-Search-ID",
"9tixaevz3jfs5lwdq3xm6b0be",
"X-Content-Type-Options",
"nosniff",
"Content-Disposition",
"inline; filename=api-result.json",
"X-Frame-Options",
"DENY",
"MediaWiki-Login-Suppressed",
"true",
"Access-Control-Allow-Origin",
"*",
"X-Powered-By",
"HHVM/3.18.6-dev",
"Access-Control-Expose-Headers",
"MediaWiki-API-Error, Retry-After, X-Database-Lag, MediaWiki-Login-Suppressed",
"Cache-control",
"private, must-revalidate, max-age=0",
"X-Content-Type-Options",
"nosniff",
"Backend-Timing",
"D=70432 t=1555380039687651",
"D=129586 t=1557432056070315",
"Vary",
"Accept-Encoding,Treat-as-Untrusted,X-Forwarded-Proto,Cookie,Authorization,X-Seven",
"Content-Encoding",
"gzip",
"X-Varnish",
"293983229, 1035413992, 826449322",
"576095967, 385225193, 156850915",
"Via",
"1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)",
"Accept-Ranges",
"bytes",
"Age",
"0",
"X-Cache",
"cp1087 pass, cp3030 pass, cp3030 pass",
"cp1081 pass, cp3030 pass, cp3030 pass",
"X-Cache-Status",
"pass",
"Server-Timing",
"cache;desc=\"pass\"",
"Strict-Transport-Security",
"max-age=106384710; includeSubDomains; preload",
"Set-Cookie",
"WMF-Last-Access=16-Apr-2019;Path=/;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access=09-May-2019;Path=/;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"WMF-Last-Access-Global=16-Apr-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access-Global=09-May-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"GeoIP=IT:45:Parma:44.80:10.33:v4; Path=/; secure; Domain=.wikidata.org",
"GeoIP=IT:::43.15:12.11:v4; Path=/; secure; Domain=.wikidata.org",
"X-Analytics",
"ns=-1;special=Badtitle;https=1;nocookies=1",
"X-Client-IP",
"146.241.122.176"
"146.241.64.248"
]
},
{
Expand All @@ -78,7 +86,7 @@
],
"rawHeaders": [
"Date",
"Tue, 16 Apr 2019 02:00:43 GMT",
"Thu, 09 May 2019 20:01:02 GMT",
"Content-Type",
"application/sparql-results+json;charset=utf-8",
"Content-Length",
Expand All @@ -88,7 +96,7 @@
"Server",
"nginx/1.13.6",
"X-Served-By",
"wdqs1005",
"wdqs1004",
"Access-Control-Allow-Origin",
"*",
"Cache-Control",
Expand All @@ -98,29 +106,29 @@
"Vary",
"Accept, Accept-Encoding",
"X-Varnish",
"63320800, 903801432, 821986168",
"557454443, 910029843, 145587214",
"Via",
"1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)",
"Accept-Ranges",
"bytes",
"Age",
"0",
"X-Cache",
"cp1081 pass, cp3033 miss, cp3030 miss",
"cp1081 pass, cp3033 miss, cp3030 pass",
"X-Cache-Status",
"miss",
"Server-Timing",
"cache;desc=\"miss\"",
"Strict-Transport-Security",
"max-age=106384710; includeSubDomains; preload",
"Set-Cookie",
"WMF-Last-Access=16-Apr-2019;Path=/;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access=09-May-2019;Path=/;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"WMF-Last-Access-Global=16-Apr-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access-Global=09-May-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"X-Analytics",
"https=1;nocookies=1",
"X-Client-IP",
"146.241.122.176",
"Accept-Ranges",
"bytes"
"146.241.64.248"
]
},
{
Expand All @@ -134,7 +142,7 @@
],
"rawHeaders": [
"Date",
"Tue, 16 Apr 2019 02:00:43 GMT",
"Thu, 09 May 2019 20:01:03 GMT",
"Content-Type",
"application/sparql-results+json;charset=utf-8",
"Content-Length",
Expand All @@ -154,7 +162,7 @@
"Vary",
"Accept, Accept-Encoding",
"X-Varnish",
"1048926662, 448644, 822586665",
"883531251, 257148349, 159953199",
"Via",
"1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1), 1.1 varnish (Varnish/5.1)",
"Age",
Expand All @@ -168,13 +176,13 @@
"Strict-Transport-Security",
"max-age=106384710; includeSubDomains; preload",
"Set-Cookie",
"WMF-Last-Access=16-Apr-2019;Path=/;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access=09-May-2019;Path=/;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"Set-Cookie",
"WMF-Last-Access-Global=16-Apr-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Sat, 18 May 2019 00:00:00 GMT",
"WMF-Last-Access-Global=09-May-2019;Path=/;Domain=.wikidata.org;HttpOnly;secure;Expires=Mon, 10 Jun 2019 12:00:00 GMT",
"X-Analytics",
"https=1;nocookies=1",
"X-Client-IP",
"146.241.122.176",
"146.241.64.248",
"Accept-Ranges",
"bytes"
]
Expand Down
Loading

0 comments on commit 7f496d8

Please sign in to comment.