From aa36ce94553ec4f17b68af38f495991f946d401c Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 24 Jan 2015 08:11:36 +0100 Subject: [PATCH] v1.0.2: Documentation fixes, and making the nodeback API work correctly for shorthand methods --- README.md | 28 +++++++++++++++++++++++++++- lib/bhttp.coffee | 16 ++++++++-------- lib/bhttp.js | 16 ++++++++-------- package.json | 2 +- testcase1.js | 13 +++++++++++++ 5 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 testcase1.js diff --git a/README.md b/README.md index d5f9a9e..8f4f519 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,32 @@ Be aware that by making a pull request, you agree to release your modifications ## Usage +A simple example: + +```javascript +var bhttp = require("bhttp"); + +Promise.try(function() { + return bhttp.get("http://icanhazip.com/"); +}).then(function(response) { + console.log("Your IP is:", response.body.toString()); +}); +``` + +... or, using nodebacks: + +```javascript +var bhttp = require("bhttp"); + +bhttp.get("http://icanhazip.com/", {}, function(err, response) { + console.log("Your IP is:", response.body.toString()); +}); +``` + +### Streaming + +Demonstrating both streaming responses and using a stream in form data for a request: + ```javascript var bhttp = require("bhttp"); @@ -149,7 +175,7 @@ Makes a request, and returns the response object asynchronously. The response ob A few extra properties are set on the response object (which is a `http.IncomingMessage`): -* __body__: When `stream` is set to `false` (the default), this will contain the response body. This can be either a string or, in the case of a JSON response, a decoded JSON object. +* __body__: When `stream` is set to `false` (the default), this will contain the response body. This can be either a Buffer or, in the case of a JSON response, a decoded JSON object. * __redirectHistory__: An array containing the redirect responses, if any, in chronological order. Response bodies are discarded by default; if you do not want this, use the `keepRedirectResponses` option. * __request__: The request configuration that was generated by `bhttp`. You probably don't need this. * __requestState__: The request state that was accumulated by `bhttp`. You probably don't need this. diff --git a/lib/bhttp.coffee b/lib/bhttp.coffee index e40bc2f..fef232a 100644 --- a/lib/bhttp.coffee +++ b/lib/bhttp.coffee @@ -414,7 +414,7 @@ processResponse = (request, response, requestState) -> # Some wrappers -doPayloadRequest = (url, data, options) -> +doPayloadRequest = (url, data, options, callback) -> # A wrapper that processes the second argument to .post, .put, .patch shorthand API methods. # FIXME: Treat a {} for data as a null? Otherwise {} combined with inputBuffer/inputStream will error. if isStream(data) @@ -424,7 +424,7 @@ doPayloadRequest = (url, data, options) -> else options.formFields = data - @request url, options + @request url, options, callback redirectGet = (request, response, requestState) -> debug "following forced-GET redirect to %s", response.headers["location"] @@ -469,22 +469,22 @@ createCookieJar = (jar) -> bhttpAPI = head: (url, options = {}, callback) -> options.method = "head" - @request url, options + @request url, options, callback get: (url, options = {}, callback) -> options.method = "get" - @request url, options + @request url, options, callback post: (url, data, options = {}, callback) -> options.method = "post" - doPayloadRequest.bind(this) url, data, options + doPayloadRequest.bind(this) url, data, options, callback put: (url, data, options = {}, callback) -> options.method = "put" - doPayloadRequest.bind(this) url, data, options + doPayloadRequest.bind(this) url, data, options, callback patch: (url, data, options = {}, callback) -> options.method = "patch" - doPayloadRequest.bind(this) url, data, options + doPayloadRequest.bind(this) url, data, options, callback delete: (url, data, options = {}, callback) -> options.method = "delete" - @request url, options + @request url, options, callback request: (url, options = {}, callback) -> @_doRequest(url, options).nodeify(callback) _doRequest: (url, options, requestState) -> diff --git a/lib/bhttp.js b/lib/bhttp.js index 282ef88..639cfb7 100644 --- a/lib/bhttp.js +++ b/lib/bhttp.js @@ -452,7 +452,7 @@ processResponse = function(request, response, requestState) { }); }; -doPayloadRequest = function(url, data, options) { +doPayloadRequest = function(url, data, options, callback) { if (isStream(data)) { options.inputStream = data; } else if (ofTypes(data, [Buffer]) || typeof data === "string") { @@ -460,7 +460,7 @@ doPayloadRequest = function(url, data, options) { } else { options.formFields = data; } - return this.request(url, options); + return this.request(url, options, callback); }; redirectGet = function(request, response, requestState) { @@ -535,42 +535,42 @@ bhttpAPI = { options = {}; } options.method = "head"; - return this.request(url, options); + return this.request(url, options, callback); }, get: function(url, options, callback) { if (options == null) { options = {}; } options.method = "get"; - return this.request(url, options); + return this.request(url, options, callback); }, post: function(url, data, options, callback) { if (options == null) { options = {}; } options.method = "post"; - return doPayloadRequest.bind(this)(url, data, options); + return doPayloadRequest.bind(this)(url, data, options, callback); }, put: function(url, data, options, callback) { if (options == null) { options = {}; } options.method = "put"; - return doPayloadRequest.bind(this)(url, data, options); + return doPayloadRequest.bind(this)(url, data, options, callback); }, patch: function(url, data, options, callback) { if (options == null) { options = {}; } options.method = "patch"; - return doPayloadRequest.bind(this)(url, data, options); + return doPayloadRequest.bind(this)(url, data, options, callback); }, "delete": function(url, data, options, callback) { if (options == null) { options = {}; } options.method = "delete"; - return this.request(url, options); + return this.request(url, options, callback); }, request: function(url, options, callback) { if (options == null) { diff --git a/package.json b/package.json index b0a34aa..efe70fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bhttp", - "version": "1.0.1", + "version": "1.0.2", "description": "A sane HTTP client library for Node.js with Streams2 support.", "main": "index.js", "scripts": { diff --git a/testcase1.js b/testcase1.js new file mode 100644 index 0000000..b3c8060 --- /dev/null +++ b/testcase1.js @@ -0,0 +1,13 @@ +var bhttp = require("./"); +var stockName = "AAPL"; +var debug = require("debug")("testcase1"); +var util = require("util"); + +bhttp.get('http://finance.yahoo.com/q/ks?s=' + stockName + '+Key+Statistics', { stream: false }, function(err, res) { + if(err) { + debug('ERROR: ' + err) + return callback(err) + } else { + debug('res: ' + util.inspect(res)) + } +})