Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Commit 786ebb2

Browse files
author
Zack Birkenbuel
committed
Merge pull request #3 from HBOCodeLabs/zackb-massive
Pulling in upstream updates to restler
2 parents 1a736d2 + 9a4d5a6 commit 786ebb2

File tree

5 files changed

+340
-214
lines changed

5 files changed

+340
-214
lines changed

README.md

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
Restler
1+
Restler [![NPM Version](https://img.shields.io/npm/v/restler.svg?style=flat)](https://www.npmjs.com/package/restler) ![Node Version](https://img.shields.io/node/v/restler.svg?style=flat) ![Downloads](https://img.shields.io/npm/dm/restler.svg?style=flat)
22
=======
33

44
(C) Dan Webb ([email protected]/@danwrong) 2011, Licensed under the MIT-LICENSE
55

6-
An HTTP client library for node.js (0.6.x and up). Hides most of the complexity of creating and using http.Client.
6+
An HTTP client library for node.js. Hides most of the complexity of creating and using http.Client.
77

8-
**Release 2.x.x** is dedicated to modifying how errors are handled and emitted. Currently errors are being fired as an on 'error' event but as [@ctavan](https://github.com/ctavan) pointed out on [issue #36](https://github.com/danwrong/restler/pull/36) a better approach (and more commonly in vogue now) would be to pass the error obj to the callback.
8+
See [Version History](https://github.com/danwrong/restler/wiki/Version-History) for changes
99

10-
Ths change will inevitably affect those using older < 0.2.x versions of restler. Those not ready to upgrade yet are encouraged to stay on the 0.2.x version.
10+
Installing
11+
----------
1112

12-
See [Version History](https://github.com/danwrong/restler/wiki/Version-History) for changes
13+
```
14+
npm install restler
15+
```
16+
17+
Running the tests
18+
-----------------
19+
20+
```
21+
npm test
22+
```
1323

1424

1525
Features
@@ -18,15 +28,15 @@ Features
1828
* Easy interface for common operations via http.request
1929
* Automatic serialization of post data
2030
* Automatic serialization of query string data
21-
* Automatic deserialization of XML, JSON and YAML responses to JavaScript objects (if you have js-yaml and/or xml2js in the require path)
31+
* Automatic deserialization of XML, JSON and YAML responses to JavaScript objects
2232
* Provide your own deserialization functions for other datatypes
2333
* Automatic following of redirects
2434
* Send files with multipart requests
2535
* Transparently handle SSL (just specify https in the URL)
2636
* Deals with basic auth for you, just provide username and password options
2737
* Simple service wrapper that allows you to easily put together REST API libraries
28-
* Transparently handle content-encoded responses (gzip, deflate) (requires node 0.6+)
29-
* Transparently handle different content charsets via [iconv](https://github.com/bnoordhuis/node-iconv) (if available)
38+
* Transparently handle content-encoded responses (gzip, deflate)
39+
* Transparently handle different content charsets via [iconv-lite](https://github.com/ashtuchkin/iconv-lite)
3040

3141

3242
API
@@ -43,6 +53,7 @@ Basic method to make a request of any type. The function returns a RestRequest o
4353
* `fail: function(data, response)` - emitted when the request was successful, but 4xx status code returned. Gets passed the response data and the response object as arguments.
4454
* `error: function(err, response)` - emitted when some errors have occurred (eg. connection aborted, parse, encoding, decoding failed or some other unhandled errors). Gets passed the `Error` object and the response object (when available) as arguments.
4555
* `abort: function()` - emitted when `request.abort()` is called.
56+
* `timeout: function(ms)` - when a request takes more than the timeout option eg: {timeout:5000}, the request will be aborted. error and abort events will not be called, instead timeout will be emitted.
4657
* `2XX`, `3XX`, `4XX`, `5XX: function(data, response)` - emitted for all requests with response codes in the range (eg. `2XX` emitted for 200, 201, 203).
4758
* <code><i>actual response code</i>: function(data, response)</code> - emitted for every single response code (eg. 404, 201, etc).
4859

@@ -73,6 +84,10 @@ Create a DELETE request.
7384

7485
Create a HEAD request.
7586

87+
### patch(url, options)
88+
89+
Create a PATCH request.
90+
7691
### json(url, data, options)
7792

7893
Send json `data` via GET method.
@@ -81,6 +96,9 @@ Send json `data` via GET method.
8196

8297
Send json `data` via POST method.
8398

99+
### putJson(url, data, options)
100+
101+
Send json `data` via PUT method.
84102

85103
### Parsers
86104

@@ -98,45 +116,53 @@ All of these attempt to turn the response into a JavaScript object. In order to
98116

99117
### Options
100118

101-
* `method` Request method, can be get, post, put, del. Defaults to `"get"`.
119+
* `method` Request method, can be get, post, put, delete. Defaults to `"get"`.
102120
* `query` Query string variables as a javascript object, will override the querystring in the URL. Defaults to empty.
103121
* `data` The data to be added to the body of the request. Can be a string or any object.
104122
Note that if you want your request body to be JSON with the `Content-Type: application/json`, you need to
105123
`JSON.stringify` your object first. Otherwise, it will be sent as `application/x-www-form-urlencoded` and encoded accordingly.
106124
Also you can use `json()` and `postJson()` methods.
107125
* `parser` A function that will be called on the returned data. Use any of predefined `restler.parsers`. See parsers section below. Defaults to `restler.parsers.auto`.
108126
* `encoding` The encoding of the request body. Defaults to `"utf8"`.
109-
* `decoding` The encoding of the response body. For a list of supported values see [Buffers](http://nodejs.org/docs/latest/api/buffers.html#buffers). Additionally accepts `"buffer"` - returns response as `Buffer`. Defaults to `"utf8"`.
127+
* `decoding` The encoding of the response body. For a list of supported values see [Buffers](http://nodejs.org/api/buffer.html#buffer_buffer). Additionally accepts `"buffer"` - returns response as `Buffer`. Defaults to `"utf8"`.
110128
* `headers` A hash of HTTP headers to be sent. Defaults to `{ 'Accept': '*/*', 'User-Agent': 'Restler for node.js' }`.
111129
* `username` Basic auth username. Defaults to empty.
112130
* `password` Basic auth password. Defaults to empty.
131+
* `accessToken` OAuth Bearer Token. Defaults to empty.
113132
* `multipart` If set the data passed will be formated as `multipart/form-encoded`. See multipart example below. Defaults to `false`.
114133
* `client` A http.Client instance if you want to reuse or implement some kind of connection pooling. Defaults to empty.
115134
* `followRedirects` If set will recursively follow redirects. Defaults to `true`.
135+
* `timeout` If set, will emit the timeout event when the response does not return within the said value (in ms)
136+
* `rejectUnauthorized` If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.
116137

117138

118139
Example usage
119140
-------------
120141

121142
```javascript
122-
var sys = require('util'),
123-
rest = require('./restler');
143+
var rest = require('./restler');
124144

125145
rest.get('http://google.com').on('complete', function(result) {
126146
if (result instanceof Error) {
127-
sys.puts('Error: ' + result.message);
147+
console.log('Error:', result.message);
128148
this.retry(5000); // try again after 5 sec
129149
} else {
130-
sys.puts(result);
150+
console.log(result);
131151
}
132152
});
133153

134154
rest.get('http://twaud.io/api/v1/users/danwrong.json').on('complete', function(data) {
135-
sys.puts(data[0].message); // auto convert to object
155+
console.log(data[0].message); // auto convert to object
136156
});
137157

138158
rest.get('http://twaud.io/api/v1/users/danwrong.xml').on('complete', function(data) {
139-
sys.puts(data[0].sounds[0].sound[0].message); // auto convert to object
159+
console.log(data[0].sounds[0].sound[0].message); // auto convert to object
160+
});
161+
162+
rest.get('http://someslowdomain.com',{timeout: 10000}).on('timeout', function(ms){
163+
console.log('did not return within '+ms+' ms');
164+
}).on('complete',function(data,response){
165+
console.log('did not time out');
140166
});
141167

142168
rest.post('http://user:[email protected]/action', {
@@ -157,7 +183,7 @@ rest.post('https://twaud.io/api/v1/upload.json', {
157183
'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, 321567, null, 'audio/mpeg')
158184
}
159185
}).on('complete', function(data) {
160-
sys.puts(data.audio_url);
186+
console.log(data.audio_url);
161187
});
162188

163189
// create a service constructor for very easy API wrappers a la HTTParty...
@@ -174,7 +200,7 @@ Twitter = rest.service(function(u, p) {
174200

175201
var client = new Twitter('danwrong', 'password');
176202
client.update('Tweeting using a Restler service thingy').on('complete', function(data) {
177-
sys.p(data);
203+
console.log(data);
178204
});
179205

180206
// post JSON
@@ -183,26 +209,12 @@ rest.postJson('http://example.com/action', jsonData).on('complete', function(dat
183209
// handle response
184210
});
185211

186-
```
187-
188-
Running the tests
189-
-----------------
190-
install **[nodeunit](https://github.com/caolan/nodeunit)**
191-
192-
```bash
193-
npm install nodeunit
194-
```
195-
196-
then
197-
198-
```bash
199-
node test/all.js
200-
```
201-
202-
or
212+
// put JSON
213+
var jsonData = { id: 334 };
214+
rest.putJson('http://example.com/action', jsonData).on('complete', function(data, response) {
215+
// handle response
216+
});
203217

204-
```bash
205-
nodeunit test/restler.js
206218
```
207219

208220
TODO

lib/multipartform.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ Part.prototype = {
6767
if (this.value.data) {
6868
header = "Content-Disposition: form-data; name=\"" + this.name +
6969
"\"; filename=\"" + this.value.filename + "\"\r\n" +
70+
"Content-Length: " + this.value.data.length + "\r\n" +
7071
"Content-Type: " + this.value.contentType;
71-
} if (this.value instanceof File) {
72+
} else if (this.value instanceof File) {
7273
header = "Content-Disposition: form-data; name=\"" + this.name +
7374
"\"; filename=\"" + this.value.filename + "\"\r\n" +
7475
"Content-Length: " + this.value.fileSize + "\r\n" +
@@ -127,6 +128,10 @@ Part.prototype = {
127128
});
128129
})(); // reader()
129130
});
131+
} else if (this.value instanceof Data) {
132+
stream.write(this.value.data);
133+
stream.write("\r\n");
134+
callback();
130135
} else {
131136
stream.write(this.value + "\r\n");
132137
callback();

0 commit comments

Comments
 (0)