Browse Source

functioning http_client.request

pull/2/merge
Brett Langdon 13 years ago
parent
commit
be87247c76
1 changed files with 80 additions and 4 deletions
  1. +80
    -4
      lib/http_client.js

+ 80
- 4
lib/http_client.js View File

@ -1,4 +1,7 @@
var extend = require('extend');
var https = require('https'); var https = require('https');
var url = require('url');
var util = require('util');
var v8type = require('v8type'); var v8type = require('v8type');
@ -13,7 +16,7 @@ var client = function(options){
* 'api_key': 'datadog api key', * 'api_key': 'datadog api key',
* 'app_key': 'datadog app key', * 'app_key': 'datadog app key',
* 'api_version': 'default is v1', * 'api_version': 'default is v1',
* 'api_host': 'default is https://app.datadoghq.com',
* 'api_host': 'default is app.datadoghq.com',
* } * }
* *
* this client also allows you to provide these options as * this client also allows you to provide these options as
@ -43,17 +46,90 @@ var client = function(options){
this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST']; this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST'];
if(!this.api_host){ if(!this.api_host){
this.api_host = 'https://app.datadoghq.com';
this.api_host = 'app.datadoghq.com';
} }
}; };
client.prototype.request = function(method, url, params, callback){
client.prototype.request = function(method, path, params, callback){
/*
* http_client.request(method, path, [[params], callback])
*
* method used to send an http request to the datadog api service
*
* `method` is the http method to use ('POST', 'GET', 'DELETE', etc)
* `path` the api url to call, e.g '/events', '/series', '/dash', etc
* `params` an optional object of additional options:
* {
* 'query': { 'key': 'value' },
* 'body': { 'json/object': 'body' } or 'string body',
* }
* `callback` an optional function to obtain the results of the api call
* callback(error, result)
*/
if(arguments.length == 3 && v8type.is(arguments[2], v8type.FUNCTION)){ if(arguments.length == 3 && v8type.is(arguments[2], v8type.FUNCTION)){
params = {};
callback = arguments[2]; callback = arguments[2];
params = {};
}
params = params || {};
var body = v8type.is(params['body'], v8type.OBJECT)? JSON.stringify(params['body']) : params['body'];
var query = {
'api_key': this.api_key,
'application_key': this.app_key,
};
if(v8type.is(params['query'], v8type.OBJECT)){
extend(query, params['query']);
}
path = url.format({
'pathname': util.format('/api/%s%s', this.api_version, path),
'query': query,
});
var http_options = {
hostname: this.api_host,
port: 443,
method: method.toUpperCase(),
path: path,
};
if(http_options['method'] == 'POST'){
http_options['headers'] = {
'Content-Type': 'application/json',
'Content-Length': body.length,
};
}
var req = https.request(http_options, function(res){
res.on('error', function(err){
if(v8type.is(callback, v8type.FUNCTION)){
callback(err, null, res.statusCode);
}
});
var data = '';
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
if(res.headers['content-type'] == 'application/json'){
data = JSON.parse(data);
}
if(v8type.is(callback, v8type.FUNCTION)){
callback(null, data, res.statusCode);
}
});
});
if(http_options['method'] == 'POST'){
req.write(body);
} }
req.end()
}; };
return module.exports = client; return module.exports = client;

Loading…
Cancel
Save