apiclient
v0.2.5
Published
request wrapper
Downloads
86
Readme
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
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
apiClient.download(api, param, options, callback) ⇒ Void
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. |
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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback 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.
| Param | Type | Description | | --- | --- | --- | | api | String | Endpoint API. | | param | Object | Parameter url object. | | options | Object | Request option object. | | callback | function | Callback function. |
isEmptyObject(obj) ⇒ Boolean
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 |