REST¶
REST library takes its roots on Http library, and adds some syntactic sugar for RESTFul calls.
- class Rest.Client(baseUrl, options)¶
- Arguments
baseUrl (
string()
) – RPC target URLopts (
object()
) – Options
Accepted options are:
http_transaction_factory
A function yielding a transaction. Defaults to
Http.Transaction.factory()
.suffix
String to append to all fully-formed URLs. Defaults to “/”.
- Rest.Client.Rest.Client.add(name, options)¶
- Arguments
name (
string()
) – resource name to addopts (
object()
) – Options
Adds a resource path to client handle.
Accepted options are:
url
A relative URL to add to parent path. Defaults to
name
.suffix
String to append to all fully-formed URLs. Defaults to client
suffix
option value.flat
Whether this resource has no ID.
Each time a resource path is added, user may add sub-resources by calling
add()
again on them.
- class RestResource()¶
This may not be created explicitely, but created through
Rest.Client.add()
orRestResource.add()
.- RestResource.RestResource.add(name, options)¶
- Arguments
name (
string()
) – resource name to addopts (
object()
) – Options
Adds a resource path as subresource of the current one.
Accepted options are:
url
A relative URL to add to parent path. Defaults to
name
.suffix
String to append to all fully-formed URLs. Defaults to client
suffix
option value.flat
Whether this resource has no ID.
- RestResource.RestResource.read(ids...)¶
- Arguments
ids – Variadic of IDs for object to retrieve
- Returns
A
Deferred.Deferred()
resolved when HTTP server answers GET
- RestResource.RestResource.create(ids..., data)¶
- Arguments
ids – Variadic of IDs for object to create
data (
object()
) – An object to POST for creation
- Returns
A
Deferred.Deferred()
resolved when HTTP server answers POST
- RestResource.RestResource.update(ids..., data)¶
- Arguments
ids – Variadic of IDs for object to update
data (
object()
) – An object to PUT for update
- Returns
A
Deferred.Deferred()
resolved when HTTP server answers PUT
- RestResource.RestResource.destroy(ids...)¶
- Arguments
ids – Variadic of IDs for object to DELETE
- Returns
A
Deferred.Deferred()
resolved when HTTP server answers DELETE
var client = new Rest.Client("http://example.com/api/v1", { http_transaction_factory: Http.Transaction.debug_factory }); client.add("storage", {flat: true}); client.storage.add("partition"); client.storage.partition.add("check", {flat: true}); client.storage.partition.read().then(function (data) { console.log("Got partition list:", data); }); client.storage.partition.read(1).then(function (data) { console.log("Got partition info for 1:", data); }); client.storage.partition.update(1, {test: true}).then(function (data) { console.log("Updated 1"); }); client.storage.partition.delete(1).then(function (data) { console.log("Deleted 1"); });
Transaction factory may also be overridden with an OAuth client. See OAuth library documentation for details.