Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from mdholloway/x-client-ip
Browse files Browse the repository at this point in the history
Pass through X-Client-IP header to WDQS
  • Loading branch information
thesocialdev committed Sep 26, 2018
2 parents b415827 + 9f8471c commit 2f53349
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions geoshapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ function handler(type, req, res, next) {
return Promise.try(
() => {
geoshape = new GeoShapes(type, req.query);
return geoshape.execute();
const lowerHeaders = Object.keys(req.headers).reduce((newHeaders, key) => {
newHeaders[key.toLowerCase()] = headers[key];
return newHeaders;
}, {});
return geoshape.execute(lowerHeaders['x-client-ip']);
}
).then(geodata => {
core.setResponseHeaders(res);
Expand Down Expand Up @@ -208,10 +212,10 @@ function GeoShapes(type, reqParams) {
* Main execution method
* @return {Promise}
*/
GeoShapes.prototype.execute = function execute () {
GeoShapes.prototype.execute = function execute (xClientIp) {
let self = this;
return Promise.try(
() => self.runWikidataQuery()
() => self.runWikidataQuery(xClientIp)
).then(
() => Promise.all([self.runSqlQuery(), self.expandProperties()])
).then(
Expand All @@ -223,7 +227,7 @@ GeoShapes.prototype.execute = function execute () {
*
* @return {Promise|undefined}
*/
GeoShapes.prototype.runWikidataQuery = function runWikidataQuery () {
GeoShapes.prototype.runWikidataQuery = function runWikidataQuery (xClientIp) {
let self = this;
// If there is no query, we only use the ids given in the request
if (!self.sparqlQuery) return;
Expand All @@ -234,7 +238,7 @@ GeoShapes.prototype.runWikidataQuery = function runWikidataQuery () {
format: 'json',
query: self.sparqlQuery
},
headers: config.sparqlHeaders
headers: Object.assign(config.sparqlHeaders, { 'X-Client-IP': xClientIp });
}).then(queryResult => {
if (queryResult.headers['content-type'] !== 'application/sparql-results+json') {
throw new Err('Unexpected content type %s', queryResult.headers['content-type']);
Expand Down

0 comments on commit 2f53349

Please sign in to comment.