Skip to content

Commit ac5ab2d

Browse files
authored
Add tags as an array (#252)
* Add addTag and removeTag to receive Strings array * Add add tag function to recieve multiple tags * Add test * Add replace tag as array * Fix addTag, removeTag, replaceTag * Fix comment
1 parent 709f4a9 commit ac5ab2d

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

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

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,40 +323,133 @@ public Map explode(String public_id, Map options) throws IOException {
323323
return callApi("explode", params, options, null);
324324
}
325325

326-
// options may include 'exclusive' (boolean) which causes clearing this tag
327-
// from all other resources
326+
/**
327+
* Add a tag to one or more assets in your cloud.
328+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
329+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
330+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
331+
* @param tag - The tag to assign.
332+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
333+
* @param options - An object holding the available parameters for the request.
334+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
335+
* @return A map with the public ids returned from the server
336+
* @throws IOException
337+
*/
328338
public Map addTag(String tag, String[] publicIds, Map options) throws IOException {
339+
return addTag(new String[]{tag}, publicIds, options);
340+
}
341+
342+
/**
343+
* Add a tag to one or more assets in your cloud.
344+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
345+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
346+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
347+
* @param tag - An array of tags to assign.
348+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
349+
* @param options - An object holding the available parameters for the request.
350+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
351+
* @return A map with the public ids returned from the server.
352+
* @throws IOException
353+
*/
354+
public Map addTag(String[] tag, String[] publicIds, Map options) throws IOException {
329355
if (options == null)
330356
options = ObjectUtils.emptyMap();
331357
boolean exclusive = ObjectUtils.asBoolean(options.get("exclusive"), false);
332358
String command = exclusive ? "set_exclusive" : Command.add;
333359
return callTagsApi(tag, command, publicIds, options);
334360
}
335361

362+
/**
363+
* Remove a tag to one or more assets in your cloud.
364+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
365+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
366+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
367+
* @param tag - The tag to remove.
368+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
369+
* @param options - An object holding the available parameters for the request.
370+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
371+
* @return - A map with the public ids returned from the server.
372+
* @throws IOException
373+
*/
336374
public Map removeTag(String tag, String[] publicIds, Map options) throws IOException {
375+
return removeTag(new String[]{tag}, publicIds, options);
376+
}
377+
378+
/**
379+
* Remove tags to one or more assets in your cloud.
380+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
381+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
382+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
383+
* @param tag - The array of tags to remove.
384+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
385+
* @param options - An object holding the available parameters for the request.
386+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
387+
* @return - * @return - A map with the public ids returned from the server.
388+
* @throws IOException
389+
*/
390+
public Map removeTag(String[] tag, String[] publicIds, Map options) throws IOException {
337391
if (options == null)
338392
options = ObjectUtils.emptyMap();
339393
return callTagsApi(tag, Command.remove, publicIds, options);
340394
}
341395

396+
/**
397+
* Remove an array of tags to one or more assets in your cloud.
398+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
399+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
400+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
401+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
402+
* @param options - An object holding the available parameters for the request.
403+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
404+
* @return - * @return - A map with the public ids returned from the server.
405+
* @throws IOException
406+
*/
342407
public Map removeAllTags(String[] publicIds, Map options) throws IOException {
343408
if (options == null)
344409
options = ObjectUtils.emptyMap();
345410
return callTagsApi(null, Command.removeAll, publicIds, options);
346411
}
347412

413+
/**
414+
* Replaces a tag to one or more assets in your cloud.
415+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
416+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
417+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
418+
* @param tag - The tag to replace.
419+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
420+
* @param options - An object holding the available options for the request.
421+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
422+
* @return - A map with the public ids returned from the server.
423+
* @throws IOException
424+
*/
348425
public Map replaceTag(String tag, String[] publicIds, Map options) throws IOException {
426+
return replaceTag(new String[]{tag}, publicIds, options);
427+
}
428+
429+
/**
430+
* Replaces tags to one or more assets in your cloud.
431+
* Tags are used to categorize and organize your images, and can also be used to apply group actions to images,
432+
* for example to delete images, create sprites, ZIP files, JSON lists, or animated GIFs.
433+
* Each image can be assigned one or more tags, which is a short name that you can dynamically use (no need to predefine tags).
434+
* @param tag - An array of tag to replace.
435+
* @param publicIds - An array of Public IDs of images uploaded to Cloudinary.
436+
* @param options - An object holding the available options for the request.
437+
* options may include 'exclusive' (boolean) which causes clearing this tag from all other resources
438+
* @return - A map with the public ids returned from the server.
439+
* @throws IOException
440+
*/
441+
public Map replaceTag(String[] tag, String[] publicIds, Map options) throws IOException {
349442
if (options == null)
350443
options = ObjectUtils.emptyMap();
351444
return callTagsApi(tag, Command.replace, publicIds, options);
352445
}
353446

354-
public Map callTagsApi(String tag, String command, String[] publicIds, Map options) throws IOException {
447+
public Map callTagsApi(String[] tag, String command, String[] publicIds, Map options) throws IOException {
355448
if (options == null)
356449
options = ObjectUtils.emptyMap();
357450
Map<String, Object> params = new HashMap<String, Object>();
358451
if (tag != null) {
359-
params.put("tag", tag);
452+
params.put("tag", StringUtils.join(tag, ","));
360453
}
361454
params.put("command", command);
362455
params.put("type", (String) options.get("type"));

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,35 @@ public void testTags() throws Exception {
342342
Map result2 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.emptyMap());
343343
String public_id2 = (String) result2.get("public_id");
344344
addToDeleteList("upload", public_id2);
345+
346+
//Test add tags
345347
cloudinary.uploader().addTag("tag1", new String[]{public_id, public_id2}, ObjectUtils.emptyMap());
346348
cloudinary.uploader().addTag("tag2", new String[]{public_id}, ObjectUtils.emptyMap());
349+
cloudinary.uploader().addTag(new String[]{"tag4","tag5"}, new String[]{public_id}, ObjectUtils.emptyMap());
347350
List<String> tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
348-
assertEquals(tags, asArray(new String[]{"tag1", "tag2"}));
351+
assertEquals(tags, asArray(new String[]{"tag1", "tag2", "tag4", "tag5"}));
349352
tags = (List<String>) cloudinary.api().resource(public_id2, ObjectUtils.emptyMap()).get("tags");
350353
assertEquals(tags, asArray(new String[]{"tag1"}));
354+
355+
//Test remove tags
351356
cloudinary.uploader().removeTag("tag1", new String[]{public_id}, ObjectUtils.emptyMap());
352357
tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
358+
assertEquals(tags, asArray(new String[]{"tag2", "tag4", "tag5"}));
359+
cloudinary.uploader().removeTag(new String[]{"tag4", "tag5"}, new String[]{public_id}, ObjectUtils.emptyMap());
360+
tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
353361
assertEquals(tags, asArray(new String[]{"tag2"}));
362+
363+
//Test replace tags
354364
cloudinary.uploader().replaceTag("tag3", new String[]{public_id}, ObjectUtils.emptyMap());
355365
tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
356366
assertEquals(tags, asArray(new String[]{"tag3"}));
367+
cloudinary.uploader().replaceTag(new String[]{"tag6", "tag7"}, new String[]{public_id}, ObjectUtils.emptyMap());
368+
tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
369+
assertEquals(tags, asArray(new String[]{"tag6", "tag7"}));
370+
371+
//Test remove all tags
357372
result = cloudinary.uploader().removeAllTags(new String[]{public_id, public_id2, "noSuchId"}, ObjectUtils.emptyMap());
358373
List<String> publicIds = (List<String>) result.get("public_ids");
359-
360374
assertThat(publicIds, containsInAnyOrder(public_id, public_id2)); // = and not containing "noSuchId"
361375
result = cloudinary.api().resource(public_id, ObjectUtils.emptyMap());
362376
assertThat((Map<? extends String, ?>) result, not(hasKey("tags")));

0 commit comments

Comments
 (0)