Skip to content

Commit 900cbd4

Browse files
author
Serhat Can
committed
Initial commit
0 parents  commit 900cbd4

30 files changed

+891
-0
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
npm-debug.log
2+
node_modules
3+
report
4+
.idea/
5+
6+
lib-cov
7+
*.seed
8+
*.log
9+
*.csv
10+
*.dat
11+
*.out
12+
*.pid
13+
*.gz
14+
*.tmp
15+
16+
pids
17+
logs
18+
results
19+
20+
samples/config.json

.jshintrc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"passfail": false,
3+
"maxerr": 100,
4+
"browser": false,
5+
"node": true,
6+
"rhino": false,
7+
"couch": false,
8+
"wsh": false,
9+
"jquery": false,
10+
"prototypejs": false,
11+
"mootools": false,
12+
"dojo": false,
13+
"predef": [
14+
"describe",
15+
"it"
16+
],
17+
"debug": false,
18+
"devel": false,
19+
"es5": true,
20+
"strict": true,
21+
"globalstrict": false,
22+
"asi": false,
23+
"laxbreak": false,
24+
"bitwise": false,
25+
"boss": true,
26+
"curly": true,
27+
"eqeqeq": true,
28+
"eqnull": false,
29+
"evil": false,
30+
"expr": true,
31+
"forin": false,
32+
"immed": true,
33+
"latedef": false,
34+
"loopfunc": false,
35+
"noarg": false,
36+
"regexp": false,
37+
"regexdash": false,
38+
"scripturl": false,
39+
"shadow": false,
40+
"supernew": false,
41+
"undef": true,
42+
"validthis": false,
43+
"smarttabs": true,
44+
"proto": false,
45+
"onecase": false,
46+
"nonstandard": false,
47+
"multistr": false,
48+
"laxcomma": false,
49+
"lastsemic": false,
50+
"iterator": false,
51+
"funcscope": false,
52+
"esnext": false,
53+
"newcap": false,
54+
"noempty": false,
55+
"nonew": false,
56+
"nomen": false,
57+
"onevar": false,
58+
"plusplus": false,
59+
"sub": false,
60+
"trailing": true,
61+
"white": true,
62+
"indent": 4
63+
}

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "0.10"
5+
- "4.0"
6+
- "4.1"
7+
- "stable"
8+
before_install:
9+
- npm install -g grunt-cli
10+
after_script:
11+
- mocha -t 60000 --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
12+
after_success:
13+
- if [ $TRAVIS_NODE_VERSION = '0.10' ] && [ $TRAVIS_BRANCH = 'master' ] && [ $TRAVIS_PULL_REQUEST = 'false' ]; then sh generate-doc.sh; fi

Gruntfile.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module.exports = function (grunt) {
2+
3+
"use strict";
4+
5+
// Project configuration.
6+
grunt.initConfig({
7+
pkg: grunt.file.readJSON("package.json"),
8+
meta: {
9+
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"
10+
},
11+
jshint: {
12+
all: {
13+
src: ["lib/*.js", "test/*.js", "samples/**/*.js", "lib/resources/*.js"],
14+
options: {
15+
jshintrc: ".jshintrc"
16+
}
17+
}
18+
},
19+
simplemocha: {
20+
options: {
21+
timeout: 15000,
22+
reporter: 'dot'
23+
},
24+
all: {
25+
src: 'test/*.js'
26+
}
27+
}
28+
/*,
29+
jsdoc: {
30+
dist: {
31+
src: ['lib/!*'],
32+
jsdoc: './node_modules/.bin/jsdoc',
33+
options: {
34+
destination: 'docs/jsdoc/',
35+
configure: './node_modules/jsdoc/conf.json',
36+
template: './node_modules/ink-docstrap/template'
37+
}
38+
}
39+
}
40+
*/
41+
});
42+
43+
// Load grunt tasks from npm packages
44+
grunt.loadNpmTasks("grunt-contrib-jshint");
45+
grunt.loadNpmTasks('grunt-simple-mocha');
46+
// grunt.loadNpmTasks('grunt-jsdoc');
47+
48+
// Test task
49+
grunt.registerTask("test", ["simplemocha"]);
50+
51+
// Default task.
52+
grunt.registerTask("default", ["jshint", "simplemocha"]);
53+
54+
};

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OpsGenie NodeJs SDK
2+
3+
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)
4+
5+
This is the unofficial (for now) NodeJs sdk for OpsGenie.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Created by Serhat Can on 05/06/16.
3+
*/
4+
module.exports = require('./lib/opsgenie-sdk.js')();

lib/client.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"use strict";
2+
3+
var utils = require('./utils');
4+
var http = require('http');
5+
var https = require('https');
6+
var querystring = require('querystring');
7+
var configuration = require('./configure');
8+
9+
var configure = exports.configure = function configure(options) {
10+
if (options !== undefined && typeof options === 'object') {
11+
configuration.default_options = utils.merge(configuration.default_options, options);
12+
}
13+
};
14+
15+
/**
16+
* Wraps the http client, handles request parameters, populates request headers, handles response
17+
*
18+
* @type {exports.executeHttp}
19+
*/
20+
var executeHttp = exports.executeHttp = function executeHttp(http_method, path, data, http_options, cb) {
21+
if (typeof http_options === "function") {
22+
cb = http_options;
23+
http_options = null;
24+
}
25+
if (!http_options) {
26+
http_options = configuration.default_options;
27+
} else {
28+
http_options = utils.merge(http_options, configuration.default_options, true);
29+
}
30+
31+
//Get host endpoint using mode
32+
http_options.host = utils.getDefaultApiEndpoint(http_options.mode) || http_options.host;
33+
34+
var client = (http_options.schema === 'http') ? http : https;
35+
36+
var request_data = data;
37+
38+
if (http_method === 'GET' || http_method === 'DELETE') {
39+
//format object parameters into GET request query string
40+
if (typeof request_data !== 'string') {
41+
request_data = querystring.stringify(request_data);
42+
}
43+
if (request_data) {
44+
path = path + "?" + request_data + "&apiKey=" + http_options.api_key;
45+
request_data = "";
46+
} else {
47+
// add api key to request
48+
path += "?apiKey=" + http_options.api_key;
49+
}
50+
51+
52+
} else if (typeof request_data !== 'string') {
53+
// add api key to request
54+
request_data.apiKey = http_options.api_key;
55+
request_data = JSON.stringify(request_data);
56+
}
57+
58+
if (http_options) {
59+
60+
http_options = JSON.parse(JSON.stringify(http_options));
61+
62+
if (!http_options.headers) {
63+
http_options.headers = {};
64+
}
65+
http_options.path = path;
66+
http_options.method = http_method;
67+
if (request_data) {
68+
http_options.headers['Content-Length'] = Buffer.byteLength(request_data, 'utf-8');
69+
}
70+
71+
if (!http_options.headers.Accept) {
72+
http_options.headers.Accept = 'application/json';
73+
}
74+
75+
if (!http_options.headers['Content-Type']) {
76+
http_options.headers['Content-Type'] = 'application/json';
77+
}
78+
}
79+
80+
// Enable full request response logging in development/non-production environment only
81+
if (configuration.default_options.mode !== 'live' && process.env.NODE_ENV === 'development') {
82+
console.dir(JSON.stringify(http_options.headers));
83+
console.dir(request_data);
84+
}
85+
86+
// client is http or https library
87+
var req = client.request(http_options);
88+
req.on('error', function (e) {
89+
console.log('problem with request: ' + e.message);
90+
cb(e, null);
91+
});
92+
93+
req.on('response', function (res) {
94+
var response = '';
95+
96+
//do not setEndcoding with browserify
97+
if (res.setEncoding) {
98+
res.setEncoding('utf8');
99+
}
100+
101+
res.on('data', function (chunk) {
102+
response += chunk;
103+
});
104+
105+
res.on('end', function () {
106+
var err = null;
107+
108+
try {
109+
if (process.env.NODE_ENV === 'development') {
110+
console.dir("Development request log:");
111+
console.dir(JSON.stringify(res.headers));
112+
console.dir(response);
113+
}
114+
115+
//Set response to be parsed JSON object if data received is json
116+
//expect that content-type header has application/json when it
117+
//returns data
118+
if (typeof res.headers['content-type'] === "string" && res.headers['content-type'].match(/^application\/json(?:;.*)?$/) !== null) {
119+
response = JSON.parse(response);
120+
}
121+
//Set response to an empty object if no data was received
122+
if (response === '') {
123+
response = {};
124+
}
125+
response.httpStatusCode = res.statusCode;
126+
} catch (e) {
127+
err = new Error('Invalid JSON Response Received.');
128+
err.error = {
129+
name: 'Invalid JSON Response Received, JSON Parse Error.'
130+
};
131+
err.response = response;
132+
err.httpStatusCode = res.statusCode;
133+
response = null;
134+
}
135+
136+
if (!err && (res.statusCode < 200 || res.statusCode >= 300)) {
137+
err = new Error('Response Status : ' + res.statusCode);
138+
err.response = response;
139+
if (process.env.NODE_ENV === 'development') {
140+
err.response_stringified = JSON.stringify(response);
141+
}
142+
err.httpStatusCode = res.statusCode;
143+
response = null;
144+
}
145+
cb(err, response);
146+
});
147+
});
148+
149+
if (request_data) {
150+
req.write(request_data);
151+
}
152+
req.end();
153+
};

lib/configure.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
var sdkVersion = exports.sdkVersion = require('../package').version;
4+
5+
var default_options = exports.default_options = {
6+
'mode': 'live',
7+
'schema': 'https',
8+
'headers': {}
9+
};

0 commit comments

Comments
 (0)