Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = function (api_key,api_secret, options) {

var base_url = options.base_url || 'https://api.semantics3.com/v1/';
var userAgent = 'Semantics3 Node.js Lib/' + VERSION;
if (options.userAgent) {
userAgent = options.userAgent;
}

var customHeaders = {"Accept" : "*/*", "Connection" : "close", "User-Agent" : userAgent};
var securer = new OAuth(null, null, api_key, api_secret,'1.0', null,'HMAC-SHA1',32,customHeaders);
Expand All @@ -32,12 +35,12 @@ module.exports = function (api_key,api_secret, options) {
method = method.toLowerCase();
if( method === 'get'){
var request_data = encodeURIComponent(data);
var url = normalizeUrl(base_url + path + '?q=' + request_data);
var url = normalizeUrl(base_url + path) + '?q=' + request_data;

securer.get(url, null, null,
function(err, data, result) {
if(!err) { query_result = JSON.parse(data); }
callback(err,data);
if(!err) { query_result = JSON.parse(data); }
callback(err,data);
});
}
else if( method == 'delete' ){
Expand All @@ -53,13 +56,14 @@ module.exports = function (api_key,api_secret, options) {
securer[method](url, null, null, data, "application/json",
function(err, data, result) {
if(!err) { query_result = JSON.parse(data); }
callback(err,data);
callback(err, data);
});
}
}

function run_query(endpoint) {
var queryObj = {};
data_query[endpoint] = data_query[endpoint] || {};

if(arguments.length < 2) {
throw new Error("Insufficient parameters provided");
Expand All @@ -86,7 +90,7 @@ module.exports = function (api_key,api_secret, options) {
else if(arguments.length == 4){
data = arguments[1];
method = arguments[2];
}
}

if(typeof method === 'undefined')
method = 'GET';
Expand All @@ -95,7 +99,12 @@ module.exports = function (api_key,api_secret, options) {
}
else {
if(typeof data === 'string') {
_request(method, endpoint,JSON.parse(data),callback);
try {
var parsed_data = JSON.parse(data);
_request(method, endpoint, parsed_data, callback);
} catch (e) {
callback("Invalid JSON");
}
}
else if(typeof data === 'object') {
_request(method, endpoint,data,callback);
Expand Down