Skip to content

Commit 0a2192c

Browse files
daniel cohenAmir Tocker
authored andcommitted
access_mode api stubs
1 parent d8c8c25 commit 0a2192c

File tree

1 file changed

+101
-8
lines changed
  • cloudinary-core/src/main/java/com/cloudinary

1 file changed

+101
-8
lines changed

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

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.cloudinary;
22

33
import java.util.*;
4-
import java.util.concurrent.ExecutionException;
54

65
import com.cloudinary.api.ApiResponse;
76
import com.cloudinary.api.AuthorizationRequired;
@@ -13,6 +12,7 @@
1312
import com.cloudinary.api.exceptions.RateLimited;
1413
import com.cloudinary.strategies.AbstractApiStrategy;
1514
import com.cloudinary.utils.ObjectUtils;
15+
import com.cloudinary.utils.StringUtils;
1616
import org.cloudinary.json.JSONArray;
1717
import com.cloudinary.utils.StringUtils;
1818

@@ -70,13 +70,19 @@ public ApiResponse resources(Map options) throws Exception {
7070
uri.add(resourceType);
7171
if (type != null)
7272
uri.add(type);
73-
return callApi(HttpMethod.GET, uri, ObjectUtils.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations", "start_at"), options);
73+
74+
ApiResponse response = callApi(HttpMethod.GET, uri, ObjectUtils.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations", "start_at"), options);
75+
addAccessModeToResponse(response,"public");
76+
return response;
7477
}
7578

7679
public ApiResponse resourcesByTag(String tag, Map options) throws Exception {
7780
if (options == null) options = ObjectUtils.emptyMap();
7881
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
79-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
82+
83+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
84+
addAccessModeToResponse(response,"public");
85+
return response;
8086
}
8187

8288
public ApiResponse resourcesByContext(String key, Map options) throws Exception {
@@ -100,22 +106,32 @@ public ApiResponse resourcesByIds(Iterable<String> publicIds, Map options) throw
100106
String type = ObjectUtils.asString(options.get("type"), "upload");
101107
Map params = ObjectUtils.only(options, "tags", "context", "moderations");
102108
params.put("public_ids", publicIds);
103-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type), params, options);
109+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type), params, options);
110+
addAccessModeToResponse(response,"public");
111+
return response;
104112
}
105113

106114
public ApiResponse resourcesByModeration(String kind, String status, Map options) throws Exception {
107115
if (options == null) options = ObjectUtils.emptyMap();
108116
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
109-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
117+
118+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
119+
addAccessModeToResponse(response,"public");
120+
return response;
110121
}
111122

112123
public ApiResponse resource(String public_id, Map options) throws Exception {
113124
if (options == null) options = ObjectUtils.emptyMap();
114125
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
115126
String type = ObjectUtils.asString(options.get("type"), "upload");
116-
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type, public_id),
127+
128+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, type, public_id),
117129
ObjectUtils.only(options, "exif", "colors", "faces", "coordinates",
118130
"image_metadata", "pages", "phash", "max_results"), options);
131+
132+
133+
addAccessModeToResponse(response,"public");
134+
return response;
119135
}
120136

121137
public ApiResponse update(String public_id, Map options) throws Exception {
@@ -125,8 +141,10 @@ public ApiResponse update(String public_id, Map options) throws Exception {
125141
Map params = new HashMap<String, Object>();
126142
Util.processWriteParameters(options, params);
127143
params.put("moderation_status", options.get("moderation_status"));
128-
return callApi(HttpMethod.POST, Arrays.asList("resources", resourceType, type, public_id),
144+
ApiResponse response = callApi(HttpMethod.POST, Arrays.asList("resources", resourceType, type, public_id),
129145
params, options);
146+
addAccessModeToResponse(response,"public");
147+
return response;
130148
}
131149

132150
public ApiResponse deleteResources(Iterable<String> publicIds, Map options) throws Exception {
@@ -250,7 +268,9 @@ public ApiResponse restore(Iterable<String> publicIds, Map options) throws Excep
250268
String type = ObjectUtils.asString(options.get("type"), "upload");
251269
Map params = new HashMap<String, Object>();
252270
params.put("public_ids", publicIds);
253-
return callApi(HttpMethod.POST, Arrays.asList("resources", resourceType, type, "restore"), params, options);
271+
272+
ApiResponse response = callApi(HttpMethod.POST, Arrays.asList("resources", resourceType, type, "restore"), params, options);
273+
return response;
254274
}
255275

256276
public ApiResponse uploadMappings(Map options) throws Exception {
@@ -449,6 +469,79 @@ public ApiResponse updateStreamingProfile(String name, String displayName, List<
449469
return callApi(HttpMethod.PUT, uri, params, options);
450470
}
451471

472+
public ApiResponse updateResourcesAccessModeByIds(String accessMode, List<String> ids, Map options) throws Exception {
473+
if (options == null) options = ObjectUtils.asMap();
474+
validateAccessMode(accessMode);
475+
options.put("max_results",100);
476+
477+
ApiResponse response = this.resourcesByIds(ids,options);
478+
addAccessModeToResponse(response,accessMode);
479+
response.put("updated",response.get("resources"));
480+
response.remove("resources");
481+
response.put("failed" ,Arrays.asList());
482+
483+
return response;
484+
}
485+
486+
public ApiResponse updateResourcesAccessModeByTag(String accessMode, String tag,Map options) throws Exception {
487+
if (options == null) options = ObjectUtils.asMap();
488+
validateAccessMode(accessMode);
489+
options.put("max_results",100);
490+
ApiResponse response = this.resourcesByTag(tag,options);
491+
addAccessModeToResponse(response,accessMode);
492+
response.put("updated",response.get("resources"));
493+
response.remove("resources");
494+
response.put("failed" ,Arrays.asList());
495+
return response;
496+
}
497+
498+
public ApiResponse updateResourcesAccessModeByPrefix(String accessMode, String prefix, Map options) throws Exception{
499+
if (options == null) options = ObjectUtils.asMap();
500+
validateAccessMode(accessMode);
501+
options.put("prefix",prefix);
502+
options.put("max_results",100);
503+
ApiResponse response = this.resources(options);
504+
addAccessModeToResponse(response,accessMode);
505+
response.put("updated",response.get("resources"));
506+
response.remove("resources");
507+
response.put("failed" ,Arrays.asList());
508+
return response;
509+
}
510+
511+
private void validateAccessMode(String accessMode){
512+
List<String> modes = Arrays.asList("public","authenticated");
513+
if (!modes.contains(accessMode)){
514+
throw new Error("access mode \""+accessMode+"\" not does not match "+ StringUtils.join(modes,"/"));
515+
}
516+
}
517+
518+
private void addAccessModeToCollection(Object collection, String accessMode){
519+
List<Object> collectionList = (List) collection;
520+
if (collectionList==null) {
521+
throw new Error("no collection found");
522+
523+
}
524+
for (Object listItem :collectionList){
525+
this.addAccessModeToResource(listItem, accessMode);
526+
}
527+
}
528+
529+
private void addAccessModeToResponse(ApiResponse response, String accessMode){
530+
if (response.keySet().contains("resources")){
531+
addAccessModeToCollection(response.get("resources"),accessMode);
532+
}else if (response.keySet().contains("public_id")){
533+
addAccessModeToResource(response,accessMode);
534+
}else {
535+
throw new Error("unidentified response type (keys: "+StringUtils.join(response.keySet(),",")+")");
536+
}
537+
}
538+
539+
private void addAccessModeToResource(Object resource, String accessMode){
540+
Map<Object,Object> resourceMap = (Map<Object,Object>) resource;
541+
if (resourceMap==null) { return ;}
542+
resourceMap.put("access_mode",accessMode);
543+
}
544+
452545
/**
453546
* @see Api#updateStreamingProfile(String, String, List, Map)
454547
*/

0 commit comments

Comments
 (0)