sparc-commons
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -1,5 +0,4 @@ | ||
var resources = exports.resources = require('./libs/resources'); | ||
var logger = require('./libs/logger'); | ||
exports.logger = logger.logger; | ||
exports.log = logger.log; | ||
var _ = require('lodash') | ||
var es = exports | ||
var lib = require('./libs') | ||
_.merge(es, lib) |
@@ -25,62 +25,60 @@ /*jshint multistr: true */ | ||
module.exports = { | ||
/** | ||
* Perform a needle request with parameters given from the configuration | ||
* | ||
* @param {String} target the name of the service provider | ||
* @param {String} service the name of the service as present in the configuration file | ||
* @param {Object} opts | ||
* @param {Object} [opts.request] the request options | ||
* @param {Stream} [opts.stream] if this is true, then data is ignored | ||
* @param {Object} [opts.data] key value pairs to be set in the request | ||
* @param {Object} [opts.header] optional header in case of post requests | ||
* @return {Stream<JSON>} | ||
*/ | ||
requestResource: function requestResource(target, service, opts){ | ||
var self = this; | ||
if(_.isUndefined(service)){ | ||
throw new Error("Resource invoked with undefined service parameter for resource " + target); | ||
} | ||
var resource = _.get(resources, target); | ||
var url = resource.url + '/' + resource[service]; | ||
var data = _.get(opts, "data", {}); | ||
_.merge(data, _.get(resource, "api-params", {})); | ||
var requestStream = _.get(opts, 'stream'); | ||
var parser; | ||
/** | ||
* Perform a needle request with parameters given from the configuration | ||
* | ||
* @param {String} target the name of the service provider | ||
* @param {String} service the name of the service as present in the configuration file | ||
* @param {Object} opts | ||
* @param {Object} [opts.request] the request options | ||
* @param {Stream} [opts.stream] if this is true, then data is ignored | ||
* @param {Object} [opts.data] key value pairs to be set in the request | ||
* @param {Object} [opts.header] optional header in case of post requests | ||
* @return {Stream<JSON>} | ||
*/ | ||
exports.requestResource = function requestResource(target, service, opts){ | ||
var self = this; | ||
if(_.isUndefined(service)){ | ||
throw new Error("Resource invoked with undefined service parameter for resource " + target); | ||
} | ||
var resource = _.get(resources, target); | ||
var url = resource.url + '/' + resource[service]; | ||
var data = _.get(opts, "data", {}); | ||
_.merge(data, _.get(resource, "api-params", {})); | ||
var requestStream = _.get(opts, 'stream'); | ||
var parser; | ||
if(!_.isUndefined(requestStream)){ | ||
//in this case all data options are brutally added to the url. | ||
//A cleaner way would be for needle to permit access to http_opts (see get_request_opts and config.http_opts in needle) | ||
url += '?'; | ||
url += _.reduce(data, function(u, v, k){ | ||
u.push(k + '=' + encodeURIComponent(v)); | ||
return u; | ||
}, []).join('&'); | ||
if(!_.isUndefined(requestStream)){ | ||
//in this case all data options are brutally added to the url. | ||
//A cleaner way would be for needle to permit access to http_opts (see get_request_opts and config.http_opts in needle) | ||
url += '?'; | ||
url += _.reduce(data, function(u, v, k){ | ||
u.push(k + '=' + encodeURIComponent(v)); | ||
return u; | ||
}, []).join('&'); | ||
data = requestStream; | ||
} | ||
data = requestStream; | ||
} | ||
return new Promise(function(res, rej){ | ||
try{ | ||
needle.request( | ||
resource.method, | ||
url, | ||
data, | ||
_.get(opts, 'request', {}), | ||
function(err, resp){ | ||
if(err){ | ||
rej(err); | ||
} else if(resp.statusCode == 200){ | ||
res(resp.body); | ||
} else { | ||
var myError = new Error("Resource " + target + " replied with " + resp.statusCode + " when requesting " + service + ". The url is " + url); | ||
myError.statusCode = resp.statusCode; | ||
rej(myError); | ||
} | ||
}); | ||
} catch(error){ | ||
rej(error); | ||
} | ||
}); | ||
return new Promise(function(res, rej){ | ||
try{ | ||
needle.request( | ||
resource.method, | ||
url, | ||
data, | ||
_.get(opts, 'request', {}), | ||
function(err, resp){ | ||
if(err){ | ||
rej(err); | ||
} else if(resp.statusCode == 200){ | ||
res(resp.body); | ||
} else { | ||
var myError = new Error("Resource " + target + " replied with " + resp.statusCode + " when requesting " + service + ". The url is " + url); | ||
myError.statusCode = resp.statusCode; | ||
rej(myError); | ||
} | ||
}); | ||
} catch(error){ | ||
rej(error); | ||
} | ||
}); | ||
} | ||
}; |
{ | ||
"name": "sparc-commons", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Library with all small time common stuff used across the SPARC echosystem", | ||
@@ -10,3 +10,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "export NODE_CONFIG_DIR=./test/config/ && mocha" | ||
"test:debug": "export NODE_CONFIG_DIR=./test/config && mocha --debug-brk", | ||
"test": "export NODE_CONFIG_DIR=./test/config && mocha" | ||
}, | ||
@@ -18,3 +19,4 @@ "author": "Emanuele Francioni <emanuele@gosparc.com> (http://gosparc.com)", | ||
"nock": "^3.2.0", | ||
"sinon": "^1.17.2" | ||
"sinon": "^1.17.2", | ||
"xml2js": "^0.4.15" | ||
}, | ||
@@ -27,4 +29,2 @@ "repository": { | ||
"config": "^1.17.1", | ||
"crypto": "0.0.3", | ||
"handlebars": "^4.0.5", | ||
"highland": "^2.5.1", | ||
@@ -34,4 +34,5 @@ "lodash": "^3.10.1", | ||
"moment": "^2.10.6", | ||
"polyline": "^0.2.0", | ||
"needle": "^0.11.0" | ||
} | ||
} |
{ | ||
"version": "0.2.4", | ||
"external-resource-timeout": 6000, | ||
"environment": "vm", | ||
"logging": { | ||
@@ -19,4 +22,62 @@ "appenders": [ | ||
"device_server": { | ||
"port": 7001, | ||
"cache": { | ||
"target": "/tmp/routecache.db" | ||
}, | ||
"dispatcher": { | ||
"parking": "http://localhost:8000/parking" | ||
} | ||
}, | ||
"test_server": { | ||
"port": 7002 | ||
}, | ||
"web_server": { | ||
"port": 8080 | ||
}, | ||
"services": { | ||
"route-tracker": { | ||
"allowed-distance-on-start-km": 0.4 | ||
}, | ||
"route-correction": { | ||
"enabled": true, | ||
"interval-millis": 300000, | ||
"max-batch-size": 20, | ||
"route-point-batch-size": 10, | ||
"max-routepoint-interval-millis": 1080000 //18 minutes of max interval between routepoints before splitting routes | ||
} | ||
}, | ||
//TODO: hook this up in a http making library | ||
"resource": { | ||
"default": { | ||
"request": { | ||
"timeout": 3000 | ||
} | ||
}, | ||
"beestar": { | ||
"method": "get", | ||
"url": "http://hq.beestar.eu:8080", | ||
"uri": { | ||
"reverse-geocoding": "reverse.php" | ||
} | ||
}, | ||
"trackmatch": { | ||
"method": "post", | ||
"url": "http://test.roadmatching.com/rest", | ||
"uri": { | ||
"correction-service": "mapmatch/" | ||
}, | ||
"api-params": { | ||
"app_key": "67ea88d046968a417c87ead1dbaec738", | ||
"app_id": "7a3946af" | ||
} | ||
}, | ||
"test_resource": { | ||
@@ -23,0 +84,0 @@ "method": "get", |
@@ -1,1 +0,16 @@ | ||
{"resource":{"test_resource":{"method":"get","url":"http://bogus.com","uri":{"boguservice":"bellobell","boguservice2":"gallogall"},"api-params":{"apikey":"asdfg"}}}} | ||
{ | ||
//TODO: hook this up in a http making library | ||
"resource": { | ||
"test_resource": { | ||
"method": "get", | ||
"url": "http://bogus.com", | ||
"uri": { | ||
"boguservice": "bellobell", | ||
"boguservice2": "gallogall" | ||
}, | ||
"api-params": { | ||
"apikey": "asdfg" | ||
} | ||
} | ||
} | ||
} |
@@ -6,3 +6,3 @@ var log = require('..').log; | ||
var assert = require('chai').assert; | ||
var resources = require('..').resources; | ||
var requestResource = require('..').requestResource; | ||
@@ -17,3 +17,3 @@ describe("[RESOURCE TESTS - dependent on config/default.json]", function(){ | ||
return resources.requestResource("test_resource", "boguservice") | ||
return requestResource("test_resource", "boguservice") | ||
.then(function(result){ | ||
@@ -30,3 +30,3 @@ assert.equal("Hallo", result); | ||
return resources.requestResource("test_resource", "boguservice") | ||
return requestResource("test_resource", "boguservice") | ||
.catch(function(err){ | ||
@@ -33,0 +33,0 @@ return 123; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
41872
7
14
935
4
2
1
+ Addedpolyline@^0.2.0
+ Addedpolyline@0.2.0(transitive)
- Removedcrypto@0.0.3
- Removedhandlebars@^4.0.5
- Removedcrypto@0.0.3(transitive)
- Removedhandlebars@4.7.8(transitive)
- Removedneo-async@2.6.2(transitive)
- Removedsource-map@0.6.1(transitive)
- Removeduglify-js@3.19.3(transitive)
- Removedwordwrap@1.0.0(transitive)