diff --git a/cloudinary-core/src/main/java/com/cloudinary/Api.java b/cloudinary-core/src/main/java/com/cloudinary/Api.java index 25fa8d33..fba156db 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/Api.java +++ b/cloudinary-core/src/main/java/com/cloudinary/Api.java @@ -14,6 +14,7 @@ import com.cloudinary.strategies.AbstractApiStrategy; import com.cloudinary.utils.ObjectUtils; import org.cloudinary.json.JSONArray; +import com.cloudinary.utils.StringUtils; @SuppressWarnings({"rawtypes", "unchecked"}) public class Api { @@ -78,6 +79,21 @@ public ApiResponse resourcesByTag(String tag, Map options) throws Exception { return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType, "tags", tag), ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"), options); } + public ApiResponse resourcesByContext(String key, Map options) throws Exception { + return resourcesByContext(key,null,options); + } + + public ApiResponse resourcesByContext(String key,String value, Map options) throws Exception { + if (options == null) options = ObjectUtils.emptyMap(); + String resourceType = ObjectUtils.asString(options.get("resource_type"), "image"); + Map params = ObjectUtils.only(options, "next_cursor", "direction", "max_results", "tags", "context", "moderations"); + params.put("key",key); + if (StringUtils.isNotBlank(value)) { + params.put("value",value); + } + return callApi(HttpMethod.GET, Arrays.asList("resources", resourceType,"context"), params , options); + } + public ApiResponse resourcesByIds(Iterable publicIds, Map options) throws Exception { if (options == null) options = ObjectUtils.emptyMap(); String resourceType = ObjectUtils.asString(options.get("resource_type"), "image"); diff --git a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java index 4b9e8918..0b34cf70 100644 --- a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java +++ b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java @@ -50,6 +50,11 @@ public static void setUpClass() throws IOException { cloudinary.uploader().upload(SRC_TEST_IMAGE, options); options.put("public_id", API_TEST_1); cloudinary.uploader().upload(SRC_TEST_IMAGE, options); + + options = ObjectUtils.asMap("public_id", "context_1", "tags", new String[]{SDK_TEST_TAG, uniqueTag}, "context", "test-key=alt"); + cloudinary.uploader().upload(SRC_TEST_IMAGE, options); + options = ObjectUtils.asMap("public_id", "context_2", "tags", new String[]{SDK_TEST_TAG, uniqueTag}, "context", "test-key=alternate"); + cloudinary.uploader().upload(SRC_TEST_IMAGE, options); } @AfterClass @@ -387,6 +392,18 @@ public void test17aTransformationDeleteImplicit() throws Exception { api.deleteTransformation("c_scale,w_100", ObjectUtils.emptyMap()); } + @Test + public void test20ResourcesContext() throws Exception { + Map result = api.resourcesByContext("test-key", ObjectUtils.emptyMap()); + + List resources = (List) result.get("resources"); + assertEquals(2,resources.size()); + result = api.resourcesByContext("test-key","alt", ObjectUtils.emptyMap()); + + resources = (List) result.get("resources"); + assertEquals(1,resources.size()); + } + /** * @throws Exception * @expectedException \Cloudinary\Api\NotFound