Skip to content

Commit af1d740

Browse files
authored
Add support for date param in Api.usage() (#210)
1 parent fbdaf1c commit af1d740

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

cloudinary-core/src/main/java/com/cloudinary/Api.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@ public ApiResponse ping(Map options) throws Exception {
5555

5656
public ApiResponse usage(Map options) throws Exception {
5757
if (options == null) options = ObjectUtils.emptyMap();
58-
return callApi(HttpMethod.GET, Arrays.asList("usage"), ObjectUtils.emptyMap(), options);
58+
59+
final List<String> uri = new ArrayList<String>();
60+
uri.add("usage");
61+
62+
Object date = options.get("date");
63+
64+
if (date != null) {
65+
if (date instanceof Date) {
66+
date = ObjectUtils.toUsageApiDateFormat((Date) date);
67+
}
68+
69+
uri.add(date.toString());
70+
}
71+
72+
return callApi(HttpMethod.GET, uri, ObjectUtils.emptyMap(), options);
5973
}
6074

75+
6176
public ApiResponse resourceTypes(Map options) throws Exception {
6277
if (options == null) options = ObjectUtils.emptyMap();
6378
return callApi(HttpMethod.GET, Arrays.asList("resources"), ObjectUtils.emptyMap(), options);
@@ -543,7 +558,8 @@ public ApiResponse updateResourcesAccessModeByTag(String accessMode, String tag,
543558

544559
/**
545560
* Delete a folder (must be empty).
546-
* @param folder The full path of the folder to delete
561+
*
562+
* @param folder The full path of the folder to delete
547563
* @param options additional options.
548564
* @return The operation result.
549565
* @throws Exception When the folder isn't empty or doesn't exist.
@@ -588,6 +604,7 @@ private ApiResponse updateResourcesAccessMode(String accessMode, String byKey, O
588604

589605
/**
590606
* Add a new metadata field definition
607+
*
591608
* @param field The field to add.
592609
* @return A map representing the newly added field.
593610
* @throws Exception
@@ -599,6 +616,7 @@ public ApiResponse addMetadataField(MetadataField field) throws Exception {
599616

600617
/**
601618
* List all the metadata field definitions (structure, not values)
619+
*
602620
* @return A map containing the list of field definitions maps.
603621
* @throws Exception
604622
*/
@@ -608,6 +626,7 @@ public ApiResponse listMetadataFields() throws Exception {
608626

609627
/**
610628
* Get a metadata field definition by id
629+
*
611630
* @param fieldExternalId The id of the field to retrieve
612631
* @return The fields definitions.
613632
* @throws Exception
@@ -618,8 +637,9 @@ public ApiResponse metadataFieldByFieldId(String fieldExternalId) throws Excepti
618637

619638
/**
620639
* Update the definitions of a single metadata field.
640+
*
621641
* @param fieldExternalId The id of the field to update
622-
* @param field The field definition
642+
* @param field The field definition
623643
* @return The updated fields definition.
624644
* @throws Exception
625645
*/
@@ -630,9 +650,10 @@ public ApiResponse updateMetadataField(String fieldExternalId, MetadataField fie
630650

631651
/**
632652
* Update the datasource entries for a given field
653+
*
633654
* @param fieldExternalId The id of the field to update
634-
* @param entries A list of datasource entries. Existing entries (according to entry id) will be updated,
635-
* new entries will be added.
655+
* @param entries A list of datasource entries. Existing entries (according to entry id) will be updated,
656+
* new entries will be added.
636657
* @return The updated field definition.
637658
* @throws Exception
638659
*/
@@ -643,7 +664,8 @@ public ApiResponse updateMetadataFieldDatasource(String fieldExternalId, List<Me
643664

644665
/**
645666
* Delete data source entries for a given field
646-
* @param fieldExternalId The id of the field to update
667+
*
668+
* @param fieldExternalId The id of the field to update
647669
* @param entriesExternalId The ids of all the entries to delete from the data source
648670
* @return The remaining datasource entries.
649671
* @throws Exception
@@ -655,7 +677,8 @@ public ApiResponse deleteDatasourceEntries(String fieldExternalId, List<String>
655677

656678
/**
657679
* Restore deleted data source entries for a given field
658-
* @param fieldExternalId The id of the field to operate
680+
*
681+
* @param fieldExternalId The id of the field to operate
659682
* @param entriesExternalId The ids of all the entries to restore from the data source
660683
* @return The datasource entries state after restore
661684
* @throws Exception
@@ -667,6 +690,7 @@ public ApiResponse restoreDatasourceEntries(String fieldExternalId, List<String>
667690

668691
/**
669692
* Delete a field definition.
693+
*
670694
* @param fieldExternalId The id of the field to delete
671695
* @return A map with a "message" key. "ok" value indicates a successful deletion.
672696
* @throws Exception

cloudinary-core/src/main/java/com/cloudinary/utils/ObjectUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public static Long asLong(Object value, Long defaultValue) {
215215
}
216216
}
217217

218+
public static String toUsageApiDateFormat(Date date){
219+
return new SimpleDateFormat("dd-MM-yyy").format(date);
220+
}
221+
218222
public static String toISO8601DateOnly(Date date) {
219223
return new SimpleDateFormat("yyyy-MM-dd").format(date);
220224
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,20 @@ public void test20ResourcesContext() throws Exception {
512512
@Test
513513
public void test18Usage() throws Exception {
514514
// should support usage API call
515-
Map result = api.usage(ObjectUtils.emptyMap());
515+
final Date yesterday = yesterday();
516+
517+
Map result = api.usage(ObjectUtils.asMap("date", yesterday));
518+
assertNotNull(result.get("last_updated"));
519+
520+
result = api.usage(ObjectUtils.asMap("date", ObjectUtils.toUsageApiDateFormat(yesterday)));
516521
assertNotNull(result.get("last_updated"));
522+
523+
result = api.usage(ObjectUtils.emptyMap());
524+
assertNotNull(result.get("last_updated"));
525+
}
526+
527+
private Date yesterday() {
528+
return new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
517529
}
518530

519531
@Test

0 commit comments

Comments
 (0)