Skip to content

Commit c60af98

Browse files
itaibenariAmir Tocker
authored andcommitted
unstub access mode support
1 parent 725b875 commit c60af98

File tree

3 files changed

+96
-80
lines changed

3 files changed

+96
-80
lines changed

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

Lines changed: 50 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public ApiResponse resources(Map options) throws Exception {
7272
uri.add(type);
7373

7474
ApiResponse response = callApi(HttpMethod.GET, uri, ObjectUtils.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations", "start_at"), options);
75-
// stubbing default access mode to response , will be removed in final version
76-
addAccessModeToResponse(response,"public");
7775
return response;
7876
}
7977

@@ -82,8 +80,6 @@ public ApiResponse resourcesByTag(String tag, Map options) throws Exception {
8280
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
8381

8482
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
85-
// stubbing default access mode to response , will be removed in final version
86-
addAccessModeToResponse(response,"public");
8783
return response;
8884
}
8985

@@ -109,8 +105,6 @@ public ApiResponse resourcesByIds(Iterable<String> publicIds, Map options) throw
109105
Map params = ObjectUtils.only(options, "tags", "context", "moderations");
110106
params.put("public_ids", publicIds);
111107
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type), params, options);
112-
// stubbing default access mode to response , will be removed in final version
113-
addAccessModeToResponse(response,"public");
114108
return response;
115109
}
116110

@@ -119,8 +113,6 @@ public ApiResponse resourcesByModeration(String kind, String status, Map options
119113
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
120114

121115
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
122-
// stubbing default access mode to response , will be removed in final version
123-
addAccessModeToResponse(response,"public");
124116
return response;
125117
}
126118

@@ -133,9 +125,6 @@ public ApiResponse resource(String public_id, Map options) throws Exception {
133125
ObjectUtils.only(options, "exif", "colors", "faces", "coordinates",
134126
"image_metadata", "pages", "phash", "max_results"), options);
135127

136-
137-
// stubbing default access mode to response , will be removed in final version
138-
addAccessModeToResponse(response,"public");
139128
return response;
140129
}
141130

@@ -148,8 +137,6 @@ public ApiResponse update(String public_id, Map options) throws Exception {
148137
params.put("moderation_status", options.get("moderation_status"));
149138
ApiResponse response = callApi(HttpMethod.POST, Arrays.asList("resources", resourceType, type, public_id),
150139
params, options);
151-
// stubbing default access mode to response , will be removed in final version
152-
addAccessModeToResponse(response,"public");
153140
return response;
154141
}
155142

@@ -475,81 +462,64 @@ public ApiResponse updateStreamingProfile(String name, String displayName, List<
475462
return callApi(HttpMethod.PUT, uri, params, options);
476463
}
477464

478-
/* Update access mode method stubs */
479-
public ApiResponse updateResourcesAccessModeByIds(String accessMode, List<String> ids, Map options) throws Exception {
480-
if (options == null) options = ObjectUtils.asMap();
481-
options.put("max_results",100);
482-
483-
ApiResponse response = this.resourcesByIds(ids,options);
484-
485-
// stubbing default access mode to response , will be removed in final version
486-
addAccessModeToResponse(response,accessMode);
487-
response.put("updated",response.get("resources"));
488-
response.remove("resources");
489-
response.put("failed" ,Arrays.asList());
490-
491-
return response;
492-
}
493-
494-
public ApiResponse updateResourcesAccessModeByTag(String accessMode, String tag,Map options) throws Exception {
495-
if (options == null) options = ObjectUtils.asMap();
496-
options.put("max_results",100);
497-
ApiResponse response = this.resourcesByTag(tag,options);
498-
addAccessModeToResponse(response,accessMode);
499-
response.put("updated",response.get("resources"));
500-
response.remove("resources");
501-
response.put("failed" ,Arrays.asList());
502-
return response;
503-
}
504-
505-
public ApiResponse updateResourcesAccessModeByPrefix(String accessMode, String prefix, Map options) throws Exception{
506-
if (options == null) options = ObjectUtils.asMap();
507-
options.put("prefix",prefix);
508-
options.put("max_results",100);
509-
ApiResponse response = this.resources(options);
510-
addAccessModeToResponse(response,accessMode);
511-
response.put("updated",response.get("resources"));
512-
response.remove("resources");
513-
response.put("failed" ,Arrays.asList());
514-
return response;
515-
}
516-
/* Update access mode method stubs */
517-
518-
/* Temporary access mode helper methods */
519-
520-
private void addAccessModeToCollection(Object collection, String accessMode){
521-
List<Object> collectionList = (List) collection;
522-
if (collectionList==null) {
523-
throw new Error("no collection found");
524-
525-
}
526-
for (Object listItem :collectionList){
527-
this.addAccessModeToResource(listItem, accessMode);
528-
}
465+
/**
466+
* @see Api#updateStreamingProfile(String, String, List, Map)
467+
*/
468+
public ApiResponse updateStreamingProfile(String name, String displayName, List<Map> representations) throws Exception {
469+
return createStreamingProfile(name, displayName, representations);
529470
}
530471

531-
private void addAccessModeToResponse(ApiResponse response, String accessMode){
532-
if (response.keySet().contains("resources")){
533-
addAccessModeToCollection(response.get("resources"),accessMode);
534-
}else if (response.keySet().contains("public_id")){
535-
addAccessModeToResource(response,accessMode);
536-
}else {
537-
throw new Error("unidentified response type (keys: "+StringUtils.join(response.keySet(),",")+")");
538-
}
472+
/**
473+
* Update access mode of one or more resources by prefix
474+
*
475+
* @param accessMode The new access mode, "public" or "authenticated"
476+
* @param prefix The prefix by which to filter applicable resources
477+
* @param options
478+
* @return
479+
* @throws Exception
480+
*/
481+
public ApiResponse updateResourcesAccessModeByPrefix(String accessMode, String prefix, Map options) throws Exception {
482+
return updateResourcesAccessMode(accessMode, "prefix", prefix, options);
539483
}
540484

541-
private void addAccessModeToResource(Object resource, String accessMode){
542-
Map<Object,Object> resourceMap = (Map<Object,Object>) resource;
543-
if (resourceMap==null) { return ;}
544-
resourceMap.put("access_mode",accessMode);
485+
/**
486+
* Update access mode of one or more resources by tag
487+
*
488+
* @param accessMode The new access mode, "public" or "authenticated"
489+
* @param tag The tag by which to filter applicable resources
490+
* @param options
491+
* @return
492+
* @throws Exception
493+
*/
494+
public ApiResponse updateResourcesAccessModeByTag(String accessMode, String tag, Map options) throws Exception {
495+
return updateResourcesAccessMode(accessMode, "tag", tag, options);
545496
}
546-
/* temporary access_mode stubs */
547497

548498
/**
549-
* @see Api#updateStreamingProfile(String, String, List, Map)
499+
* Update access mode of one or more resources by publicIds
500+
*
501+
* @param accessMode The new access mode, "public" or "authenticated"
502+
* @param publicIds A list of public ids of resources to be updated
503+
* @param options
504+
* @return
505+
* @throws Exception
550506
*/
551-
public ApiResponse updateStreamingProfile(String name, String displayName, List<Map> representations) throws Exception {
552-
return createStreamingProfile(name, displayName, representations);
507+
public ApiResponse updateResourcesAccessModeByIds(String accessMode, Iterable<String> publicIds, Map options) throws Exception {
508+
return updateResourcesAccessMode(accessMode, "public_ids", publicIds, options);
509+
}
510+
511+
private ApiResponse updateResourcesAccessMode(String accessMode, String byKey, Object value, Map options) throws Exception {
512+
if (options == null) options = ObjectUtils.emptyMap();
513+
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
514+
List<String> uri = new ArrayList<String>();
515+
uri.add("resources");
516+
uri.add(resourceType);
517+
uri.add("upload");
518+
uri.add("update_access_mode");
519+
Map params = new HashMap<String, Object>();
520+
params.put("access_mode", accessMode);
521+
params.put(byKey, value);
522+
return callApi(HttpMethod.POST, uri, params, options);
553523
}
554524

555525
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static final Map<String, Object> buildUploadParams(Map options) {
3030
params.put("folder", (String) options.get("folder"));
3131
params.put("allowed_formats", StringUtils.join(ObjectUtils.asArray(options.get("allowed_formats")), ","));
3232
params.put("moderation", options.get("moderation"));
33+
params.put("access_mode", (String) options.get("access_mode"));
3334
Object responsive_breakpoints = options.get("responsive_breakpoints");
3435
if (responsive_breakpoints != null) {
3536
params.put("responsive_breakpoints", JSONObject.wrap(responsive_breakpoints));

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,49 @@ public void testPublishByTag() throws Exception {
749749
assertNotNull(resource.get("url"));
750750
cloudinary.uploader().destroy(publicId, null);
751751
}
752+
753+
@Test
754+
public void testUpdateResourcesAccessModeByIds() throws Exception {
755+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "access_mode", "authenticated"));
756+
String publicId = (String) response.get("public_id");
757+
assertEquals(response.get("access_mode"), "authenticated");
758+
response = cloudinary.api().updateResourcesAccessModeByIds("public", Arrays.asList(publicId), null);
759+
List updated = (List) response.get("updated");
760+
assertNotNull(updated);
761+
assertEquals(updated.size(), 1);
762+
Map resource = (Map) updated.get(0);
763+
assertEquals(resource.get("public_id"), publicId);
764+
assertEquals(resource.get("access_mode"), "public");
765+
cloudinary.uploader().destroy(publicId, null);
766+
}
767+
768+
@Test
769+
public void testUpdateResourcesAccessModeByPrefix() throws Exception {
770+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "access_mode", "authenticated"));
771+
String publicId = (String) response.get("public_id");
772+
assertEquals(response.get("access_mode"), "authenticated");
773+
response = cloudinary.api().updateResourcesAccessModeByPrefix("public", publicId.substring(0, publicId.length() - 2), null);
774+
List updated = (List) response.get("updated");
775+
assertNotNull(updated);
776+
assertEquals(updated.size(), 1);
777+
Map resource = (Map) updated.get(0);
778+
assertEquals(resource.get("public_id"), publicId);
779+
assertEquals(resource.get("access_mode"), "public");
780+
cloudinary.uploader().destroy(publicId, null);
781+
}
782+
783+
@Test
784+
public void testUpdateResourcesAccessModeByTag() throws Exception {
785+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", Arrays.asList(uniqueTag, uniqueTag + "2"), "access_mode", "authenticated"));
786+
String publicId = (String) response.get("public_id");
787+
assertEquals(response.get("access_mode"), "authenticated");
788+
response = cloudinary.api().updateResourcesAccessModeByTag("public", uniqueTag + "2", null);
789+
List updated = (List) response.get("updated");
790+
assertNotNull(updated);
791+
assertEquals(updated.size(), 1);
792+
Map resource = (Map) updated.get(0);
793+
assertEquals(resource.get("public_id"), publicId);
794+
assertEquals(resource.get("access_mode"), "public");
795+
cloudinary.uploader().destroy(publicId, null);
796+
}
752797
}

0 commit comments

Comments
 (0)