Skip to content

Commit

Permalink
Some fixes and docs rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cavage committed Jun 13, 2011
1 parent 81aa232 commit d872273
Show file tree
Hide file tree
Showing 25 changed files with 992 additions and 75 deletions.
3 changes: 3 additions & 0 deletions docs/index.txt
Expand Up @@ -4,3 +4,6 @@ restify-log(7) restify-log.7.ronn
restify-request(7) restify-request.7.ronn
restify-response(7) restify-response.7.ronn
restify-routes(7) restify-routes.7.ronn
restify-versions(7) restify-versions.7.ronn
restify-throttle(7) restify-throttle.7.ronn
restify-client(7) restify-client.7.ronn
178 changes: 178 additions & 0 deletions docs/restify-client.7
@@ -0,0 +1,178 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "RESTIFY\-CLIENT" "7" "June 2011" "" ""
.
.SH "NAME"
\fBrestify\-client\fR \- The restify client
.
.SH "SYNOPSIS"
.
.nf

var restify = require(\'restify\');

var client = restify\.createClient({
url: \'http://localhost:8080\',
version: \'1\.2\.3\',
retryOptions: {
retries: 2,
minTimeout: 250
}
});

client\.get(\'/users\', function(err, body, headers) {
if (err)
return console\.log(err);

console\.log(Body: \' + body);
console\.log(Headers: \' + JSON\.stringify(headers, null, 2));
});

var req = {
path: \'/mark/update\',
body: {
foo: \'bar\'
},
query: {
action: \'addAttribute\'
},
expect: [200, 201, 202, 204]
};
client\.post(req, function(err, body, headers) {
if (err)
console\.log(err);
});
client\.head(\'/mark\', function(err, headers) {
if (err)
return console\.log(err);

console\.log(Headers: \' + JSON\.stringify(headers, null, 2));
});
.
.fi
.
.SH "DESCRIPTION"
The restify client is the complement to the server; that is it tries to offload some of the difficulty of dealing with REST APIs and node\.js\. Like the server routing, you define \'get/head/put/post/del\' methods with a URL path, and wait for your callback to get invoked\. Advanced usage involves sending a fully parameretized request object to these methods\. More on that below\.
.
.P
One of the big value adds is that it bakes in exponential backoff and retries; If you\'re not familiar with how that works or why you nee it, read this: http://dthain\.blogspot\.com/2009/02/exponential\-backoff\-in\-distributed\.html \. Basically, if you\'re making requests to another system, there\'s almost never a case when you don\'t want this\. The restify client uses node\-retry under the hood, and so the options you pass in retryOptions go straight through to that module\.
.
.SH "CREATING A CLIENT"
To create a client, invoke \fBrestify\.createClient\fR\. This method takes an optional \fBoptions\fR Object, of the following syntax:
.
.IP "" 4
.
.nf

{
version: \'>=1\.2\.3\', // x\-api\-version to send
url: \'http://google\.com\', // What to talk to
path: \'/v1\', // path to prefix on all requests
headers: {
\'x\-foo\-bar\': \'bah\' // Any Additional headers
},
contentType: \'application/x\-www\-form\-urlencoded\', // How to send data
retryOptions: {
retries: 5 // node\-retry options
}
}
.
.fi
.
.IP "" 0
.
.SH "ISSUING REQUESTS"
Like the server API, you issue requests with restify by invoking a method of the same name as the HTTP verb you want to invoke\. There are two ways to invoke these APIs, either with a string path to hit, in which case restify makes some (hopefully) sane guesses about what the endpoint will return, or with a fully parameterized request object, in which case restify doesn\'t make any guesses\.
.
.P
If you\'re issuing a fully parameterized request, the object syntax is:
.
.IP "" 4
.
.nf

{
path: \'/foo\',
body: {
key: \'value\'
},
query: {
limit: 10,
offset: 11
},
expect: [100, 200]
}
.
.fi
.
.IP "" 0
.
.P
Where \fBpath\fR is the resource to hit (remember this is suffixed to the path you passed into the \fBcreateClient\fR call), \fBbody\fR is any data to send in the request, serialized with \fBcontentType\fR\. \fBquery\fR is a set of params to tack on as a query string, and \fBexpect\fR is either a single code or an array of codes to check for on response from the server\.
.
.P
Most of these should be obvious, but expect is noteworthy in that if the server didn\'t return a status code matching that code, your callback will get an error\.
.
.SH "APIs"
.
.nf

client\.get(request, function(err, body, headers, res) {});
client\.head(request, function(err, headers, res) {});
client\.post(request, function(err, body, headers, res) {});
client\.put(request, function(err, body, headers, res) {});
client\.del(request, function(err, headers, res) {});
.
.fi
.
.IP "\(bu" 4
get expects a 200 by default\.
.
.IP "\(bu" 4
head expects [200, 204] by default\.
.
.IP "\(bu" 4
post expects [200, 201] by default\.
.
.IP "\(bu" 4
put expects [200, 201] by default\.
.
.IP "\(bu" 4
del expects [200, 202, 204] by default\.
.
.IP "" 0
.
.SH "HEADERS"
The restify client automatically fills in the following headers:
.
.IP "\(bu" 4
Date
.
.IP "\(bu" 4
Accept (always application/json)
.
.IP "" 0
.
.P
If there is a body, then additionally:
.
.IP "\(bu" 4
Content\-Type
.
.IP "\(bu" 4
Content\-Length
.
.IP "\(bu" 4
Content\-MD5
.
.IP "" 0
.
.SH "COPYRIGHT/LICENSE"
Copyright 2011 Mark Cavage \fImcavage@gmail\.com\fR
.
.P
This software is licensed under the MIT License\.
.
.SH "SEE ALSO"
restify(3), restify\-versions(7)
147 changes: 147 additions & 0 deletions docs/restify-client.md
@@ -0,0 +1,147 @@
restify-client(7) -- The restify client
=======================================

## SYNOPSIS

var restify = require('restify');

var client = restify.createClient({
url: 'http://localhost:8080',
version: '1.2.3',
retryOptions: {
retries: 2,
minTimeout: 250
}
});

client.get('/users', function(err, body, headers) {
if (err)
return console.log(err);

console.log(Body: ' + body);
console.log(Headers: ' + JSON.stringify(headers, null, 2));
});

var req = {
path: '/mark/update',
body: {
foo: 'bar'
},
query: {
action: 'addAttribute'
},
expect: [200, 201, 202, 204]
};
client.post(req, function(err, body, headers) {
if (err)
console.log(err);
});
client.head('/mark', function(err, headers) {
if (err)
return console.log(err);

console.log(Headers: ' + JSON.stringify(headers, null, 2));
});


## DESCRIPTION

The restify client is the complement to the server; that is it tries to offload
some of the difficulty of dealing with REST APIs and node.js. Like the server
routing, you define 'get/head/put/post/del' methods with a URL path, and wait
for your callback to get invoked. Advanced usage involves sending a fully
parameretized request object to these methods. More on that below.

One of the big value adds is that it bakes in exponential backoff and retries;
If you're not familiar with how that works or why you nee it, read this:
http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html .
Basically, if you're making requests to another system, there's almost never a
case when you don't want this. The restify client uses node-retry under the
hood, and so the options you pass in retryOptions go straight through to that
module.

## CREATING A CLIENT

To create a client, invoke `restify.createClient`. This method takes an
optional `options` Object, of the following syntax:

{
version: '>=1.2.3', // x-api-version to send
url: 'http://google.com', // What to talk to
path: '/v1', // path to prefix on all requests
headers: {
'x-foo-bar': 'bah' // Any Additional headers
},
contentType: 'application/x-www-form-urlencoded', // How to send data
retryOptions: {
retries: 5 // node-retry options
}
}

## ISSUING REQUESTS

Like the server API, you issue requests with restify by invoking a method of the
same name as the HTTP verb you want to invoke. There are two ways to invoke
these APIs, either with a string path to hit, in which case restify makes some
(hopefully) sane guesses about what the endpoint will return, or with a fully
parameterized request object, in which case restify doesn't make any guesses.

If you're issuing a fully parameterized request, the object syntax is:

{
path: '/foo',
body: {
key: 'value'
},
query: {
limit: 10,
offset: 11
},
expect: [100, 200]
}

Where `path` is the resource to hit (remember this is suffixed to the path you
passed into the `createClient` call), `body` is any data to send in the request,
serialized with `contentType`. `query` is a set of params to tack on as a query
string, and `expect` is either a single code or an array of codes to check for
on response from the server.

Most of these should be obvious, but expect is noteworthy in that if the server
didn't return a status code matching that code, your callback will get an error.

## APIs

client.get(request, function(err, body, headers, res) {});
client.head(request, function(err, headers, res) {});
client.post(request, function(err, body, headers, res) {});
client.put(request, function(err, body, headers, res) {});
client.del(request, function(err, headers, res) {});

* get expects a 200 by default.
* head expects [200, 204] by default.
* post expects [200, 201] by default.
* put expects [200, 201] by default.
* del expects [200, 202, 204] by default.

## HEADERS

The restify client automatically fills in the following headers:

* Date
* Accept (always application/json)

If there is a body, then additionally:

* Content-Type
* Content-Length
* Content-MD5

## COPYRIGHT/LICENSE

Copyright 2011 Mark Cavage <mcavage@gmail.com>

This software is licensed under the MIT License.

## SEE ALSO

restify(3), restify-versions(7)
29 changes: 25 additions & 4 deletions docs/restify-log.7
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "RESTIFY\-LOG" "7" "May 2011" "" ""
.TH "RESTIFY\-LOG" "7" "June 2011" "" ""
.
.SH "NAME"
\fBrestify\-log\fR \- The restify Logger
Expand All @@ -15,9 +15,8 @@ var log = restify\.log;

log\.level(restify\.LogLevel\.Debug);

if (log\.debug()) {
if (log\.debug())
log\.debug(\'Here\'s an object: %o\', { foo: \'bar\' });
}
.
.fi
.
Expand Down Expand Up @@ -96,7 +95,7 @@ These methods take an \fBsprintf\fR style message, where the following format qu
%s String
.
.IP "\(bu" 4
%o Object (uses node\.js util\.inspect())
%o Object
.
.IP "" 0
.
Expand All @@ -122,6 +121,28 @@ The restify framework uses Fatal/Error/Warn/Info/Trace\. To get verbose internal
.P
Finally, set the level with \fBlog\.level\fR\. The level params are on the: \fBrestify\.LogLevel\fR object\.
.
.SH "W3C LOGGING"
In addition to the "human" output functions above, the log also ships with a W3C compliant interceptor that you can use anywhere after \fBresponse\.send\fR has been invoked\. W3C log messages are basically this:
.
.IP "" 4
.
.nf

127\.0\.0\.1 \- admin [12/05/2011:18:31:49 GMT] "GET /foo/bar HTTP/1\.1" 200 79 58
.
.fi
.
.IP "" 0
.
.P
Where you have (in order) IP, username, timestamp, the HTTP request\-line, response code, bytes sent, and the time (ms) it took to respond\. The response time is not technically part of the W3C specification, but a lot of HTTP servers do it, and it\'s useful, so restify does it\. The user above comes from \fBreq\.username\fR, which automatically gets set if you\'re using HTTP Basic Authentication\. If your clients are not authenticating with that, you will have to set it in somewhere before the logger gets invoked\. Otherwise you\'ll just get a \'\-\' character in it\'s place (which is what the spec says to do)\.
.
.P
To use this logger, just add \fBlog\.w3c\fR to your request chain (somewhere after a handler that will have called \fBresponse\.send\fR\.
.
.SH "OUTPUT REDIRECTION"
By default, \fBlog\.w3c\fR writes to stdout, and the debug/info/\.\.\. functions write to \fBstderr\fR\. To redirect these, pass a node\.js \fBWriteableStream\fR into the functions \fBlog\.stdout\fR and \fBlog\.stderr\fR respectively\. For example, you could write a \fBWriteableStream\fR that is actually a rolling file appender\. That\'d be cool\.\.\.
.
.SH "COPYRIGHT/LICENSE"
Copyright 2011 Mark Cavage \fImcavage@gmail\.com\fR
.
Expand Down

0 comments on commit d872273

Please sign in to comment.