Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
axeal committed Sep 6, 2017
1 parent b12e334 commit be1b63a
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
root = true

[*]
charset = utf-8
end_of_line = lf

[*.js]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,yml}]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
insert_final_newline = false
28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
4,
{
"SwitchCase": 1
}
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"no-console": 0
}
}
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
sudo: false

node_js:
- "0.10"
- "0.12"
- "iojs"
- "4"
- "6"
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
# cloudflare-reseller

[![Build Status](https://travis-ci.org/axeal/cloudflare-reseller.svg?branch=master)](https://travis-ci.org/axeal/cloudflare-reseller)
[![Coverage Status](https://coveralls.io/repos/github/axeal/cloudflare-reseller/badge.svg?branch=master)](https://coveralls.io/github/axeal/cloudflare-reseller?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/github/axeal/cloudflare-reseller/badge.svg)](https://snyk.io/test/github/axeal/cloudflare-reseller)

Cloudflare Reseller API Client

## APIs

This module can be used as a client to access the Cloudflare Reseller and Host APIs. API documentation with the available actions and required parameters can be found on the Cloudflare documentation site:

* [Reseller API](https://www.cloudflare.com/docs/reseller-api/)
* [Host API](https://www.cloudflare.com/docs/host-api/)

## Installation
This module can be install via npm:
```text
$ npm install --save cloudflare-reseller
```

## Usage

The `.call()` method accepts three arguments:
1. The name of the Cloudflare Reseller API action to call
2. An object with the variables for the API call
3. An optional callback function

If no callback function is passed to the call then a promise is returned. To use a callback instead, pass a callback function as the third argument.

```javascript
var cloudflare = require('cloudlfare-reseller');

//Configure the API client with the Cloudflare API Host Key
cloudflare.configure('hostKey');

//Example of a promise-based call to the client
cloudlfare.call('user_lookup', {
unique_id: 'test'
})
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.log(err);
});

//Example of a callback-based call to the client
cloudflare.call('user_lookup', {
unique_id: 'test'
}, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
```
75 changes: 75 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

var gulp = require('gulp');
var eslint = require('gulp-eslint');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var coveralls = require('gulp-coveralls');
var runSequence = require('run-sequence');
var rimraf = require('rimraf');
var _ = require('lodash');

var paths = {
libJsFiles: 'lib/**/*.js',
specFiles: 'test/**/*.js',
gulpfile: './gulpfile.js',
eslintrc: './.eslintrc.json'
};

gulp.task('lint', function() {
return gulp.src(paths.libJsFiles)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('test', ['clean'], function(done) {

var coverageVariable = '$$cov_' + new Date().getTime() + '$$';

gulp.src(paths.libJsFiles)
.pipe(istanbul({
includeUntested: true,
coverageVariable: coverageVariable
}))
.pipe(istanbul.hookRequire())
.on('finish', function() {
gulp.src(paths.specFiles)
.pipe(mocha({reporter: 'spec'}))
.pipe(istanbul.writeReports({
reporters: ['lcov'],
coverageVariable: coverageVariable
}))
.on('end', done);
});
});

gulp.task('clean', ['clean-coverage']);

gulp.task('clean-coverage', function (done) {
rimraf('./coverage', done);
});

gulp.task('validate', ['lint', 'test']);

gulp.task('watch', function() {
gulp.watch(_.flatten([
paths.libJsFiles,
paths.specFiles,
paths.gulpfile
]), [
'validate'
]);
gulp.watch(paths.eslintrc, ['lint']);
});

gulp.task('dev', ['watch', 'validate']);

gulp.task('ci', function(done) {
runSequence('validate', 'coveralls', done);
});

gulp.task('coveralls', function () {
return gulp.src('coverage/**/lcov.info')
.pipe(coveralls());
});
75 changes: 75 additions & 0 deletions lib/cloudflare-reseller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

var rp = require('request-promise');
var Promise = require('bluebird');
var util = require('util');
var debug = require('debug')('cloudflare-reseller');

var configuration = exports.configuration = {
url: 'https://api.cloudflare.com/host-gw.html',
key: ''
};

exports.configure = function(key) {
configuration.key = key;
};

var CloudflareError = exports.CloudflareError = function(message, errorCode) {
this.name = 'CloudflareError';
this.message = message;
this.errorCode = errorCode;
Error.captureStackTrace(this, CloudflareError);
};

util.inherits(CloudflareError, Error);

exports.call = function(action, params, cb) {

if(!params) {
params = {};
}

var retPromise = new Promise(function(resolve, reject) {

params['act'] = action;
params['host_key'] = configuration.key;

var options = {
url: configuration.url,
method: 'POST',
form: params,
resolveWithFullResponse: true
};

debug('Making Cloudflare-Reseller Request - url: %s action: %s', configuration.url, action);

rp(options)
.then(function(response) {
debug('Successful Cloudflare-Reseller Request - url: %s action: %s', configuration.url, action);

var jsonResponse = JSON.parse(response.body);
if(jsonResponse.result != 'success') {
var err = new CloudflareError(jsonResponse.msg, jsonResponse.err_code);
reject(err);
} else {
resolve(jsonResponse.response);
}
})
.catch(function(err) {
debug('Cloudflare-Reseller Request Error - url: %s action: %s msg: %s code: %s', configuration.url, action, err.msg, err.errorCode);
reject(err);
});
});

if(cb) {
retPromise
.then(function(response) {
return cb(null, response);
})
.catch(function(err) {
return cb(err);
});
}

return retPromise;
};
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "cloudflare-reseller",
"version": "1.0.0",
"description": "Cloudflare Reseller API client.",
"main": "lib/cloudflare-reseller.js",
"scripts": {
"test": "./node_modules/.bin/gulp ci"
},
"repository": {
"type": "git",
"url": "git+https://github.com/axeal/cloudflare-reseller.git"
},
"author": "Alex Seymour (https://github.com/axeal)",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/axeal/cloudflare-reseller/issues"
},
"homepage": "https://github.com/axeal/cloudlfare-reseller#readme",
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"bluebird": "^3.5.0",
"debug": "^2.6.8",
"request": "^2.81.0",
"request-promise": "^4.2.1",
"util": "^0.10.3"
},
"devDependencies": {
"chai": "^4.1.0",
"gulp": "^3.9.1",
"gulp-coveralls": "^0.1.4",
"gulp-eslint": "~2.1.0",
"gulp-istanbul": "^1.1.2",
"gulp-mocha": "~3.0.1",
"lodash": "^4.17.4",
"mocha": "^3.4.2",
"nock": "^9.0.14",
"rimraf": "^2.6.1",
"run-sequence": "^2.1.0"
}
}
5 changes: 5 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}

0 comments on commit be1b63a

Please sign in to comment.