Permalink
Browse files

Added AlRequest method with basic _do_post method.

  • Loading branch information...
1 parent 789a0f5 commit 84533482f9af35b4c279740873ef30a35fb320aa Chavez committed Mar 22, 2012
Showing with 266 additions and 10 deletions.
  1. +14 −5 README.md
  2. +1 −1 lib/AlConfig.js
  3. +241 −0 lib/AlHttp.js
  4. +2 −2 lib/AlPapi.js
  5. +8 −2 lib/AlRequest.js
View
@@ -2,8 +2,17 @@ npm install -g
npm link
node // for console
-// Example So Far
-var al = require('al-papi');
-al.AlConfig('my-api-key');
-var req = new al.AlRequest();
-req.post('params', function(){ console.log('callback') } );
+ // Example So Far
+ var al = require('al-papi');
+ al.AlConfig('yRRBtB99jSIovMy6y6K0');
+ var req = new al.AlRequest();
+ result = req.post({'keyword' : 'Centaur Carnage'}, function(error, result) {
+ if (error)
+ console.log('Error: ' + (error.code ? error.code + ' ' + error.message : error.message));
+
+ if (result)
+ return result;
+ });
+ console.log('finished');
+ console.log(JSON.parse(result.data));
+
View
@@ -1,7 +1,7 @@
var Util = require('util');
var AlConfig = function(_api_key) {
- this.api_key = _api_key;
+ this.api_key = _api_key;
global.al_config = { "api_key" : _api_key };
};
View
@@ -0,0 +1,241 @@
+var Util = require('util');
+var Events = require('events');
+var request = require('request');
+var QueryString = require('querystring');
+var Util = require('util');
+
+var AlHttp = function() {
+ Events.EventEmitter.call(this);
+
+ /** @private */
+ this.baseUrl = "http://api.authoritylabs.com";
+ this.api_key = global.al_config.api_key;
+ this._connections = {};
+}
+
+Util.inherits(AlHttp, Events.EventEmitter);
+
+AlHttp.prototype._do_post = function(params, callback) {
+ params['auth_token'] = this.api_key;
+ var queryParams = QueryString.stringify(params);
+ var apiUrl = this.baseUrl + '/keywords.json'
+
+ if (queryParams != undefined && queryParams.length > 0)
+ apiUrl += '?' + queryParams;
+
+ var requestOptions = { method: 'POST', url: apiUrl };
+ var httpRequest = request.post(requestOptions);
+
+ this._connections[httpRequest.hash] = { callback: callback, data: '', httpRequest: httpRequest };
+ this._createEventListenersForRequest(httpRequest);
+}
+
+/**
+ * Returns the connection object associated with the specified key.
+ *
+ * @private
+ * @this {Client}
+ * @param {String} aKey
+ * @return The connection object or null.
+ */
+AlHttp.prototype._connectionForKey = function(aKey) {
+ var connection = this._connections[aKey];
+
+ return connection === undefined ? null : connection;
+};
+
+
+//
+// EVENT METHODS
+//
+
+/**
+ * Creates listeners that respond to events triggered by the specified
+ * request object.
+ *
+ * @private
+ * @this {AlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ */
+AlHttp.prototype._createEventListenersForRequest = function(aRequest) {
+ var self = this;
+
+ aRequest.on('close', function() {
+ self._requestDidClose(aRequest);
+ });
+
+ aRequest.on('data', function(data) {
+ self._requestDidReceiveData(aRequest, data);
+ });
+
+ aRequest.on('end', function() {
+ self._requestDidEnd(aRequest);
+ });
+
+ aRequest.on('error', function(error) {
+ self._requestDidFailWithError(aRequest, error);
+ });
+
+ aRequest.on('response', function(aResponse) {
+ self._requestDidReceiveResponse(aRequest, aResponse);
+ });
+};
+
+/**
+ * Removes the connection object associated with the specified key.
+ *
+ * @private
+ * @this {AlHttp}
+ * @param {String} aKey
+ */
+AlHttp.prototype._removeConnectionForKey = function(aKey) {
+ if (this._connectionForKey(aKey))
+ delete this._connections[aKey];
+};
+
+/**
+ *
+ *
+ * @private
+ * @this {StreamAlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ */
+AlHttp.prototype._requestDidClose = function(aRequest) {
+ var connection = this._connectionForKey(aRequest.hash);
+
+ if (connection !== undefined) {
+ var error = undefined;
+ var result = null;
+
+ try {
+ result = JSON.parse(connection.data);
+ }
+ catch (e) {
+ error = e;
+ }
+
+ connection.callback(error, result);
+ this._removeConnectionForKey(aRequest.hash);
+ }
+};
+
+/**
+ *
+ *
+ * @private
+ * @this {StreamAlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ */
+AlHttp.prototype._requestDidEnd = function(aRequest) {
+ var connection = this._connectionForKey(aRequest.hash);
+
+ if (connection != undefined) {
+ var error = undefined;
+ var result = null;
+
+ try {
+ result = JSON.parse(connection.data);
+ }
+ catch (e) {
+ error = e;
+ }
+ console.log(connection);
+ connection.callback(error, result);
+ this._removeConnectionForKey(aRequest.hash);
+ }
+};
+
+/**
+ * Handles a request failure.
+ *
+ * @private
+ * @this {StreamAlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ * @param {Error} aError
+ */
+AlHttp.prototype._requestDidFailWithError = function(aRequest, aError) {
+ var connection = this._connectionForKey(aRequest.hash);
+
+ if (connection !== undefined) {
+ connection.callback(aError, undefined);
+ this._removeConnectionForKey(aRequest.hash);
+ }
+};
+
+/**
+ * Handles data received from the server.
+ *
+ * @private
+ * @this {StreamAlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ * @param {Buffer} aData
+ */
+AlHttp.prototype._requestDidReceiveData = function(aRequest, aData) {
+ var connection = this._connectionForKey(aRequest.hash);
+
+ if (connection == undefined) return;
+
+ var receivedData = connection['data'] + aData.toString('utf8');
+ connection['data'] = receivedData;
+};
+
+/**
+ * Handles a response by the server.
+ *
+ * @private
+ * @this {StreamAlHttp}
+ * @param {http.AlHttpRequest} aRequest The request object.
+ * @param {http.AlHttpResponse} aResponse The response object.
+ */
+AlHttp.prototype._requestDidReceiveResponse = function(aRequest, aResponse) {
+ var connection = this._connectionForKey(aRequest.hash);
+
+ if (connection !== undefined)
+ connection['data'] = '';
+
+ var error = null;
+ if (aResponse.statusCode != 200) {
+ error = new Error();
+ error.code = aResponse.statusCode;
+ }
+
+ switch(aResponse.statusCode) {
+ case 304:
+ error.message = 'Not Modified.';
+ break;
+ case 400:
+ error.message = 'Bad Request';
+ break;
+ case 401:
+ error.message = 'Unauthorized';
+ break;
+ case 403:
+ error.message = 'Forbidden';
+ break;
+ case 404:
+ error.message = 'Not Found';
+ break;
+ case 406:
+ error.message = 'Not Acceptable';
+ break;
+ case 420:
+ error.message = 'Enhance Your Calm';
+ break;
+ case 500:
+ error.message = 'Internal Server Error';
+ break;
+ case 502:
+ error.message = 'Bad Gateway';
+ break;
+ case 503:
+ error.message = 'Service Unavailable';
+ break;
+ }
+
+ if (error != undefined) {
+ this._requestDidFailWithError(aRequest, error);
+ return;
+ }
+};
+
+module.exports = AlHttp
View
@@ -1,5 +1,5 @@
module.exports = {
- AlConfig: require('./AlConfig'),
- AlRequest: require('./AlRequest'),
+ AlConfig: require('./AlConfig'),
+ AlRequest: require('./AlRequest'),
AlResponse: require('./AlResponse')
};
View
@@ -1,16 +1,22 @@
-var AlConfig = require('./AlConfig');
+var Util = require('util');
+var AlHttp = require('./AlHttp');
var AlRequest = function(_api_key) {
this.api_key = _api_key;
global.al_config.api_key = _api_key;
+ AlHttp.call(this);
}
var AlRequest = function() {
this.api_key = global.al_config.api_key;
+ AlHttp.call(this);
}
-AlRequest.prototype.post = function(parameters, callback) {
+Util.inherits(AlRequest, AlHttp);
+
+AlRequest.prototype.post = function(params, callback) {
console.log("POST " + this.api_key);
+ this._do_post(params, callback);
};
module.exports = AlRequest;

0 comments on commit 8453348

Please sign in to comment.