Skip to content
Browse files

Added priority post and get calls. Added AlResponse class.

  • Loading branch information...
1 parent 8453348 commit 6b6e866071de9a7903ea6b9a4791692240715562 Chavez committed Apr 6, 2012
Showing with 145 additions and 23 deletions.
  1. +54 −7 README.md
  2. +66 −14 lib/AlHttp.js
  3. +12 −1 lib/AlRequest.js
  4. +12 −0 lib/AlResponse.js
  5. +1 −1 package.json
View
61 README.md
@@ -6,13 +6,60 @@ node // for console
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));
+ req.post({'keyword' : 'Centaur Carnage'}, function(response) {
- if (result)
- return result;
+ console.log('Code:');
+ console.log(response.statusCode);
+ console.log('URL:');
+ console.log(response.url);
+ console.log('Params:');
+ console.log(response.params);
+
+ if (response.success) {
+ console.log('Body:');
+ console.log(response.body);
+ }
+ else {
+ console.log('Error:');
+ console.log(response.errorMessage);
+ }
+ });
+
+ req.priority_post({'keyword' : 'Centaur Soirée'}, function(response) {
+
+ console.log('Code:');
+ console.log(response.statusCode);
+ console.log('URL:');
+ console.log(response.url);
+ console.log('Params:');
+ console.log(response.params);
+
+ if (response.success) {
+ console.log('Body:');
+ console.log(response.body);
+ }
+ else {
+ console.log('Error:');
+ console.log(response.errorMessage);
+ }
+ });
+
+ req.get({'keyword' : 'Centaur Carnage'}, function(response) {
+
+ console.log('Code:');
+ console.log(response.statusCode);
+ console.log('URL:');
+ console.log(response.url);
+ console.log('Params:');
+ console.log(response.params);
+
+ if (response.success) {
+ console.log('Body:');
+ console.log(response.body);
+ }
+ else {
+ console.log('Error:');
+ console.log(response.errorMessage);
+ }
});
- console.log('finished');
- console.log(JSON.parse(result.data));
View
80 lib/AlHttp.js
@@ -3,6 +3,7 @@ var Events = require('events');
var request = require('request');
var QueryString = require('querystring');
var Util = require('util');
+var AlResponse = require('./AlResponse');
var AlHttp = function() {
Events.EventEmitter.call(this);
@@ -17,16 +18,43 @@ Util.inherits(AlHttp, Events.EventEmitter);
AlHttp.prototype._do_post = function(params, callback) {
params['auth_token'] = this.api_key;
+ var priority = params['priority'];
+ var relativePath;
+ if (priority === undefined) {
+ relativePath = '/keywords.json';
+ }
+ else {
+ delete params['priority'];
+ relativePath = '/keywords/priority.json';
+ }
+
var queryParams = QueryString.stringify(params);
- var apiUrl = this.baseUrl + '/keywords.json'
+ var apiUrl = this.baseUrl + relativePath;
if (queryParams != undefined && queryParams.length > 0)
apiUrl += '?' + queryParams;
var requestOptions = { method: 'POST', url: apiUrl };
- var httpRequest = request.post(requestOptions);
+ var alResponse = new AlResponse(params, apiUrl);
+ var httpRequest = request.post(requestOptions);
+
+ this._connections[httpRequest.hash] = { callback: callback, data: '', httpRequest: httpRequest, alResponse: alResponse };
+ this._createEventListenersForRequest(httpRequest);
+}
+
+AlHttp.prototype._do_get = function(params, callback) {
+ params['auth_token'] = this.api_key;
+ var queryParams = QueryString.stringify(params);
+ var apiUrl = this.baseUrl + '/keywords/get.json';
+
+ if (queryParams != undefined && queryParams.length > 0)
+ apiUrl += '?' + queryParams;
+
+ var requestOptions = { method: 'GET', url: apiUrl };
+ var alResponse = new AlResponse(params, apiUrl);
+ var httpRequest = request.get(requestOptions);
- this._connections[httpRequest.hash] = { callback: callback, data: '', httpRequest: httpRequest };
+ this._connections[httpRequest.hash] = { callback: callback, data: '', httpRequest: httpRequest, alResponse: alResponse };
this._createEventListenersForRequest(httpRequest);
}
@@ -104,17 +132,22 @@ AlHttp.prototype._requestDidClose = function(aRequest) {
var connection = this._connectionForKey(aRequest.hash);
if (connection !== undefined) {
- var error = undefined;
- var result = null;
+ var error = undefined;
+ var result = null;
+ var alResponse = connection.alResponse;
try {
- result = JSON.parse(connection.data);
+ result = JSON.parse(connection.data);
+ alResponse.body = result;
+ alResponse.success = true;
}
catch (e) {
error = e;
+ alResponse.success = false;
+ alResponse.errorMessage = e.message;
}
-
- connection.callback(error, result);
+ console.log('_requestDidClose');
+ connection.callback(alResponse);
this._removeConnectionForKey(aRequest.hash);
}
};
@@ -130,17 +163,23 @@ AlHttp.prototype._requestDidEnd = function(aRequest) {
var connection = this._connectionForKey(aRequest.hash);
if (connection != undefined) {
- var error = undefined;
- var result = null;
+ var error = undefined;
+ var result = null;
+ var alResponse = connection.alResponse;
try {
result = JSON.parse(connection.data);
+ alResponse.body = result;
+ alResponse.success = true;
}
catch (e) {
error = e;
+ alResponse.success = false;
+ alResponse.errorMessage = e.message;
}
- console.log(connection);
- connection.callback(error, result);
+
+ console.log('_requestDidEnd');
+ connection.callback(alResponse);
this._removeConnectionForKey(aRequest.hash);
}
};
@@ -157,7 +196,12 @@ AlHttp.prototype._requestDidFailWithError = function(aRequest, aError) {
var connection = this._connectionForKey(aRequest.hash);
if (connection !== undefined) {
- connection.callback(aError, undefined);
+ var alResponse = connection.alResponse;
+ alResponse.success = false;
+ alResponse.errorMessage = aError.message;
+
+ console.log('_requestDidFailWithError');
+ connection.callback(alResponse);
this._removeConnectionForKey(aRequest.hash);
}
};
@@ -190,15 +234,23 @@ AlHttp.prototype._requestDidReceiveData = function(aRequest, aData) {
AlHttp.prototype._requestDidReceiveResponse = function(aRequest, aResponse) {
var connection = this._connectionForKey(aRequest.hash);
- if (connection !== undefined)
+ if (connection !== undefined) {
connection['data'] = '';
+ console.log('connection in did receive response')
+ var alResponse = connection.alResponse;
+ alResponse.statusCode = aResponse.statusCode;
+ connection.alResponse = alResponse;
+ }
var error = null;
if (aResponse.statusCode != 200) {
error = new Error();
error.code = aResponse.statusCode;
+ console.log(aResponse);
}
+ console.log('_requestDidReceiveResponse');
+
switch(aResponse.statusCode) {
case 304:
error.message = 'Not Modified.';
View
13 lib/AlRequest.js
@@ -15,8 +15,19 @@ var AlRequest = function() {
Util.inherits(AlRequest, AlHttp);
AlRequest.prototype.post = function(params, callback) {
- console.log("POST " + this.api_key);
+ if (params === undefined) { params = {}; }
this._do_post(params, callback);
};
+AlRequest.prototype.priority_post = function(params, callback) {
+ if (params === undefined) { params = {}; }
+ params['priority'] = true;
+ this._do_post(params, callback);
+};
+
+AlRequest.prototype.get = function(params, callback) {
+ if (params === undefined) { params = {}; }
+ this._do_get(params, callback);
+};
+
module.exports = AlRequest;
View
12 lib/AlResponse.js
@@ -0,0 +1,12 @@
+var Util = require('util');
+
+var AlResponse = function(params, url) {
+ this.params = params;
+ this.url = url;
+ this.body = {}
+ this.success = false;
+ this.errorMessage = null;
+ this.statusCode = null;
+}
+
+module.exports = AlResponse;
View
2 package.json
@@ -1,5 +1,5 @@
{
- "author": "Chavez <me@example.com>",
+ "author": "Chavez",
"name": "al-papi",
"description": "AuthorityLabs Partner API wrapper.",
"version": "0.0.1",

0 comments on commit 6b6e866

Please sign in to comment.
Something went wrong with that request. Please try again.