Skip to content

RESTFul api client base object. Usually use by some api client implementation.

Notifications You must be signed in to change notification settings

agarnaas/restful-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

restful-client

NPM version build status Test coverage Gittip David deps node version npm download

RESTFul api client base object. Usually use by some api client implementation.

Example: gitlab

Install

$ npm install restful-client --save

Usage

var restful = require('restful-client');
var util = require('util');

function Project(client) {
  restful.RESTFulResource.call(this, client, '/projects', 'id');
}
util.inherits(Project, restful.RESTFulResource);

function Gitlab(options) {
  options = options || {};
  options.api = options.api || 'https://gitlab.com/api/v3';
  restful.RESTFulClient.call(this, options);
  this.token = options.token;

  this.addResources({
    projects: Project
  });
}

util.inherits(Gitlab, restful.RESTFulClient);

Gitlab.prototype.setAuthentication = function (req) {
  req.params.data.private_token = req.params.data.private_token || this.token;
  return req;
};

var gitlab = new Gitlab({token: 'your token', requestTimeout: 5000});
gitlab.projects.list({page: 1}, function (err, result) {
  console.log(arguments);
});

RESTFulResource

Every RESTFulResource instance has these methods:

  • constructor
/**
 * RESTFul Resource base class.
 *
 * @param {Client} client RESTful api client instance.
 * @param {String} resourcePath resource url path, e.g.: '/issues'
 * @param {String} idName id name for get one entry, e.g.: 'id', 'name'
 * @param {String} [updateMethod] update http method, default is 'put'
 */
function RESTFulResource(client, resourcePath, idName, updateMethod);
  • get(params, callback)
  • list(params, callback)
  • create(params, callback)
  • update(params, callback)
  • remove(params, callback)

Example

function Project(client) {
  this.constructor.super_.call(this, client, '/projects', 'id');
}
util.inherits(Project, restful.RESTFulResource);

License

(The MIT License)

Copyright (c) 2013 - 2014 fengmk2 <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

RESTFul api client base object. Usually use by some api client implementation.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%