Skip to content

Commit 3e33935

Browse files
author
Amir Tocker
committed
Merge branch 'feature/search-by-context'
2 parents cca7bc9 + 090dc23 commit 3e33935

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.cloudinary.strategies.AbstractApiStrategy;
1515
import com.cloudinary.utils.ObjectUtils;
1616
import org.cloudinary.json.JSONArray;
17+
import com.cloudinary.utils.StringUtils;
1718

1819
@SuppressWarnings({"rawtypes", "unchecked"})
1920
public class Api {
@@ -78,6 +79,21 @@ public ApiResponse resourcesByTag(String tag, Map options) throws Exception {
7879
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options);
7980
}
8081

82+
public ApiResponse resourcesByContext(String key, Map options) throws Exception {
83+
return resourcesByContext(key,null,options);
84+
}
85+
86+
public ApiResponse resourcesByContext(String key,String value, Map options) throws Exception {
87+
if (options == null) options = ObjectUtils.emptyMap();
88+
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");
89+
Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations");
90+
params.put("key",key);
91+
if (StringUtils.isNotBlank(value)) {
92+
params.put("value",value);
93+
}
94+
return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType,"context"), params , options);
95+
}
96+
8197
public ApiResponse resourcesByIds(Iterable<String> publicIds, Map options) throws Exception {
8298
if (options == null) options = ObjectUtils.emptyMap();
8399
String resourceType = ObjectUtils.asString(options.get("resource_type"), "image");

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static void setUpClass() throws IOException {
5050
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
5151
options.put("public_id", API_TEST_1);
5252
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
53+
54+
options = ObjectUtils.asMap("public_id", "context_1", "tags", new String[]{SDK_TEST_TAG, uniqueTag}, "context", "test-key=alt");
55+
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
56+
options = ObjectUtils.asMap("public_id", "context_2", "tags", new String[]{SDK_TEST_TAG, uniqueTag}, "context", "test-key=alternate");
57+
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
5358
}
5459

5560
@AfterClass
@@ -387,6 +392,18 @@ public void test17aTransformationDeleteImplicit() throws Exception {
387392
api.deleteTransformation("c_scale,w_100", ObjectUtils.emptyMap());
388393
}
389394

395+
@Test
396+
public void test20ResourcesContext() throws Exception {
397+
Map result = api.resourcesByContext("test-key", ObjectUtils.emptyMap());
398+
399+
List<Map> resources = (List<Map>) result.get("resources");
400+
assertEquals(2,resources.size());
401+
result = api.resourcesByContext("test-key","alt", ObjectUtils.emptyMap());
402+
403+
resources = (List<Map>) result.get("resources");
404+
assertEquals(1,resources.size());
405+
}
406+
390407
/**
391408
* @throws Exception
392409
* @expectedException \Cloudinary\Api\NotFound

0 commit comments

Comments
 (0)