From 7b55a272243b3c23b0792d3111ffc8f6d3a70f63 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 18 Mar 2015 08:51:44 -0400 Subject: [PATCH] replace old http client library with a new one --- lib/client.js | 122 ++++++++++++++++++++++++++++++++++++++ lib/http_client.js | 145 --------------------------------------------- 2 files changed, 122 insertions(+), 145 deletions(-) create mode 100644 lib/client.js delete mode 100644 lib/http_client.js diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..3e91e40 --- /dev/null +++ b/lib/client.js @@ -0,0 +1,122 @@ +var extend = require("extend"); +var https = require("https"); +var url = require("url"); +var util = require("util"); + + +/*section: client + *comment: | + * the constructor for _client_ object + *params: + *example: | + * See [client.request](#client-request) + */ +var client = function(){ + this.api_key = null; + this.app_key = null; + this.api_version = "v1"; + this.api_host = "app.datadoghq.com"; +}; + +/*section: client + *comment: | + * used to make a raw request to the datadog api + *params: + * method: | + * http method GET, POST, PUT, DELETE + * path: | + * the api url path e.g. /tags/hosts + * params: | + * an object which allows the keys `query` or `body` + * callback: | + * function to call on success/failure callback(err, result) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.client.request("GET", "/url/path", {}, function(err, results){ + * console.dir(results); + * }); + * ``` + */ +client.prototype.request = function(method, path, params, callback){ + if(arguments.length === 3 && typeof arguments[2] === "function"){ + callback = arguments[2]; + params = {}; + } + + params = params || {}; + var body = (typeof params["body"] === "object")? JSON.stringify(params["body"]) : params["body"]; + var query = { + "api_key": this.api_key, + "application_key": this.app_key, + }; + + if(typeof params["query"] === "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(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ + 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(typeof callback == "function"){ + callback(err, null, res.statusCode); + } + }); + + var data = ""; + res.on("data", function(chunk){ + data += chunk; + }); + + res.on("end", function(){ + var error = null; + try{ data = JSON.parse(data); }catch(e){} + if(data["errors"]){ + error = data["errors"]; + data = null; + } + + if(typeof callback === "function"){ + callback(error, data, res.statusCode); + } + }); + }); + + // This should only occur for errors such as a socket hang up prior to any + // data being received, or SSL-related issues. + req.on("error", function(err){ + if(typeof callback === "function"){ + callback(err, null, 0); + } + }); + + if(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ + req.write(body); + } + req.end() +}; + +module.exports = new client(); diff --git a/lib/http_client.js b/lib/http_client.js deleted file mode 100644 index bfcb8fc..0000000 --- a/lib/http_client.js +++ /dev/null @@ -1,145 +0,0 @@ -var extend = require('extend'); -var https = require('https'); -var url = require('url'); -var util = require('util'); - - -var client = function(options){ - /* - * new http_client([options]) - * - * http client used to make requests to datadog api service - * - * `options` is an optional object of parameters to set: - * { - * 'api_key': 'datadog api key', - * 'app_key': 'datadog app key', - * 'api_version': 'default is v1', - * 'api_host': 'default is app.datadoghq.com', - * } - * - * this client also allows you to provide these options as - * environment variables instead: - * - * api_key: DD_API_KEY - * app_key: DD_APP_KEY - * api_version: DD_API_VERSION - * api_host: DD_API_HOST - */ - options = (options)? options : {}; - - this.api_key = (options['api_key'])? options['api_key'] : process.env['DD_API_KEY']; - if(!this.api_key){ - throw new Error('`api_key` is not present, either provide `api_key` in `options` or use environment variable `DD_API_KEY`'); - } - - this.app_key = (options['app_key'])? options['app_key'] : process.env['DD_APP_KEY']; - if(!this.api_key){ - throw new Error('`app_key` is not present, either provide `app_key` in `options` or use environment variable `DD_APP_KEY`'); - } - - this.api_version = (options['api_version'])? options['api_version'] : process.env['DD_API_VERSION']; - if(!this.api_version){ - this.api_version = 'v1'; - } - - this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST']; - if(!this.api_host){ - this.api_host = 'app.datadoghq.com'; - } - - -}; - -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 && typeof arguments[2] == 'function'){ - callback = arguments[2]; - params = {}; - } - - params = params || {}; - var body = (typeof params['body'] == 'object')? JSON.stringify(params['body']) : params['body']; - var query = { - 'api_key': this.api_key, - 'application_key': this.app_key, - }; - - if(typeof params['query'] == '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(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ - 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(typeof callback == 'function'){ - callback(err, null, res.statusCode); - } - }); - - var data = ''; - res.on('data', function(chunk){ - data += chunk; - }); - - res.on('end', function(){ - var error = null; - try{ data = JSON.parse(data); }catch(e){} - if(data['errors']){ - error = data['errors']; - data = null; - } - - if(typeof callback == 'function'){ - callback(error, data, res.statusCode); - } - }); - }); - - // This should only occur for errors such as a socket hang up prior to any - // data being received, or SSL-related issues. - req.on('error', function(err) { - if(typeof callback == 'function'){ - callback(err, null, 0); - } - }); - - if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ - req.write(body); - } - req.end() -}; - -return module.exports = client;