Skip to content

Commit

Permalink
adding more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonPereira1991 committed Apr 19, 2018
1 parent 1d6081e commit a57034c
Showing 1 changed file with 168 additions and 90 deletions.
258 changes: 168 additions & 90 deletions node-b2share-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,44 +98,70 @@ B2ShareClient.prototype.listCommunities = function (callback) {
* @param callback
*/
B2ShareClient.prototype.getCommunitySchema = function (communityID, callback) {
communityID = querystring.escape(communityID);
const params = {
host: this.host,
path: "/api/communities/" + communityID + "/schemas/last?access_token=" + this.accessToken,
method: "GET"
const validInputs = function ()
{
try
{
if(!(typeof communityID === "string" || communityID instanceof String))
{
return false;
}
else
{
return true;
}
}
catch (err)
{
return false;
}
};

let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
if(validInputs() === true)
{
communityID = querystring.escape(communityID);
const params = {
host: this.host,
path: "/api/communities/" + communityID + "/schemas/last?access_token=" + this.accessToken,
method: "GET"
};

let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
});

response.on("end", function () {
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = null;
}
callback(hasError, result);
});
});

response.on("end", function () {
req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
"statusCode": "500",
"statusMessage": "Error getting the community schema"
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = false;
}
callback(hasError, result);
callback(true, result);
});
});

req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": "500",
"statusMessage": "Error getting the community schema"
};
callback(true, result);
});

req.end();
req.end();
}
else
{
callback(true, "Invalid communityID");
}
};

/**
Expand Down Expand Up @@ -188,41 +214,67 @@ B2ShareClient.prototype.listAllRecords = function (callback) {
* @param callback
*/
B2ShareClient.prototype.listRecordsPerCommunity = function (communityID, callback) {
communityID = querystring.escape(communityID);
const params = {
host: this.host,
path: "/api/records/?q=community:" + communityID + "?access_token=" + this.accessToken,
method: "GET"
const validInputs = function ()
{
try
{
if(!(typeof communityID === "string" || communityID instanceof String))
{
return false;
}
else
{
return true;
}
}
catch (err)
{
return false;
}
};
let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
});

response.on("end", function () {
if(validInputs() === true)
{
communityID = querystring.escape(communityID);
const params = {
host: this.host,
path: "/api/records/?q=community:" + communityID + "?access_token=" + this.accessToken,
method: "GET"
};
let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
});

response.on("end", function () {
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = false;
}
callback(hasError, result);
});
});
req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
"statusCode": "500",
"statusMessage": "Error getting records per community"
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = false;
}
callback(hasError, result);
callback(true, result);
});
});
req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": "500",
"statusMessage": "Error getting records per community"
};
callback(true, result);
});
req.end();
req.end();
}
else
{
callback(true, "Invalid communityID");
}
};

/**
Expand All @@ -231,41 +283,67 @@ B2ShareClient.prototype.listRecordsPerCommunity = function (communityID, callbac
* @param callback
*/
B2ShareClient.prototype.searchRecords = function (queryString, callback) {
queryString = querystring.escape(queryString);
const params = {
host: this.host,
path: "/api/records/?q=" + queryString + "?access_token=" + this.accessToken,
method: "GET"
const validInputs = function ()
{
try
{
if(!(typeof queryString === "string" || queryString instanceof String))
{
return false;
}
else
{
return true;
}
}
catch (err)
{
return false;
}
};
let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
});

response.on("end", function () {
if(validInputs() === true)
{
queryString = querystring.escape(queryString);
const params = {
host: this.host,
path: "/api/records/?q=" + queryString + "?access_token=" + this.accessToken,
method: "GET"
};
let body = "";
let hasError = true;
let req = https.request(params, function (response) {
response.on("data", function (chunk) {
body += chunk;
});

response.on("end", function () {
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = false;
}
callback(hasError, result);
});
});
req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": response.statusCode,
"statusMessage": response.statusMessage
"statusCode": "500",
"statusMessage": "Error searching a record"
};
if(response.statusCode === 200)
{
result.data = JSON.parse(body);
hasError = false;
}
callback(hasError, result);
callback(true, result);
});
});
req.on("error", function (e) {
console.log(e);
let result = {
"statusCode": "500",
"statusMessage": "Error searching a record"
};
callback(true, result);
});
req.end();
req.end();
}
else
{
callback(true, "Invalid queryString");
}
};

/**
Expand Down

0 comments on commit a57034c

Please sign in to comment.