Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bca14c0
Showing
14 changed files
with
567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# kickbox-node | ||
|
||
Official kickbox API library client for node.js | ||
|
||
__This library is generated by [alpaca](https://github.com/pksunkara/alpaca)__ | ||
|
||
## Installation | ||
|
||
Make sure you have [npm](https://npmjs.org) installed. | ||
|
||
```bash | ||
$ npm install kickbox | ||
``` | ||
|
||
#### Versions | ||
|
||
Works with [ 0.8 / 0.9 / 0.10 / 0.11 ] | ||
|
||
## Usage | ||
|
||
```js | ||
var kickbox = require('kickbox'); | ||
|
||
// Then we instantiate a client (as shown below) | ||
``` | ||
|
||
### Build a client | ||
|
||
__Using this api without authentication gives an error__ | ||
|
||
##### Authorization header token | ||
|
||
```js | ||
var client = kickbox.client('1a2b3', clientOptions); | ||
``` | ||
|
||
### Client Options | ||
|
||
The following options are available while instantiating a client: | ||
|
||
* __base__: Base url for the api | ||
* __api_version__: Default version of the api (to be used in url) | ||
* __user_agent__: Default user-agent for all requests | ||
* __headers__: Default headers for all requests | ||
* __request_type__: Default format of the request body | ||
|
||
### Response information | ||
|
||
__All the callbacks provided to an api call will recieve the response as shown below__ | ||
|
||
```js | ||
// You can also omit the 'methodOptions' param below | ||
client.klass('args').method('args', methodOptions, function (err, response) { | ||
if (err) console.log(err); | ||
|
||
response.code; | ||
// >>> 200 | ||
|
||
response.headers; | ||
// >>> {'x-server': 'apache'} | ||
} | ||
``` | ||
##### JSON response | ||
When the response sent by server is __json__, it is decoded into a hash | ||
```js | ||
response.body; | ||
// >>> {'user': 'pksunkara'} | ||
``` | ||
### Method Options | ||
The following options are available while calling a method of an api: | ||
* __api_version__: Version of the api (to be used in url) | ||
* __headers__: Headers for the request | ||
* __query__: Query parameters for the url | ||
* __body__: Body of the request | ||
* __request_type__: Format of the request body | ||
### Request body information | ||
Set __request_type__ in options to modify the body accordingly | ||
##### RAW request | ||
When the value is set to __raw__, don't modify the body at all. | ||
```js | ||
body = 'username=pksunkara'; | ||
// >>> 'username=pksunkara' | ||
``` | ||
### api | ||
```js | ||
var kickbox = client.kickbox(); | ||
``` | ||
##### (GET /verify?email=:email) | ||
Email Verification | ||
The following arguments are required: | ||
* __email__: Email address to verify | ||
```js | ||
kickbox.verify("test@example.com", options, callback); | ||
``` | ||
## Contributors | ||
Here is a list of [Contributors](https://github.com/kickbox/kickbox-node/contributors) | ||
### TODO | ||
## License | ||
MIT | ||
## Bug Reports | ||
Report [here](https://github.com/kickbox/kickbox-node/issues). | ||
## Contact | ||
Chaitanya Surapaneni (chaitanya.surapaneni@kickbox.io) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var Client = require('./kickbox/client'); | ||
|
||
// Export module | ||
var kickbox = module.exports; | ||
|
||
/** | ||
* This file contains the global namespace for the module | ||
*/ | ||
kickbox.client = function(auth, options) { | ||
return new Client(auth, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* | ||
*/ | ||
var Kickbox = function(client) { | ||
this.client = client; | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* Email Verification | ||
* | ||
* '/verify?email=:email' GET | ||
* | ||
* @param "email" Email address to verify | ||
*/ | ||
Kickbox.prototype.verify = function (email, options, callback) { | ||
if (typeof options == 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
|
||
var body = (options['query'] ? options['query'] : {}); | ||
|
||
this.client.get('/verify?email=' + email + '', body, options, function(err, response) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
callback(null, response); | ||
}); | ||
}; | ||
|
||
// Export module | ||
module.exports = Kickbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Main client for the module | ||
*/ | ||
var Client = function(auth, options) { | ||
this.httpClient = new (require('./http_client').HttpClient)(auth, options); | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* | ||
*/ | ||
Client.prototype.kickbox = function () { | ||
return new (require('./api/kickbox'))(this.httpClient); | ||
}; | ||
|
||
// Export module | ||
module.exports = Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var util = require('util'); | ||
|
||
/** | ||
* ClientError is used when the api returns an error | ||
*/ | ||
function ClientError(message, code) { | ||
var err = Error.call(this, message); | ||
err.code = code; | ||
|
||
return err | ||
} | ||
|
||
util.inherits(ClientError, Error); | ||
|
||
module.exports = ClientError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var error = module.exports; | ||
|
||
error.ClientError = require('./client_error'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* This module takes care of devising the auth type and using it | ||
*/ | ||
var Auth = function(auth) { | ||
this.auth = auth; | ||
|
||
this.HTTP_HEADER = 1; | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* Calculating the type of authentication | ||
*/ | ||
Auth.prototype.getAuthType = function () { | ||
|
||
if (this.auth['http_header']) { | ||
return this.HTTP_HEADER; | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
/** | ||
* Set authentication for the request | ||
* | ||
* This should throw error because this should be caught while in development | ||
*/ | ||
Auth.prototype.set = function (request, callback) { | ||
if (Object.keys(this.auth).length == 0) { | ||
return callback(new Error('Server requires authentication to proceed further. Please check')); | ||
} | ||
|
||
var auth = this.getAuthType(), flag = false; | ||
|
||
if (auth == this.HTTP_HEADER) { | ||
request = this.httpHeader(request); | ||
flag = true; | ||
} | ||
|
||
if (!flag) { | ||
return callback(new Error('Unable to calculate authorization method. Please check.')); | ||
} | ||
|
||
callback(null, request); | ||
}; | ||
|
||
/** | ||
* Authorization with HTTP header | ||
*/ | ||
Auth.prototype.httpHeader = function(request) { | ||
request['headers']['Authorization'] = 'token ' + this.auth['http_header']; | ||
|
||
return request; | ||
}; | ||
|
||
// Export module | ||
module.exports = Auth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var errors = require('../error'); | ||
|
||
/** | ||
* ErrorHanlder takes care of selecting the error message from response body | ||
*/ | ||
module.exports = function(response, body, callback) { | ||
var code = response.statusCode, message = '' | ||
, type = response.headers['content-type']; | ||
|
||
if (Math.floor(code/100) == 5) { | ||
return callback(new errors.ClientError('Error ' + code, code)); | ||
} | ||
|
||
if (Math.floor(code/100) == 4) { | ||
// If HTML, whole body is taken | ||
if (typeof body == 'string') { | ||
message = body; | ||
} | ||
|
||
// If JSON, a particular field is taken and used | ||
if (type.indexOf('json') != -1 && typeof body == 'object') { | ||
if (body['message']) { | ||
message = body['message']; | ||
} else { | ||
message = 'Unable to select error message from json returned by request responsible for error'; | ||
} | ||
} | ||
|
||
if (message == '') { | ||
message = 'Unable to understand the content type of response returned by request responsible for error'; | ||
} | ||
|
||
return callback(new errors.ClientError(message, code)); | ||
} | ||
|
||
return callback(null, response, body); | ||
}; |
Oops, something went wrong.