Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement content decoding, add more documentation

  • Loading branch information...
commit 05cfec5b206fd910d4c5d6511ec591b1a65aaa72 1 parent 80b444b
@pfleidi authored
Showing with 102 additions and 28 deletions.
  1. +102 −28 lib/wwwdude/index.js
View
130 lib/wwwdude/index.js
@@ -23,6 +23,7 @@ var defaultHeaders = {
};
exports.version = '0.0.7';
+exports.parsers = require('./parsers');
/**
* Factory method to create a client
@@ -47,7 +48,7 @@ var createClient = exports.createClient = function createClient(clientOptions) {
* @param {String} url
* @param {Object} options
*
- * @return {Object}
+ * @return {EventEmitter}
* @api private
*/
function makeRequest(method, url, options) {
@@ -95,11 +96,9 @@ var createClient = exports.createClient = function createClient(clientOptions) {
* @return {undefined}
* @api private
*/
- function _handleResponse(response) {
+ function _dispatchResponse(response) {
var code = response.statusCode;
- var parser = clientOptions.contentParser;
- // TODO: proper error-handling on parse errors
- var data = parser ? parser(response.rawData) : response.rawData;
+ var data = response.data || response.rawData;
_respond(code.toString().replace(/\d{2}$/, 'XX'), data, response);
_respond(code.toString(), data, response);
@@ -126,6 +125,43 @@ var createClient = exports.createClient = function createClient(clientOptions) {
_respond('complete', data, response);
}
+
+ /**
+ * Parse retrieved content and delegate to _dispatchResponse
+ *
+ * @param {Object} response
+ * @return {undefined}
+ * @api private
+ */
+ function _parseContent(response) {
+ var parser = clientOptions.contentParser;
+
+ parser(response.rawData, function (err, parsed) {
+ if (err) {
+ _respond('error', err);
+ } else {
+ response.data = parsed;
+ _dispatchResponse(response);
+ }
+ });
+ }
+
+ /**
+ * Handle incoming response.
+ * Delegate if parser is set.
+ *
+ * @param {Object} response
+ * @return {undefined}
+ * @api private
+ */
+ function _handleResponse(response) {
+ if (clientOptions.contentParser) {
+ _parseContent(response);
+ } else {
+ _dispatchResponse(response);
+ }
+ }
+
/**
* Decode gzip content with an external gunzip command
* call request._handleResponse() when ready
@@ -200,39 +236,77 @@ var createClient = exports.createClient = function createClient(clientOptions) {
/* return the actual API */
return {
- get: function get(url, requestHeaders) {
- var headers = Util.mergeHeaders(clientHeaders, requestHeaders);
- return makeRequest('GET', url, { headers: headers });
+ /**
+ * HTTP GET request
+ *
+ * @param {String} url
+ * @param {Object} opts
+ * @return {EventEmitter}
+ * @api public
+ */
+ get: function get(url, opts) {
+ opts = opts || {};
+ opts.headers = Util.mergeHeaders(clientHeaders, opts.headers);
+ return makeRequest('GET', url, opts);
},
- post: function post(url, payload, requestHeaders) {
- var headers = Util.mergeHeaders(clientHeaders, requestHeaders);
- return makeRequest('POST', url, { headers: headers, payload: payload });
+ /**
+ * HTTP PUT request
+ *
+ * @param {String} url
+ * @param {Object} opts
+ * @return {EventEmitter}
+ * @api public
+ */
+ put: function put(url, opts) {
+ opts = opts || {};
+ opts.headers = Util.mergeHeaders(clientHeaders, opts.headers);
+ return makeRequest('PUT', url, opts);
},
- put: function put(url, payload, requestHeaders) {
- var headers = Util.mergeHeaders(clientHeaders, requestHeaders);
- return makeRequest('PUT', url, { headers: headers, payload: payload });
+ /**
+ * HTTP POST request
+ *
+ * @param {String} url
+ * @param {Object} opts
+ * @return {EventEmitter}
+ * @api public
+ */
+ post: function post(url, opts) {
+ opts = opts || {};
+ opts.headers = Util.mergeHeaders(clientHeaders, opts.headers);
+ return makeRequest('POST', url, opts);
},
- del: function del(url, requestHeaders) {
- var headers = Util.mergeHeaders(clientHeaders, requestHeaders);
- return makeRequest('DELETE', url, { headers: headers });
+ /**
+ * HTTP DELETE request
+ *
+ * @param {String} url
+ * @param {Object} opts
+ * @return {EventEmitter}
+ * @api public
+ */
+ del: function del(url, opts) {
+ opts = opts || {};
+ opts.headers = Util.mergeHeaders(clientHeaders, opts.headers);
+ return makeRequest('DELETE', url, opts);
},
- head: function head(url, requestHeaders) {
- var headers = Util.mergeHeaders(clientHeaders, requestHeaders);
- return makeRequest('HEAD', url, { headers: headers });
+ /**
+ * HTTP DELETE request
+ *
+ * @param {String} url
+ * @param {Object} opts
+ * @return {Object} client
+ * @return {EventEmitter}
+ * @api public
+ */
+ head: function head(url, opts) {
+ opts = opts || {};
+ opts.headers = Util.mergeHeaders(clientHeaders, opts.headers);
+ return makeRequest('HEAD', url, opts);
}
};
};
-
-/*
- * add support for require('wwwdude')(option)
- * additionally to
- * require('wwwdude').createClient(options)
- */
-createClient.createClient = createClient;
-module.exports = createClient;
Please sign in to comment.
Something went wrong with that request. Please try again.