onep v0.4.1
This is a node.js library that provides access to the One Platform RPC API.
Usage
var rpc = require('onep/rpc');
rpc.call(cik, 'info', [{alias: ''}, {}],
function(err, rpcresponse, httpresponse) {
if (err) {
console.log('error: ' + err);
} else {
if (rpcresponse[0].status === 'ok') {
console.log(JSON.stringify(rpcresponse[0].result));
} else {
console.log('Bad status: ' + rpcresponse[0].status);
}
}
});API
rpc.call
Make a single call to the RPC API.
rpc.call(auth, procedure, arguments, callback)authmay be a 40 character client key (e.g.'0808160000000000000000000000000000000000'), or an auth object (e.g.{cik: '0808160000000000000000000000000000000000', client_key: 'e208160000000000000000000000000000000000'}). See the RPC documentation for details.procedureis the procedure name, e.g.'read'or'info'.argumentsis a list of arguments to pass the call. These are specific to<procedure>. See the RPC documentation for a list of procedures and arguments.callbackis a function that takes three parameters:erris null if the call was made successfully (no HTTP or RPC general error). If the call failed,errcontains the error message.rpcresponsecontains a list containing a single RPC response.httpresponseis the response object.
Example
var rpc = require('onep/rpc');
rpc.call(
cik,
'listing',
[['client', 'dataport'], {}],
function(err, rpcresponse, httpresponse) {
if (err) {
console.log('error: ' + err);
} else {
console.log('response: ' + util.inspect(rpcresponse, false, null));
}
});
// output:
response: [ { status: 'ok',
result:
{ client: [ '0808160000000000000000000000000000000000',
'c138cc0000000000000000000000000000000000',
'fdc8160000000000000000000000000000000000' ],
dataport: [ '7e5c6f0000000000000000000000000000000000' ] } } ]rpc.callMulti
Make multiple calls to the RPC API.
rpc.callMulti(auth, calls, callback)authmay be a 40 character client key (e.g.'0808160000000000000000000000000000000000'), or an auth object (e.g.{cik: '0808160000000000000000000000000000000000', client_key: 'e208160000000000000000000000000000000000'}). See the RPC documentation for details.callsis a list of calls like this:{procedure: <procedure>, arguments: <arguments>}. See the RPC documentation for a list of procedures and arguments.callbackis a function is called when the call completes. It takes three parameters:erris null if the call was made successfully (no HTTP or RPC general error). If the call failed,errcontains the error message.rpcresponsecontains a list of RPC responses.httpresponseis the response object.
Example
// get listing and info in one request
rpc.callMulti(
cik,
[
{procedure: 'info', arguments: [{alias: ""}, {basic: true}]}
{procedure: 'listing', arguments: [['client', 'dataport']]},
],
function(err, rpcresponse, httpresponse) {
if (err) {
console.log('error: ' + err);
} else {
console.log('response: ' + util.inspect(rpcresponse, false, null));
}
});
// output:
response: [ { id: 0,
status: 'ok',
result:
{ client: [ '0808160000000000000000000000000000000000',
'c138cc0000000000000000000000000000000000',
'fdc8160000000000000000000000000000000000' ],
dataport: [ '7e5c6f0000000000000000000000000000000000' ] } },
{ id: 1,
status: 'ok',
result:
{ basic:
{ modified: 1374553089,
subscribers: 0,
type: 'client',
status: 'activated' } } } ]rpc.tree
Return the RIDs of all descendants of a client.
rpc.tree(auth, options, callback)authmay be a 40 character client key (e.g.'0808160000000000000000000000000000000000'), or an auth object (e.g.{cik: '0808160000000000000000000000000000000000', client_key: 'e208160000000000000000000000000000000000'}). See the RPC documentation for details.optionsis an object with the following keys, all optional:depthstop at a given depth from the root client (if omitted, depth is not limited)visitfunction to call for each resource visited. It is called withrid,type, anddepthparameters.typeslist of type strings. Options are 'dataport', 'datarule', 'dispatch'. If omitted, visits only clients.infoif non-null, calls info on each resource with the specified options, e.g.,{basic: true}. May also be a function called withrid,type, anddepthparameters for each resource. If a function, it should return null or info options for that resource.
callbackis a function is called when the call completes. It takes the following parameters:erris null if the call succeeded. If the call failed,errcontains the error message.treeis the tree. If the call failed, it is undefined.
Example
rpc.tree(
'76343a56f23d9ee80f2569c8209f4e9b11a5752e',
{
depth: 1,
visit: function (rid, type, depth) {
console.log(rid + ' (' + type + ') depth:' + depth);
},
types: ['dataport', 'datarule', 'dispatch'],
info: function(rid, type, depth) {
return type === 'client' ? {basic: true} : null;
}
},
function(err, tree) {
console.log(err ? 'Error ' + err : JSON.stringify(tree, null, 2));
});
// output
datarule
30e8a54ef29e55415b444ed72a961be4ea0ec270 (client) depth:0
0affc14ebdf520587b6957170ec6af06b7b6ca2a (dataport) depth:1
cbf6f7731ed8ae0285b2e8f9b437356544332679 (dataport) depth:1
a0ebc3d703c94e9b37bb4a2796e447334d7ffea2 (datarule) depth:1
{
"rid": "30e8a54ef29e55415b444ed72a961be4ea0ec270",
"type": "client",
"children": [
{
"rid": "0affc14ebdf520587b6957170ec6af06b7b6ca2a",
"type": "dataport"
},
{
"rid": "cbf6f7731ed8ae0285b2e8f9b437356544332679",
"type": "dataport"
},
{
"rid": "a0ebc3d703c94e9b37bb4a2796e447334d7ffea2",
"type": "datarule"
}
],
"info": {
"basic": {
"modified": 1422061952,
"subscribers": 0,
"type": "client",
"status": "activated"
}
}
}rpc.batch
Make a lot of RPC calls, splitting the calls into evenly sized chunks to avoid timeouts and calling in parallel to go fast.
rpc.batch(auth, calls, options, callback)authis an RPC auth object or CIK stringcallsis an array of call objects like this:{procedure: <procedure>, arguments: <args>}optionsare:chunkSizeis the number of calls per requestparallelLimitis the maximum number of concurrent requests to run in parallel
rpc.walk
Walk the object returned by tree().
rpc.walk(tree, visit);treeis the object returned by calling tree()calls
visit(resource, depth, parentRID)on each resource. For the root resource, parentRID is undefined.
rpc.setOptions
Set options for connecting to the One Platform.
rpc.setOptions(\<options\>)<options>contains one or more options to update. Default options are as follows:
{
host: 'm2.exosite.com',
path: '/api:v1/rpc/process',
agent: 'node-onep',
https: true,
port: 443,
strictSSL: true
}You may also specify timeout in milliseconds, which defaults to the default for the request module.
Tests
$ cp test/config-template.js test/config.js
$ mochaBy default the tests run against the mock 1P server. To make them run against production 1P, modify test/config.js with a production CIK and turn off using the mock server.
