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