Commit c9b43362 authored by Sina Abadi's avatar Sina Abadi

Add log when response not equal to 200

parent 06257b86
'use strict';
'use strict'
var request = require('request');
var constants = require('./constants');
var request = require('request')
var constants = require('./constants')
var proxyUrl
var urlModule = require('url')
var HttpsProxyAgent = require('https-proxy-agent')
var ALLOWED_CREDENTIALS = [
{ name: 'userAuthKey', type: 'string' },
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
{ name: 'apps', type: 'object'}
];
{name: 'userAuthKey', type: 'string'},
{name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId']},
{name: 'apps', type: 'object'}
]
/**
* make a basic request
......@@ -23,8 +23,8 @@ var basicRequest = function (url, apiKey, method, body, callback) {
var options = {
url: url,
method: method,
agent: new HttpsProxyAgent(urlModule.parse(proxyUrl)),
};
agent: new HttpsProxyAgent(urlModule.parse(proxyUrl))
}
if (apiKey) {
options.headers = {
'Content-Type': 'application/json; charset=utf-8',
......@@ -32,22 +32,27 @@ var basicRequest = function (url, apiKey, method, body, callback) {
}
}
if (body) {
options.body = body;
options.json = true;
options.body = body
options.json = true
}
return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) {
if (err) {
callback && callback(err, httpResponse, data);
return reject(err);
callback && callback(err, httpResponse, data)
return reject(err)
}
callback && callback(err, httpResponse, data);
return resolve({ httpResponse: httpResponse, data: data });
});
});
};
if (httpResponse.statusCode !== 200) {
console.log('**********************')
console.log(JSON.stringify(data, null, 2))
process.exit(1)
console.log('**********************')
}
callback && callback(err, httpResponse, data)
return resolve({httpResponse: httpResponse, data: data})
})
})
}
/**
* check if the credential is valid
......@@ -59,20 +64,20 @@ var checkCredential = function (credentialName, credential) {
for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
if (ALLOWED_CREDENTIALS[i].name === credentialName) {
if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type;
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type
}
if (ALLOWED_CREDENTIALS[i].requiredFields) {
for (var j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) {
if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) {
throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j]
throw credentialName + ' must contain ' + ALLOWED_CREDENTIALS[i].requiredFields[j]
}
}
}
return true;
return true
}
}
return false;
};
return false
}
/**
*
......@@ -85,13 +90,13 @@ var Client = function (credentials, httpsProxyUrl) {
if (typeof credentials !== 'object') {
throw 'credentials parameter must be a JSON object'
}
this.API_URI = constants.API_ROOT;
this.API_URI = constants.API_ROOT
for (var key in credentials) {
if (credentials.hasOwnProperty(key) && checkCredential(key, credentials[key])) {
this[key] = credentials[key];
this[key] = credentials[key]
}
}
};
}
/**
......@@ -102,17 +107,17 @@ Client.prototype.setRootUrl = function (rootUrl) {
if (!rootUrl) {
throw 'You must set a valid rootUsrl.'
}
this.API_URI = rootUrl;
};
this.API_URI = rootUrl
}
/**
* set an app object
* @param app { JSON } {}
*/
Client.prototype.setApp = function (app) {
checkCredential('app', app);
this.app = app;
};
checkCredential('app', app)
this.app = app
}
/**
* create a notification
......@@ -121,32 +126,32 @@ Client.prototype.setApp = function (app) {
*/
Client.prototype.sendNotification = function (notification, callback) {
if (!notification || !notification.postBody) {
throw 'notification parameter must be a typeof Notification object.';
throw 'notification parameter must be a typeof Notification object.'
}
var postBody = notification.postBody;
var postBody = notification.postBody
if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
postBody.app_ids = this.apps
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback)
}
if (this.app) {
postBody.app_id = this.app.appId;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
postBody.app_id = this.app.appId
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback)
}
throw 'You must set either an "app" or "apps" on Client';
};
throw 'You must set either an "app" or "apps" on Client'
}
/**
* Used to stop a scheduled or currently outgoing notification.
* @param notificationId { String } Notification id
* @param notificationId { String } Notification id
* @param callback
*/
Client.prototype.cancelNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
};
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback)
}
/**
*
......@@ -157,9 +162,9 @@ Client.prototype.viewNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
};
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback)
}
/**
*
......@@ -170,9 +175,9 @@ Client.prototype.viewNotifications = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
};
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback)
}
/**
......@@ -183,8 +188,8 @@ Client.prototype.viewApps = function (callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
};
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback)
}
/**
*
......@@ -195,8 +200,8 @@ Client.prototype.viewApp = function (appId, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
};
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback)
}
/**
*
......@@ -205,17 +210,17 @@ Client.prototype.viewApp = function (appId, callback) {
*/
Client.prototype.createApp = function (body, callback) {
if (!body.name) {
throw 'You must specify a name in body';
throw 'You must specify a name in body'
}
if (!this.userAuthKey) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
};
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback)
}
/**
* Updates currently defined app
* @param body { JSON }
* @param body { JSON }
* @param callback
*/
Client.prototype.updateApp = function (body, callback) {
......@@ -225,8 +230,8 @@ Client.prototype.updateApp = function (body, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
};
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback)
}
/**
......@@ -238,9 +243,9 @@ Client.prototype.viewDevices = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback)
}
/**
*
......@@ -251,9 +256,9 @@ Client.prototype.viewDevice = function (deviceId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback)
}
/**
......@@ -266,10 +271,10 @@ Client.prototype.addDevice = function (body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
};
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback)
}
/**
*
......@@ -282,10 +287,10 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
};
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback)
}
/**
*
......@@ -298,10 +303,10 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
};
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback)
}
/**
*
......@@ -312,9 +317,9 @@ Client.prototype.csvExport = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
};
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback)
}
/**
*
......@@ -328,11 +333,11 @@ Client.prototype.newSession = function (playerId, body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_session';
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback);
};
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_session'
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback)
}
/**
*
......@@ -346,11 +351,11 @@ Client.prototype.newPurchase = function (playerId, body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_purchase';
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback);
};
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_purchase'
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback)
}
/**
*
......@@ -364,10 +369,10 @@ Client.prototype.incrementSessionLength = function (playerId, body, callback) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
body.app_id = this.app.appId
}
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_focus';
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback);
};
var requestUri = this.API_URI + constants.DEVICES_PATH + '/' + playerId + '/on_focus'
return basicRequest(requestUri, this.app.appAuthKey, 'POST', body, callback)
}
module.exports = Client;
module.exports = Client
{
"name": "onesignal-node",
"version": "2.0.1",
"version": "2.0.2",
"description": "A Node.js Library for OneSignal push notification service",
"main": "./lib/index.js",
"scripts": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment