Permalink
Browse files

Added SQS to the mix…

  • Loading branch information...
1 parent 8c7a9e4 commit 7181f28f090f7e1144caea6b86f76ae751b91107 Jordan Ryan Moore committed Jan 23, 2012
View
@@ -122,3 +122,21 @@ A potentially outdated list of supported AWS services and methods is provided be
* setTopicAttributes
* subscribe
* unsubscribe
+
+### SQS (Simple Queue Service)
+
+ * createQueue
+ * deleteQueue
+ * listQueues
+ * getQueueUrl
+ * getQueueAttributes
+ * setQueueAttributes
+ * addPermission
+ * removePermission
+ * sendMessage
+ * sendMessageBatch
+ * receiveMessage
+ * deleteMessage
+ * deleteMessageBatch
+ * changeMessageVisibility
+ * changeMessageVisibilityBatch
View
@@ -24,7 +24,7 @@ var services = [
'ses',
's3',
'sns',
- //'sqs',
+ 'sqs',
//'sts',
];
View
@@ -0,0 +1,99 @@
+var
+ util = require('util'),
+ url = require('url'),
+ aws = require('../aws');
+
+// Version Information
+var version = '2011-10-01';
+var xmlns = 'http://sqs.amazonaws.com/doc/' + version + '/';
+var defaultEndpoint = 'sqs.us-east-1.amazonaws.com';
+var methods = [
+ // Actions for Queues
+ 'createQueue',
+ 'deleteQueue',
+ 'listQueues',
+ 'getQueueUrl',
+ 'getQueueAttributes',
+ 'setQueueAttributes',
+
+ // Actions for Access Control on Queues
+ 'addPermission',
+ 'removePermission',
+
+ // Actions for Messages
+ 'sendMessage',
+ 'sendMessageBatch',
+ 'receiveMessage',
+ 'deleteMessage',
+ 'deleteMessageBatch',
+ 'changeMessageVisibility',
+ 'changeMessageVisibilityBatch',
+];
+
+/**
+ * A generic SQS request.
+ *
+ * @param {String} endpoint
+ */
+var Request = function(endpoint) {
+ aws.QueryRequest.call(this);
+
+ var self = this;
+ self.host = endpoint || defaultEndpoint;
+ self.query.Version = version;
+ self.query.SignatureMethod = 'Hmac' + self.signatureAlgorithm.toUpperCase();
+ self.query.SignatureVersion = 2;
+ self.query.Timestamp = self.date.toISOString().replace(/\.[0-9]{0,3}Z$/, 'Z');
+}
+
+util.inherits(Request, aws.QueryRequest);
+
+/**
+ * Parses the given arguments, adding the queue URL to the request.
+ *
+ * @param {Object} args
+ */
+Request.prototype.encodeQueueUrl = function(args) {
+ if ('undefined' !== typeof args.queueUrl) {
+ var queryUrl = url.parse(args.queueUrl);
+
+ if (queryUrl.host) {
+ this.host = queryUrl.host;
+ }
+
+ this.path = queryUrl.pathname;
+ }
+};
+
+/**
+ * A generic SNS response.
+ *
+ * @param {Object} httpResponse
+ */
+var Response = function(httpResponse) {
+ aws.Response.call(this, httpResponse);
+
+ var self = this;
+ var error = self.xmlToJson(self.xml.get('/ErrorResponse/Error'));
+
+ if (error) {
+ throw new aws.ResponseException(
+ error.Message,
+ error.Code,
+ self.xmlToJson(self.xml.get('/ErrorResponse/RequestId'))
+ );
+ }
+
+ self.requestId = self.xmlToJson(self.xml.root().get('./ResponseMetadata/RequestId'));
+}
+
+util.inherits(Response, aws.Response);
+
+// Exports
+module.exports.version = version;
+module.exports.xmlns = xmlns;
+module.exports.defaultEndpoint = defaultEndpoint;
+module.exports.methods = methods;
+module.exports.methodPath = __dirname + '/sqs/';
+module.exports.Request = Request;
+module.exports.Response = Response;
@@ -0,0 +1,34 @@
+/**
+ * GET AddPermission
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'AddPermission';
+
+ request.encodeQueueUrl(args);
+
+ if (util.isArray(args.awsAccountIds) {
+ for (var i = 0; i < args.awsAccountIds.length; i++) {
+ request.query['AWSAccountId.' + (i + 1)] = args.awsAccountIds[i];
+ }
+ }
+
+ if (util.isArray(args.actionNames) {
+ for (var i = 0; i < args.actionNames.length; i++) {
+ request.query['ActionName.' + (i + 1)] = args.actionNames[i];
+ }
+ }
+
+ if ('undefined' !== typeof args.label) {
+ request.query['Label'] = args.label;
+ }
+};
+
+/**
+ * AddPermissionResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {};
@@ -0,0 +1,26 @@
+/**
+ * GET ChangeMessageVisibility
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'ChangeMessageVisibility';
+
+ request.encodeQueueUrl(args);
+
+ if ('undefined' !== typeof args.receiptHandle) {
+ request.query['ReceiptHandle'] = args.receiptHandle;
+ }
+
+ if ('undefined' !== typeof args.visibilityTimeout) {
+ request.query['VisibilityTimeout'] = args.visibilityTimeout;
+ }
+};
+
+/**
+ * ChangeMessageVisibilityResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {};
@@ -0,0 +1,62 @@
+/**
+ * GET ChangeMessageVisibilityBatch
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'ChangeMessageVisibilityBatch';
+
+ request.encodeQueueUrl(args);
+
+ if (Array.isArray(args.changeMessageVisibilityBatchRequestEntries)) {
+ var entries = args.changeMessageVisibilityBatchRequestEntries;
+
+ for (var i = 0; i < entries.length; i++) {
+ var entry = entries[i];
+
+ if ('undefined' !== typeof entry.id) {
+ request.query['ChangeMessageVisibilityBatchRequestEntry.' + (i + 1) + '.Id'] = entry.id;
+ }
+
+ if ('undefined' !== typeof entry.receiptHandle) {
+ request.query['ChangeMessageVisibilityBatchRequestEntry.' + (i + 1) + '.ReceiptHandle'] = entry.receiptHandle;
+ }
+
+ if ('undefined' !== typeof entry.visibilityTimeout) {
+ request.query['ChangeMessageVisibilityBatchRequestEntry.' + (i + 1) + '.VisibilityTimeout'] = entry.visibilityTimeout;
+ }
+ }
+ }
+};
+
+/**
+ * ChangeMessageVisibilityBatchResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {
+ var result = response.xml.get('/ChangeMessageVisibilityBatchResponse/ChangeMessageVisibilityBatchResult');
+
+ response.data.changeMessageVisibilityBatchResultEntries = [];
+ response.data.batchResultErrorEntries = [];
+
+ result.find('./ChangeMessageVisibilityBatchResultEntry').forEach(function(xmlEntry) {
+ var entry = response.xmlToJson(xmlEntry);
+
+ response.data.deleteMessageBatchResultEntries.push({
+ id: entry.Id,
+ });
+ });
+
+ result.find('./BatchResultErrorEntry').forEach(function(xmlEntry) {
+ var entry = response.xmlToJson(xmlEntry);
+
+ response.data.batchResultErrorEntries.push({
+ id: entry.Id,
+ senderFault: 'true' === entry.SenderFault,
+ code: entry.Code,
+ message: entry.Message,
+ });
+ });
+};
@@ -0,0 +1,44 @@
+var util = require('util');
+
+/**
+ * GET CreateQueue
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'CreateQueue';
+
+ if ('undefined' !== typeof args.queueName) {
+ request.query['QueueName'] = args.queueName;
+ }
+
+ if (util.isArray(args.attributes)) {
+ for (var i = 0; i < args.attributes.length; i++) {
+ var attribute = args.attributes[i];
+
+ if (attribute instanceof Object) {
+ switch (attribute.name) {
+ case 'Policy':
+ if ('object' === typeof attribute.value && !(attribute.value instanceof String)) {
+ attribute.value = JSON.stringify(attribute.value);
+ }
+
+ break;
+ }
+
+ request.query['Attribute.' + (i + 1) + '.Name'] = attribute.name;
+ request.query['Attribute.' + (i + 1) + '.Value'] = attribute.value;
+ }
+ };
+ }
+};
+
+/**
+ * CreateQueueResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {
+ response.data.queueUrl = response.xmlToJson(response.xml.get('/CreateQueueResponse/CreateQueueResult/QueueUrl'));
+};
@@ -0,0 +1,22 @@
+/**
+ * GET DeleteMessage
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'DeleteMessage';
+
+ request.encodeQueueUrl(args);
+
+ if ('undefined' !== typeof args.receiptHandle) {
+ request.query['ReceiptHandle'] = args.receiptHandle;
+ }
+};
+
+/**
+ * DeleteMessageResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {};
@@ -0,0 +1,58 @@
+/**
+ * GET DeleteMessageBatch
+ *
+ * @param {Request} request
+ * @param {Object} args
+ */
+module.exports.encodeRequest = function(request, args) {
+ request.query['Action'] = 'DeleteMessageBatch';
+
+ request.encodeQueueUrl(args);
+
+ if (Array.isArray(args.deleteMessageBatchRequestEntries)) {
+ var entries = args.deleteMessageBatchRequestEntries;
+
+ for (var i = 0; i < entries.length; i++) {
+ var entry = entries[i];
+
+ if ('undefined' !== typeof entry.id) {
+ request.query['DeleteMessageBatchRequestEntry.' + (i + 1) + '.Id'] = entry.id;
+ }
+
+ if ('undefined' !== typeof entry.receiptHandle) {
+ request.query['DeleteMessageBatchRequestEntry.' + (i + 1) + '.ReceiptHandle'] = entry.receiptHandle;
+ }
+ }
+ }
+};
+
+/**
+ * DeleteMessageBatchResponse
+ *
+ * @param {Response} response
+ */
+module.exports.decodeResponse = function(response) {
+ var result = response.xml.get('/DeleteMessageBatchResponse/DeleteMessageBatchResult');
+
+ response.data.deleteMessageBatchResultEntries = [];
+ response.data.batchResultErrorEntries = [];
+
+ result.find('./DeleteMessageBatchResultEntry').forEach(function(xmlEntry) {
+ var entry = response.xmlToJson(xmlEntry);
+
+ response.data.deleteMessageBatchResultEntries.push({
+ id: entry.Id,
+ });
+ });
+
+ result.find('./BatchResultErrorEntry').forEach(function(xmlEntry) {
+ var entry = response.xmlToJson(xmlEntry);
+
+ response.data.batchResultErrorEntries.push({
+ id: entry.Id,
+ senderFault: 'true' === entry.SenderFault,
+ code: entry.Code,
+ message: entry.Message,
+ });
+ });
+};
Oops, something went wrong.

0 comments on commit 7181f28

Please sign in to comment.