Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhat Can committed Jun 8, 2016
0 parents commit 900cbd4
Show file tree
Hide file tree
Showing 30 changed files with 891 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
npm-debug.log
node_modules
report
.idea/

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.tmp

pids
logs
results

samples/config.json
63 changes: 63 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"passfail": false,
"maxerr": 100,
"browser": false,
"node": true,
"rhino": false,
"couch": false,
"wsh": false,
"jquery": false,
"prototypejs": false,
"mootools": false,
"dojo": false,
"predef": [
"describe",
"it"
],
"debug": false,
"devel": false,
"es5": true,
"strict": true,
"globalstrict": false,
"asi": false,
"laxbreak": false,
"bitwise": false,
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": false,
"evil": false,
"expr": true,
"forin": false,
"immed": true,
"latedef": false,
"loopfunc": false,
"noarg": false,
"regexp": false,
"regexdash": false,
"scripturl": false,
"shadow": false,
"supernew": false,
"undef": true,
"validthis": false,
"smarttabs": true,
"proto": false,
"onecase": false,
"nonstandard": false,
"multistr": false,
"laxcomma": false,
"lastsemic": false,
"iterator": false,
"funcscope": false,
"esnext": false,
"newcap": false,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"sub": false,
"trailing": true,
"white": true,
"indent": 4
}
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "4.0"
- "4.1"
- "stable"
before_install:
- npm install -g grunt-cli
after_script:
- mocha -t 60000 --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
after_success:
- if [ $TRAVIS_NODE_VERSION = '0.10' ] && [ $TRAVIS_BRANCH = 'master' ] && [ $TRAVIS_PULL_REQUEST = 'false' ]; then sh generate-doc.sh; fi
54 changes: 54 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = function (grunt) {

"use strict";

// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
meta: {
banner: "/*!\n * <%= pkg.name %>\n * <%= pkg.description %>\n * @version <%= pkg.version %> - <%= grunt.template.today(\'yyyy-mm-dd\') %>\n * @author <%= pkg.author.name %> <<%= pkg.author.url %>>\n */\n"
},
jshint: {
all: {
src: ["lib/*.js", "test/*.js", "samples/**/*.js", "lib/resources/*.js"],
options: {
jshintrc: ".jshintrc"
}
}
},
simplemocha: {
options: {
timeout: 15000,
reporter: 'dot'
},
all: {
src: 'test/*.js'
}
}
/*,
jsdoc: {
dist: {
src: ['lib/!*'],
jsdoc: './node_modules/.bin/jsdoc',
options: {
destination: 'docs/jsdoc/',
configure: './node_modules/jsdoc/conf.json',
template: './node_modules/ink-docstrap/template'
}
}
}
*/
});

// Load grunt tasks from npm packages
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks('grunt-simple-mocha');
// grunt.loadNpmTasks('grunt-jsdoc');

// Test task
grunt.registerTask("test", ["simplemocha"]);

// Default task.
grunt.registerTask("default", ["jshint", "simplemocha"]);

};
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# OpsGenie NodeJs SDK

Build status: [![Build Status](https://travis-ci.com/srhtcn/opsgenie-nodejs-sdk.svg?token=QTDpPBArevfhSpyxWwwz&branch=master)](https://travis-ci.com/srhtcn/opsgenie-nodejs-sdk)

This is the unofficial (for now) NodeJs sdk for OpsGenie.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Created by Serhat Can on 05/06/16.
*/
module.exports = require('./lib/opsgenie-sdk.js')();
153 changes: 153 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"use strict";

var utils = require('./utils');
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var configuration = require('./configure');

var configure = exports.configure = function configure(options) {
if (options !== undefined && typeof options === 'object') {
configuration.default_options = utils.merge(configuration.default_options, options);
}
};

/**
* Wraps the http client, handles request parameters, populates request headers, handles response
*
* @type {exports.executeHttp}
*/
var executeHttp = exports.executeHttp = function executeHttp(http_method, path, data, http_options, cb) {
if (typeof http_options === "function") {
cb = http_options;
http_options = null;
}
if (!http_options) {
http_options = configuration.default_options;
} else {
http_options = utils.merge(http_options, configuration.default_options, true);
}

//Get host endpoint using mode
http_options.host = utils.getDefaultApiEndpoint(http_options.mode) || http_options.host;

var client = (http_options.schema === 'http') ? http : https;

var request_data = data;

if (http_method === 'GET' || http_method === 'DELETE') {
//format object parameters into GET request query string
if (typeof request_data !== 'string') {
request_data = querystring.stringify(request_data);
}
if (request_data) {
path = path + "?" + request_data + "&apiKey=" + http_options.api_key;
request_data = "";
} else {
// add api key to request
path += "?apiKey=" + http_options.api_key;
}


} else if (typeof request_data !== 'string') {
// add api key to request
request_data.apiKey = http_options.api_key;
request_data = JSON.stringify(request_data);
}

if (http_options) {

http_options = JSON.parse(JSON.stringify(http_options));

if (!http_options.headers) {
http_options.headers = {};
}
http_options.path = path;
http_options.method = http_method;
if (request_data) {
http_options.headers['Content-Length'] = Buffer.byteLength(request_data, 'utf-8');
}

if (!http_options.headers.Accept) {
http_options.headers.Accept = 'application/json';
}

if (!http_options.headers['Content-Type']) {
http_options.headers['Content-Type'] = 'application/json';
}
}

// Enable full request response logging in development/non-production environment only
if (configuration.default_options.mode !== 'live' && process.env.NODE_ENV === 'development') {
console.dir(JSON.stringify(http_options.headers));
console.dir(request_data);
}

// client is http or https library
var req = client.request(http_options);
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
cb(e, null);
});

req.on('response', function (res) {
var response = '';

//do not setEndcoding with browserify
if (res.setEncoding) {
res.setEncoding('utf8');
}

res.on('data', function (chunk) {
response += chunk;
});

res.on('end', function () {
var err = null;

try {
if (process.env.NODE_ENV === 'development') {
console.dir("Development request log:");
console.dir(JSON.stringify(res.headers));
console.dir(response);
}

//Set response to be parsed JSON object if data received is json
//expect that content-type header has application/json when it
//returns data
if (typeof res.headers['content-type'] === "string" && res.headers['content-type'].match(/^application\/json(?:;.*)?$/) !== null) {
response = JSON.parse(response);
}
//Set response to an empty object if no data was received
if (response === '') {
response = {};
}
response.httpStatusCode = res.statusCode;
} catch (e) {
err = new Error('Invalid JSON Response Received.');
err.error = {
name: 'Invalid JSON Response Received, JSON Parse Error.'
};
err.response = response;
err.httpStatusCode = res.statusCode;
response = null;
}

if (!err && (res.statusCode < 200 || res.statusCode >= 300)) {
err = new Error('Response Status : ' + res.statusCode);
err.response = response;
if (process.env.NODE_ENV === 'development') {
err.response_stringified = JSON.stringify(response);
}
err.httpStatusCode = res.statusCode;
response = null;
}
cb(err, response);
});
});

if (request_data) {
req.write(request_data);
}
req.end();
};
9 changes: 9 additions & 0 deletions lib/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var sdkVersion = exports.sdkVersion = require('../package').version;

var default_options = exports.default_options = {
'mode': 'live',
'schema': 'https',
'headers': {}
};
Loading

0 comments on commit 900cbd4

Please sign in to comment.