/*jshint -W069 */
/**
*
* @class abuseTickets
* @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
* @param {string} [domainOrOptions.domain] - The project domain
* @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
*/
var abuseTickets = (function() {
'use strict';
var request = require('request');
var Q = require('q');
function abuseTickets(options) {
var domain = (typeof options === 'object') ? options.domain : options;
this.domain = domain ? domain : 'https://api.godaddy.com';
if (this.domain.length === 0) {
throw new Error('Domain parameter must be specified as a string.');
}
this.token = (typeof options === 'object') ? (options.token ? options.token : {}) : {};
}
/**
* Set Token
* @method
* @name abuseTickets#setToken
* @param {string} value - token's value
* @param {string} headerOrQueryName - the header or query name to send the token at
* @param {boolean} isQuery - true if send the token as query param, otherwise, send as header param
*
*/
abuseTickets.prototype.setToken = function(value, headerOrQueryName, isQuery) {
this.token.value = value;
this.token.headerOrQueryName = headerOrQueryName;
this.token.isQuery = isQuery;
};
/**
* List all abuse tickets ids that match user provided filters
* @method
* @name abuseTickets#getTickets
* @param {string} type - The type of abuse.
* @param {boolean} closed - Is this abuse ticket closed?
* @param {string} sourceDomainOrIp - The domain name or ip address abuse originated from
* @param {string} target - The brand/company the abuse is targeting. ie: brand name/bank name
* @param {string} createdStart - The earliest abuse ticket creation date to pull abuse tickets for
* @param {string} createdEnd - The latest abuse ticket creation date to pull abuse tickets for
* @param {integer} limit - Number of abuse ticket numbers to return.
* @param {integer} offset - The earliest result set record number to pull abuse tickets for
*
*/
abuseTickets.prototype.getTickets = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/abuse/tickets';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
if (parameters['type'] !== undefined) {
queryParameters['type'] = parameters['type'];
}
if (parameters['closed'] !== undefined) {
queryParameters['closed'] = parameters['closed'];
}
if (parameters['sourceDomainOrIp'] !== undefined) {
queryParameters['sourceDomainOrIp'] = parameters['sourceDomainOrIp'];
}
if (parameters['target'] !== undefined) {
queryParameters['target'] = parameters['target'];
}
if (parameters['createdStart'] !== undefined) {
queryParameters['createdStart'] = parameters['createdStart'];
}
if (parameters['createdEnd'] !== undefined) {
queryParameters['createdEnd'] = parameters['createdEnd'];
}
if (parameters['limit'] !== undefined) {
queryParameters['limit'] = parameters['limit'];
}
if (parameters['offset'] !== undefined) {
queryParameters['offset'] = parameters['offset'];
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
/**
* Create a new abuse ticket
* @method
* @name abuseTickets#createTicket
* @param {AbuseTicketCreate} body - The endpoint which allows the Reporter to create a new abuse ticket
*
*/
abuseTickets.prototype.createTicket = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/abuse/tickets';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
if (parameters['body'] !== undefined) {
body = parameters['body'];
}
if (parameters['body'] === undefined) {
deferred.reject(new Error('Missing required body parameter: body'));
return deferred.promise;
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'POST',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
/**
* Return the abuse ticket data for a given ticket id
* @method
* @name abuseTickets#getTicketInfo
* @param {string} ticketId - A unique abuse ticket identifier
*
*/
abuseTickets.prototype.getTicketInfo = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/abuse/tickets/{ticketId}';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
path = path.replace('{ticketId}', parameters['ticketId']);
if (parameters['ticketId'] === undefined) {
deferred.reject(new Error('Missing required path parameter: ticketId'));
return deferred.promise;
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
return abuseTickets;
})();
exports.abuseTickets = abuseTickets;