Skip to content

Commit a80eeb1

Browse files
author
RTLcoil
authored
Add support for date parameter in usage API (#467)
1 parent 1dea1ea commit a80eeb1

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

lib-es5/api.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ exports.ping = function ping(callback) {
2424
exports.usage = function usage(callback) {
2525
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2626

27-
return call_api("get", ["usage"], {}, callback, options);
27+
var uri = ["usage"];
28+
29+
if (options.date) {
30+
uri.push(options.date);
31+
}
32+
33+
return call_api("get", uri, {}, callback, options);
2834
};
2935

3036
exports.resource_types = function resource_types(callback) {

lib/api.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ exports.ping = function ping(callback, options = {}) {
1414
};
1515

1616
exports.usage = function usage(callback, options = {}) {
17-
return call_api("get", ["usage"], {}, callback, options);
17+
const uri = ["usage"];
18+
19+
if (options.date) {
20+
uri.push(options.date);
21+
}
22+
23+
return call_api("get", uri, {}, callback, options);
1824
};
1925

2026
exports.resource_types = function resource_types(callback, options = {}) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"babel-preset-stage-0": "^6.24.1",
2929
"babel-register": "^6.26.0",
3030
"babel-runtime": "^6.26.0",
31+
"date-fns": "^2.16.1",
3132
"dotenv": "4.x",
3233
"dtslint": "^0.9.1",
3334
"eslint": "^6.8.0",

test/integration/api/admin/api_spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const sinon = require('sinon');
2+
const formatDate = require("date-fns").format;
3+
const subDate = require("date-fns").sub;
24
const ClientRequest = require('_http_client').ClientRequest;
35
const Q = require('q');
46
const cloudinary = require("../../../../cloudinary");
@@ -696,7 +698,20 @@ describe("api", function () {
696698
it("should support the usage API call", function () {
697699
this.timeout(TIMEOUT.MEDIUM);
698700
return cloudinary.v2.api.usage()
699-
.then(usage => expect(usage.last_update).not.to.eql(null));
701+
.then(usage => {
702+
expect(usage).to.be.an("object");
703+
expect(usage).to.have.keys("plan", "last_updated", "transformations", "objects", "bandwidth", "storage", "requests", "resources", "derived_resources", "media_limits");
704+
});
705+
});
706+
it("should return usage values for a specific date", function () {
707+
const yesterday = formatDate(subDate(new Date(), { days: 1 }), "dd-MM-yyyy");
708+
return cloudinary.v2.api.usage({ date: yesterday })
709+
.then(usage => {
710+
expect(usage).to.be.an("object");
711+
expect(usage).to.have.keys("plan", "last_updated", "transformations", "objects", "bandwidth", "storage", "requests", "resources", "derived_resources", "media_limits");
712+
expect(usage.bandwidth).to.be.an("object");
713+
expect(usage.bandwidth).to.not.have.keys("limit", "used_percent");
714+
});
700715
});
701716
describe("delete_all_resources", function () {
702717
callReusableTest("accepts next_cursor", cloudinary.v2.api.delete_all_resources);

0 commit comments

Comments
 (0)