Browse Source

handle optional parameters and URI encoding values

dev/test.graph.create
Brett Langdon 10 years ago
parent
commit
b2c77f1cf6
2 changed files with 56 additions and 22 deletions
  1. +31
    -20
      lib/api/graph.js
  2. +25
    -2
      test/api/graph.js

+ 31
- 20
lib/api/graph.js View File

@ -62,37 +62,48 @@ function snapshot(query, from, to, eventQuery, callback){
* }; * };
* dogapi.initialize(options); * dogapi.initialize(options);
* var query = "system.cpu.idle{*}"; * var query = "system.cpu.idle{*}";
* var graph_json = {
* "viz": "timeseries",
* "requests": [
* var graphJSON = {
* viz: "timeseries",
* requests: [
* { * {
* "q": query,
* "aggregator": "avg",
* "conditional_formats": [],
* "type": "area"
* q: query,
* aggregator: "avg",
* conditional_formats: [],
* type: "area"
* } * }
* ] * ]
* } * }
* var timeframe = '1_hour';
* var size = 'xlarge';
* var legend = 'yes';
* var title = 'my awesome embed';
* dogapi.graph.createEmbed(graph_json, timeframe, size, legend, title, function(err, res){
* var timeframe = "1_hour";
* var size = "xlarge";
* var legend = "yes";
* var title = "my awesome embed";
* dogapi.graph.createEmbed(graphJSON, timeframe, size, legend, title, function(err, res){
* console.dir(res); * console.dir(res);
* }); * });
* ``` * ```
*/ */
function createEmbed(graph_json, timeframe, size, legend, title, callback){
function createEmbed(graphJSON, timeframe, size, legend, title, callback){
var lastArgument = arguments.length -1; var lastArgument = arguments.length -1;
// Build the form body
var body = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON));
if (timeframe !== undefined) {
body += "&timeframe=" + encodeURIComponent(timeframe);
}
if (size !== undefined) {
body += "&size=" + encodeURIComponent(size);
}
if (legend !== undefined) {
body += "&legend=" + encodeURIComponent(legend);
}
if (title !== undefined) {
body += "&title=" + encodeURIComponent(title);
}
// Create the request
var params = { var params = {
body: 'graph_json=' + JSON.stringify(graph_json) +
'&timeframe=' + timeframe +
'&size=' + size +
'&legend=' + legend +
'&title=' + title
,
contentType: 'application/x-www-form-urlencoded'
body: body,
contentType: "application/x-www-form-urlencoded"
}; };
client.request("POST", "/graph/embed", params, callback); client.request("POST", "/graph/embed", params, callback);


+ 25
- 2
test/api/graph.js View File

@ -38,10 +38,33 @@ describe("api/graph", function(){
// Properly formatted body and content-type // Properly formatted body and content-type
var params = call_args[2]; var params = call_args[2];
var expectedBody = "graph_json=" + JSON.stringify(graphJSON) +
"&timeframe=1_hour&size=large&legend=yes&title=test graph embed";
var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON)) +
"&timeframe=1_hour&size=large&legend=yes&title=test%20graph%20embed";
assert.equal(params.body, expectedBody); assert.equal(params.body, expectedBody);
assert(params.contentType, "application/x-form-urlencoded"); assert(params.contentType, "application/x-form-urlencoded");
}); });
it("should only require graph_json", function(){
var graphJSON = {
viz: "timeseries",
requests: [
{
q: "system.cpu.idle{*}"
}
]
};
// Make our api call
graph.createEmbed(graphJSON);
// Assert we properly called `client.request`
assert(stub_request.calledOnce);
var call_args = stub_request.getCall(0).args;
// Properly formatted body
var params = call_args[2];
var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON));
assert.equal(params.body, expectedBody);
});
}); });
}); });

Loading…
Cancel
Save