0.2.5 • Published 8 years ago

apiclient v0.2.5

Weekly downloads
13
License
MIT
Repository
github
Last release
8 years ago

API client

Build Status

API client adalah request wrapper

Basically, ApiClient is just a request wrapper. It wrap all request to more structured way. The way we separate remote endpoint specification from coding overhead.

Because of apiclient is just wrapping request, any of request options can be used here.

Usage

To access these URLs, we use apiclient this way :

URLs:

var Apiclient = require('apiclient');
var seed = {
  base: {
    protocol: 'https',
    hostname: 'remote.apihost.com',
    port: 443,
    pathname: 'v1/api'
  },
  path: {
    GET: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    POST: {
      user: {
        location: 'user',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    PUT: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    },
    DELETE: {
      user: {
        location: 'user/%(userid)d',
        query: {
          apikey: 'asdf2345kjhnkj'
        }
      }
    }
  }
}

var Api = new Apiclient(seed);

Api.get('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});

Api.post('user', {}, {
  body: {
    username: 'John',
    password: 'John123'
  }
}, function (error, response, body) {
  console.log(error, response, body);
});

Api.put('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});

Api.delete('user', {userid: 1}, {}, function (error, response, body) {
  console.log(error, response, body);
});

Functions

ApiClient(data)

ApiClient constructor

Kind: global function

ParamTypeDescription
dataObjectSeed data.

apiClient.download(api, param, options, callback) ⇒ Void

Download file from server

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.get(api, param, options, callback) ⇒ Void

Get thing from server using method HTTP GET

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.postUpload(api, param, options, callback) ⇒ Void

Upload file to server using POST method with attached file on body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.postForm(api, param, options, callback) ⇒ Void

Upload file to server using POST method with attached file on body, formatted as multipart form.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.post(api, param, options, callback) ⇒ Void

Send string via POST method. This API will keep body on memory, attaching big string more than 1MB is not recommended, as it will fill the memory so fast.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.putForm(api, param, options, callback) ⇒ Void

Send data using PUT method with multipart/form-data format body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.put(api, param, options, callback) ⇒ Void

Send data using PUT method with raw string body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

apiClient.delete(api, param, options, callback) ⇒ Void

Send data using DELETE method with raw string body.

Kind: instance method of ApiClient
Returns: Void - Nothing to return.

ParamTypeDescription
apiStringEndpoint API.
paramObjectParameter url object.
optionsObjectRequest option object.
callbackfunctionCallback function.

isEmptyObject(obj) ⇒ Boolean

Check if is object empty

Kind: global function
Returns: Boolean - Return true if empty or false otherwise.

ParamTypeDescription
objObjectObject to be Check