Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 087ef86d163e48ce6472a5616164b5a9ee421792 @icodeforlove committed Sep 17, 2012
Showing with 177 additions and 0 deletions.
  1. +1 −0 .npmignore
  2. +8 −0 LICENSE
  3. +7 −0 Makefile
  4. +8 −0 Readme.md
  5. +1 −0 index.js
  6. +123 −0 lib/request.js
  7. +29 −0 package.json
1 .npmignore
@@ -0,0 +1 @@
+node_modules
8 LICENSE
@@ -0,0 +1,8 @@
+The MIT License (MIT)
+Copyright (c) <2012> <Chad Scira>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 Makefile
@@ -0,0 +1,7 @@
+#
+# Run all tests
+#
+test:
+ @@node_modules/.bin/vows tests/* --spec
+
+.PHONY: test install
8 Readme.md
@@ -0,0 +1,8 @@
+node-request
+===========
+A simple network request helper
+
+Installation
+==============
+
+ $ npm install git://github.com/icodeforlove/node-request.git
1 index.js
@@ -0,0 +1 @@
+module.exports = exports = require('./lib/request');
123 lib/request.js
@@ -0,0 +1,123 @@
+var http = require('http'),
+ https = require('https'),
+ url = require('url'),
+ qs = require('querystring');
+
+function Request ($config) {
+ this._headers = $config.headers || {};
+ this._timeout = $config.timeout || 4000;
+ this._proxies = $config.proxies;
+ this._retries = $config.retries || 0;
+ this._encoding = $config.encoding || 'utf8';
+ this._requestID = 0;
+ this._didRequestFail = $config.didRequestFail;
+
+ this._makeRequest = this._makeRequest.bind(this);
+}
+
+Request.prototype = {
+ post: function ($url, $options, callback) {
+ if (arguments.length == 2) {
+ callback = arguments[1];
+ $options = {};
+ }
+
+ $options.method = 'POST';
+ this._makeRequest($url, $options, callback);
+ },
+
+ get: function ($url, $options, callback) {
+ if (arguments.length == 2) {
+ callback = arguments[1];
+ $options = {};
+ }
+
+ $options.method = 'GET';
+ this._makeRequest($url, $options, callback);
+ },
+
+ _makeRequest: function ($url, $options, callback, _retry) {
+ var self = this,
+ parts = url.parse($url),
+ method = $options.method,
+ query = $options.data ? qs.stringify($options.data) : null,
+ headers = {},
+ protocol = parts.protocol == 'https:' ? https : http,
+ port = parts.protocol == 'https:' ? 443 : 80,
+ proxy = this._proxies ? this._proxies[this._requestID % this._proxies.length] : null,
+ header;
+
+ _retry = _retry || 0;
+ this._requestID++;
+
+ // default headers
+ for (header in this._headers) headers[header] = this._headers[header];
+
+ // additional headers
+ if ($options.headers) for (header in $options.headers) headers[header] = $options.headers[header];
+
+ // prepare headers for request type
+ if (method == 'POST' && query) {
+ if (!headers['content-type']) headers['content-type'] = 'application/x-www-form-urlencoded';
+ headers['content-length'] = query.length;
+ } else if ($options.data && method == 'GET') {
+ parts.path += '?' + query;
+ }
+
+ function check (response) {
+ if (self._didRequestFail && self._didRequestFail.call(response, response.data)) {
+ retry(response);
+ } else {
+ callback.call(response, response.data);
+ }
+ }
+
+ function retry (response) {
+ if (_retry < self._retries) {
+ _retry++;
+ self._makeRequest($url, $options, callback, _retry);
+ console.log('attempt', _retry);
+ } else {
+ callback.call(response, false);
+ }
+ }
+
+ var request = protocol.request(
+ {
+ host: proxy ? proxy.ip : parts.host,
+ port: proxy ? proxy.port : port,
+ path: proxy ? $url : parts.path,
+ method: method,
+ headers: headers
+ },
+ function (response) {
+ var data = '';
+
+ response.setEncoding($options.encoding || self._encoding);
+
+ response.on('data', function (chunk) {
+ data += chunk;
+ });
+
+ response.on('end', function () {
+ response.data = data;
+ check(response);
+ });
+ }
+ );
+
+ request.setTimeout(this.timeout, function () {
+ console.log('request timed out');
+ });
+
+ request.on('error', function(e) {
+ console.log('problem with request: ' + e.message);
+ });
+
+ if (method == 'POST') request.write(query);
+
+ request.end();
+ }
+};
+
+module.exports = exports = Request;
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "request",
+ "description": "network request helper methods",
+ "version": "0.0.1",
+ "directories": {
+ "lib": "./lib"
+ },
+ "main": "index.js",
+ "author": {
+ "name": "Chad Scira",
+ "email": "chadvscira@gmail.com"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/icodeforlove/node-request.git"
+ },
+ "devDependencies": {
+ "vows": "0.5.x"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/icodeforlove/node-request/raw/master/LICENSE"
+ }
+ ]
+}

0 comments on commit 087ef86

Please sign in to comment.