API client
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:
- GET https://remote.apihost.com:443/v1/api/user/1?apikey=asdf2345kjhnkj
- POST https://remote.apihost.com:443/v1/api/user?apikey=asdf2345kjhnkj
- PUT https://remote.apihost.com:443/v1/api/user/1?apikey=asdf2345kjhnkj
- DELETE https://remote.apihost.com:443/v1/api/user/1?apikey=asdf2345kjhnkj
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
- isEmptyObject(obj) ⇒
Boolean
Check if is object empty
ApiClient(data)
ApiClient constructor
Kind: global function
Param | Type | Description |
---|---|---|
data | Object |
Seed data. |
- ApiClient(data)
- .download(api, param, options, callback) ⇒
Void
- .get(api, param, options, callback) ⇒
Void
- .postUpload(api, param, options, callback) ⇒
Void
- .postForm(api, param, options, callback) ⇒
Void
- .post(api, param, options, callback) ⇒
Void
- .putForm(api, param, options, callback) ⇒
Void
- .put(api, param, options, callback) ⇒
Void
- .delete(api, param, options, callback) ⇒
Void
- .download(api, param, options, callback) ⇒
Void
apiClient.download(api, param, options, callback) ⇒ Download file from server
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.get(api, param, options, callback) ⇒ Get thing from server using method HTTP GET
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.postUpload(api, param, options, callback) ⇒ Upload file to server using POST method with attached file on body.
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.postForm(api, param, options, callback) ⇒ 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.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.post(api, param, options, callback) ⇒ 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.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.putForm(api, param, options, callback) ⇒ Send data using PUT method with multipart/form-data format body.
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.put(api, param, options, callback) ⇒ Send data using PUT method with raw string body.
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Void
apiClient.delete(api, param, options, callback) ⇒ Send data using DELETE method with raw string body.
Kind: instance method of ApiClient
Returns: Void
- Nothing to return.
Param | Type | Description |
---|---|---|
api | String |
Endpoint API. |
param | Object |
Parameter url object. |
options | Object |
Request option object. |
callback | function |
Callback function. |
Boolean
isEmptyObject(obj) ⇒ Check if is object empty
Kind: global function
Returns: Boolean
- Return true if empty or false otherwise.
Param | Type | Description |
---|---|---|
obj | Object |
Object to be Check |