Skip to content

Commit 959f98a

Browse files
authored
Add selective response support
1 parent 09e0719 commit 959f98a

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ public ApiResponse resources(Map options) throws Exception {
8787
uri.add(resourceType);
8888
if (type != null)
8989
uri.add(type);
90-
91-
ApiResponse response = callApi(HttpMethod.GET, uri, ObjectUtils.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations", "start_at", "metadata"), options);
90+
if(options.get("fields") != null) {
91+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
92+
}
93+
ApiResponse response = callApi(HttpMethod.GET, uri, ObjectUtils.only(options, "next_cursor", "direction", "max_results", "prefix", "tags", "context", "moderations", "start_at", "metadata", "fields"), options);
9294
return response;
9395
}
9496

@@ -106,8 +108,10 @@ public ApiResponse visualSearch(Map options) throws Exception {
106108
public ApiResponse resourcesByTag(String tag, Map options) throws Exception {
107109
if (options == null) options = ObjectUtils.emptyMap();
108110
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
109-
110-
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata"), options);
111+
if(options.get("fields") != null) {
112+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
113+
}
114+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata", "fields"), options);
111115
return response;
112116
}
113117

@@ -118,7 +122,10 @@ public ApiResponse resourcesByContext(String key, Map options) throws Exception
118122
public ApiResponse resourcesByContext(String key, String value, Map options) throws Exception {
119123
if (options == null) options = ObjectUtils.emptyMap();
120124
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
121-
Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata");
125+
if(options.get("fields") != null) {
126+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
127+
}
128+
Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata", "fields");
122129
params.put("key", key);
123130
if (StringUtils.isNotBlank(value)) {
124131
params.put("value", value);
@@ -128,7 +135,10 @@ public ApiResponse resourcesByContext(String key, String value, Map options) thr
128135

129136
public ApiResponse resourceByAssetID(String assetId, Map options) throws Exception {
130137
if (options == null) options = ObjectUtils.emptyMap();
131-
Map params = ObjectUtils.only(options, "tags", "context", "moderations");
138+
if(options.get("fields") != null) {
139+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
140+
}
141+
Map params = ObjectUtils.only(options, "tags", "context", "moderations", "fields");
132142
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", assetId), params, options);
133143
return response;
134144
}
@@ -142,7 +152,10 @@ public ApiResponse resourcesByAssetIDs(Iterable<String> assetIds, Map options) t
142152

143153
public ApiResponse resourcesByAssetFolder(String assetFolder, Map options) throws Exception {
144154
if (options == null) options = ObjectUtils.emptyMap();
145-
Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations");
155+
if(options.get("fields") != null) {
156+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
157+
}
158+
Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "fields");
146159
params.put("asset_folder", assetFolder);
147160
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources/by_asset_folder"), params, options);
148161
return response;
@@ -161,8 +174,10 @@ public ApiResponse resourcesByIds(Iterable<String> publicIds, Map options) throw
161174
public ApiResponse resourcesByModeration(String kind, String status, Map options) throws Exception {
162175
if (options == null) options = ObjectUtils.emptyMap();
163176
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
164-
165-
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata"), options);
177+
if(options.get("fields") != null) {
178+
options.put("fields", StringUtils.join(ObjectUtils.asArray(options.get("fields")), ","));
179+
}
180+
ApiResponse response = callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "moderations", kind, status), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations", "metadata", "fields"), options);
166181
return response;
167182
}
168183

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Search {
1919
private ArrayList<String> aggregateParam;
2020
private ArrayList<String> withFieldParam;
2121
private HashMap<String, Object> params;
22+
private ArrayList<String> fields;
2223

2324
private int ttl = 300;
2425

@@ -28,6 +29,7 @@ public class Search {
2829
this.sortByParam = new ArrayList<HashMap<String, Object>>();
2930
this.aggregateParam = new ArrayList<String>();
3031
this.withFieldParam = new ArrayList<String>();
32+
this.fields = new ArrayList<String>();
3133
}
3234

3335
public Search ttl(int ttl) {
@@ -76,6 +78,13 @@ public Search sortBy(String field, String dir) {
7678
return this;
7779
}
7880

81+
public Search fields(String field) {
82+
if (!fields.contains(field)) {
83+
fields.add(field);
84+
}
85+
return this;
86+
}
87+
7988
public HashMap<String, Object> toQuery() {
8089
HashMap<String, Object> queryParams = new HashMap<String, Object>(this.params);
8190
if (withFieldParam.size() > 0) {
@@ -87,6 +96,9 @@ public HashMap<String, Object> toQuery() {
8796
if(aggregateParam.size() > 0) {
8897
queryParams.put("aggregate", aggregateParam);
8998
}
99+
if(fields.size() > 0) {
100+
queryParams.put("fields", fields);
101+
}
90102
return queryParams;
91103
}
92104

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ public void test01ResourceTypes() throws Exception {
181181
assertThat(resource_types, hasItem("image"));
182182
}
183183

184+
@Test
185+
public void testSingleSelectiveResponse() throws Exception {
186+
Map options = new HashMap();
187+
options.put("fields", "width");
188+
Map result = api.resources(options);
189+
List<Map> resources = (List<Map>) result.get("resources");
190+
assertNotNull(resources);
191+
Map resource = resources.get(0);
192+
assertNotNull(resource);
193+
assertNotNull(resource.get("width"));
194+
assertNull(resource.get("format"));
195+
}
196+
197+
@Test
198+
public void testMultipleSelectiveResponse() throws Exception {
199+
Map options = new HashMap();
200+
options.put("fields", new String[]{"width", "format"});
201+
Map result = api.resources(options);
202+
List<Map> resources = (List<Map>) result.get("resources");
203+
assertNotNull(resources);
204+
Map resource = resources.get(0);
205+
assertNotNull(resource);
206+
assertNotNull(resource.get("width"));
207+
assertNotNull(resource.get("format"));
208+
assertNull(resource.get("height"));
209+
}
210+
184211
@Test
185212
public void test03ResourcesCursor() throws Exception {
186213
// should allow listing resources with cursor

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,16 @@ public void testShouldBuildSearchUrl() throws Exception {
177177
cloudinaryToSearch.config.privateCdn = true;
178178
assertEquals(String.format("https://%s-res.cloudinary.com/search/%s/%d/%s", cloudinaryToSearch.config.cloudName, ttl300Signature, 300, base64Query), search.toUrl(300, ""));
179179
}
180+
181+
@Test
182+
public void testSearchWithSelectiveResponse() throws Exception {
183+
Map result = cloudinary.search().expression(String.format("tags:%s", SEARCH_TAG)).fields("width").fields("height").execute();
184+
List<Map> resources = (List<Map>) result.get("resources");
185+
assertEquals(3, resources.size());
186+
Map resource = resources.get(0);
187+
assertNotNull(resource);
188+
assertNotNull(resource.get("width"));
189+
assertNotNull(resource.get("height"));
190+
assertNull(resource.get("format"));
191+
}
180192
}

0 commit comments

Comments
 (0)