postage
Version:
Node.JS PostageApp API Client
168 lines (128 loc) • 3.5 kB
JavaScript
/**
* @class postage
* @params {String} API_KEY
*/
var http = require('http');
var postage = module.exports = function(API_KEY) {
if (API_KEY === undefined) {
return new Error('Please call w/ an API Key!');
} else {
postage.API_KEY = API_KEY;
};
return postage;
};
/**
* @class postageapp
*/
var postageapp = {};
/**
* @class postageapp
* @params {String} path API Path
* @method request
* @params {Object} options See README.md
* @params {Function} callback Method to execute on complete
*/
postageapp.request = function(path, payload, callback) {
callback = callback || function(){};
var API_Request_Object = {
host: 'api.postageapp.com',
port: 80,
path: path,
method: 'POST',
headers: {
'host': 'api.postageapp.com',
'content-type': 'application/json',
'user-agent': 'npm install postage'
}
};
var _request = http.request(API_Request_Object, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
var data = JSON.parse(chunk);
if (response.statusCode === 200) {
callback(null, data);
} else if (response.statusCode === 401) {
callback(new Error('Invalid or Expired API Key!'), null);
} else if (response.statusCode === 400 || response.statusCode === 412) {
callback(new Error('Invalid API Call Arguments!'), null);
} else {
callback(new Error('Unknown Error!'), null);
}
});
});
_request.on('error', function(error) {
callback(error, null);
});
_request.end(JSON.stringify(payload));
};
/**
* @class postage
* @method send Send an email over PostageApp
* @params {Object} options See README.md
* @optional {Function} callback
*/
postage.send = function(options, callback){
var payload = {
api_key: postage.API_KEY,
uid: new Date().getTime(),
arguments: {
recipients: options.recipients,
headers: {
subject: options.subject,
from: options.from
},
content: options.content,
template: options.template,
variables: options.variables
}
}
postageapp.request('/v.1.0/send_message.json', payload, callback);
};
/**
* @class postage
* @method get_message_receipt Retrieve a message receipt by uid
* @params {Object} options See README.md
* @optional {Function} callback
*/
postage.get_message_receipt = function(uid, callback){
var payload = {
api_key: postage.API_KEY,
uid: uid
};
postageapp.request('/v.1.0/get_message_receipt.json', payload, callback);
};
/**
* @class postage
* @method account_info Retrieve account info
* @optional {Function} callback
*/
postage.get_account_info = function(callback){
var payload = {
api_key: postage.API_KEY,
uid: new Date().getTime()
};
postageapp.request('/v.1.0/get_account_info.json', payload, callback);
};
/**
* @class postage
* @method get_project_info Retrieve project info
* @optional {Function} callback
*/
postage.get_project_info = function(callback){
var payload = {
api_key: postage.API_KEY
};
postageapp.request('/v.1.0/get_project_info.json', payload, callback);
};
/**
* @class postage
* @method get_method_list Retrieve available API methods
* @optional {Function} callback
*/
postage.get_method_list = function(callback){
var payload = {
api_key: postage.API_KEY
};
postageapp.request('/v.1.0/get_method_list.json', payload, callback);
};
/* EOF */