Skip to content

Commit 659249e

Browse files
michalkcloudinaynitzanj
authored andcommitted
Support create folder API (#188)
1 parent 358bfbf commit 659249e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,15 @@ public ApiResponse updateUploadPreset(String name, Map options) throws Exception
244244
if (options == null) options = ObjectUtils.emptyMap();
245245
Map params = Util.buildUploadParams(options);
246246
Util.clearEmpty(params);
247-
params.putAll(ObjectUtils.only(options, "unsigned", "disallow_public_id","live"));
247+
params.putAll(ObjectUtils.only(options, "unsigned", "disallow_public_id", "live"));
248248
return callApi(HttpMethod.PUT, Arrays.asList("upload_presets", name), params, options);
249249
}
250250

251251
public ApiResponse createUploadPreset(Map options) throws Exception {
252252
if (options == null) options = ObjectUtils.emptyMap();
253253
Map params = Util.buildUploadParams(options);
254254
Util.clearEmpty(params);
255-
params.putAll(ObjectUtils.only(options, "name", "unsigned", "disallow_public_id","live"));
255+
params.putAll(ObjectUtils.only(options, "name", "unsigned", "disallow_public_id", "live"));
256256
return callApi(HttpMethod.POST, Arrays.asList("upload_presets"), params, options);
257257
}
258258

@@ -268,6 +268,13 @@ public ApiResponse subFolders(String ofFolderPath, Map options) throws Exception
268268
return callApi(HttpMethod.GET, Arrays.asList("folders", ofFolderPath), ObjectUtils.emptyMap(), options);
269269
}
270270

271+
//Creates an empty folder
272+
public ApiResponse createFolder(String folderName, Map options) throws Exception {
273+
if (options == null)
274+
options = ObjectUtils.emptyMap();
275+
return callApi(HttpMethod.POST, Arrays.asList("folders", folderName), ObjectUtils.emptyMap(), options);
276+
}
277+
271278
public ApiResponse restore(Iterable<String> publicIds, Map options) throws Exception {
272279
if (options == null)
273280
options = ObjectUtils.emptyMap();
@@ -583,7 +590,7 @@ private ApiResponse updateResourcesAccessMode(String accessMode, String byKey, O
583590
*/
584591
public ApiResponse addMetadataField(MetadataField field) throws Exception {
585592
return callApi(HttpMethod.POST, Collections.singletonList("metadata_fields"),
586-
ObjectUtils.toMap(field), ObjectUtils.asMap ("content_type", "json"));
593+
ObjectUtils.toMap(field), ObjectUtils.asMap("content_type", "json"));
587594
}
588595

589596
/**
@@ -639,7 +646,7 @@ public ApiResponse updateMetadataFieldDatasource(String fieldExternalId, List<Me
639646
*/
640647
public ApiResponse deleteDatasourceEntries(String fieldExternalId, List<String> entriesExternalId) throws Exception {
641648
List<String> uri = Arrays.asList("metadata_fields", fieldExternalId, "datasource");
642-
return callApi(HttpMethod.DELETE, uri,Collections.singletonMap("external_ids", entriesExternalId) , Collections.emptyMap());
649+
return callApi(HttpMethod.DELETE, uri, Collections.singletonMap("external_ids", entriesExternalId), Collections.emptyMap());
643650
}
644651

645652
/**

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ abstract public class AbstractApiTest extends MockableTest {
4646
public static final Transformation DELETE_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX + "_delete").fontFamily("Arial").fontSize(60));
4747
public static final String TEST_KEY = "test-key" + SUFFIX;
4848
public static final String API_TEST_RESTORE = "api_test_restore" + SUFFIX;
49+
public static final Set<String> createdFolders = new HashSet<String>();
4950

5051
protected Api api;
5152

@@ -119,6 +120,12 @@ public static void tearDownClass() {
119120
api.deleteUploadPreset(API_TEST_UPLOAD_PRESET_4, ObjectUtils.emptyMap());
120121
} catch (Exception ignored) {
121122
}
123+
try {
124+
for (String folder : createdFolders) {
125+
api.deleteFolder(folder, ObjectUtils.emptyMap());
126+
}
127+
} catch (Exception ignored) {
128+
}
122129

123130
}
124131

@@ -484,7 +491,8 @@ public void testListTransformationByNamed() throws Exception {
484491
} finally {
485492
try {
486493
api.deleteTransformation(name, null);
487-
} catch (Exception ignored){}
494+
} catch (Exception ignored) {
495+
}
488496
}
489497
}
490498

@@ -653,7 +661,7 @@ public void testGetUploadPreset() throws Exception {
653661
String[] tags = {"a", "b", "c"};
654662
Map context = ObjectUtils.asMap("a", "b", "c", "d");
655663
Map result = api.createUploadPreset(ObjectUtils.asMap("unsigned", true, "folder", "folder", "transformation", EXPLICIT_TRANSFORMATION, "tags", tags, "context",
656-
context,"live",true));
664+
context, "live", true));
657665
String name = result.get("name").toString();
658666
Map preset = api.uploadPreset(name, ObjectUtils.emptyMap());
659667
assertEquals(preset.get("name"), name);
@@ -693,7 +701,7 @@ public void testUpdateUploadPreset() throws Exception {
693701
String name = api.createUploadPreset(ObjectUtils.asMap("folder", "folder")).get("name").toString();
694702
Map preset = api.uploadPreset(name, ObjectUtils.emptyMap());
695703
Map settings = (Map) preset.get("settings");
696-
settings.putAll(ObjectUtils.asMap("colors", true, "unsigned", true, "disallow_public_id", true,"live",true));
704+
settings.putAll(ObjectUtils.asMap("colors", true, "unsigned", true, "disallow_public_id", true, "live", true));
697705
api.updateUploadPreset(name, settings);
698706
settings.remove("unsigned");
699707
preset = api.uploadPreset(name, ObjectUtils.emptyMap());
@@ -758,6 +766,14 @@ public void testFolderApi() throws Exception {
758766
api.deleteResourcesByPrefix("test_folder", ObjectUtils.emptyMap());
759767
}
760768

769+
@Test
770+
public void testCreateFolder() throws Exception {
771+
String apTestCreateFolder = "api_test_create_folder" + "_" + SUFFIX;
772+
createdFolders.add(apTestCreateFolder);
773+
Map result = api.createFolder("apTestCreateFolder", null);
774+
assertTrue((Boolean) result.get("success"));
775+
}
776+
761777
@Test
762778
public void testRestore() throws Exception {
763779
// should support restoring resources
@@ -946,7 +962,7 @@ public void testDeleteFolder() throws Exception {
946962
Thread.sleep(5000);
947963
api.deleteResources(Collections.singletonList(uploadResult.get("public_id").toString()), emptyMap());
948964
ApiResponse result = api.deleteFolder(toDelete, emptyMap());
949-
assertTrue(((ArrayList)result.get("deleted")).contains(toDelete));
965+
assertTrue(((ArrayList) result.get("deleted")).contains(toDelete));
950966

951967
// should throw exception (folder not found):
952968
api.deleteFolder(cloudinary.randomPublicId(), emptyMap());
@@ -958,4 +974,4 @@ public void testCinemagraphAnalysisResource() throws Exception {
958974
ApiResponse res = api.resource(API_TEST, Collections.singletonMap("cinemagraph_analysis", true));
959975
assertNotNull(res.get("cinemagraph_analysis"));
960976
}
961-
}
977+
}

0 commit comments

Comments
 (0)