Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Creating an API client using Rem

tcr edited this page Mar 15, 2013 · 1 revision

Rem creates API objects built around a JSON schema. It's easy to configure Rem to work with your own API by using a constructor.

The simplest HTTP client is the Rem object itself, which has built-in methods to make HTTP calls:

rem.json('http://archive.org/', {output: 'json'}).get(function (err, json) { ... });

This requests http://archive.org/?output=json and returns the JSON body.

Creating a Client

You can create an independent client with createClient:

var api = rem.createClient();
var api = rem.createClient('http://api.example.com/v1/'); // Set a base URL
var api = rem.createClient({base: 'http://api.example.com/v1/'}); // equivalent

The first argument can be a string or an options object. The "base" parameter prepends the URL fragment to subsequent requests made with the API. For example:

var api = rem.createClient('http://api.example.com/v1/');
api.json('posts/latest').get(function (err, json) { ... }); // http://api.example.com/v1/posts/latest

You can set a default request type for your API to return with the "format" parameter.

var api = rem.createClient({
  "base": "http://api.example.com/v1",
  "format": "json"
});
api('posts/latest') // equivalent to...
api.json('posts/latest')

Clone this wiki locally