Skip to content

Commit 99e73d2

Browse files
author
Amir Tocker
committed
Add context API
1 parent 3505fcd commit 99e73d2

File tree

5 files changed

+242
-78
lines changed

5 files changed

+242
-78
lines changed

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

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818

1919
@SuppressWarnings({"rawtypes", "unchecked"})
2020
public class Uploader {
21+
22+
private final class Command {
23+
final static String add = "add";
24+
final static String remove = "remove";
25+
final static String replace = "replace";
26+
final static String removeAll = "remove_all";
27+
28+
private Command() {
29+
}
30+
}
31+
2132
public Map callApi(String action, Map<String, Object> params, Map options, Object file) throws IOException {
2233
return strategy.callApi(action, params, options, file);
2334
}
@@ -267,26 +278,26 @@ public Map addTag(String tag, String[] publicIds, Map options) throws IOExceptio
267278
if (options == null)
268279
options = ObjectUtils.emptyMap();
269280
boolean exclusive = ObjectUtils.asBoolean(options.get("exclusive"), false);
270-
String command = exclusive ? "set_exclusive" : "add";
281+
String command = exclusive ? "set_exclusive" : Command.add;
271282
return callTagsApi(tag, command, publicIds, options);
272283
}
273284

274285
public Map removeTag(String tag, String[] publicIds, Map options) throws IOException {
275286
if (options == null)
276287
options = ObjectUtils.emptyMap();
277-
return callTagsApi(tag, "remove", publicIds, options);
288+
return callTagsApi(tag, Command.remove, publicIds, options);
278289
}
279290

280291
public Map removeAllTags(String[] publicIds, Map options) throws IOException {
281292
if (options == null)
282293
options = ObjectUtils.emptyMap();
283-
return callTagsApi(null, "remove_all", publicIds, options);
294+
return callTagsApi(null, Command.removeAll, publicIds, options);
284295
}
285296

286297
public Map replaceTag(String tag, String[] publicIds, Map options) throws IOException {
287298
if (options == null)
288299
options = ObjectUtils.emptyMap();
289-
return callTagsApi(tag, "replace", publicIds, options);
300+
return callTagsApi(tag, Command.replace, publicIds, options);
290301
}
291302

292303
public Map callTagsApi(String tag, String command, String[] publicIds, Map options) throws IOException {
@@ -302,6 +313,58 @@ public Map callTagsApi(String tag, String command, String[] publicIds, Map optio
302313
return callApi("tags", params, options, null);
303314
}
304315

316+
/**
317+
* Add a context keys and values. If a particular key already exists, the value associated with the key is updated.
318+
* @param context a map of key and value. Serialized to "key1=value1|key2=value2"
319+
* @param publicIds the public IDs of the resources to update
320+
* @param options additional options passed to the request
321+
* @return a list of public IDs that were updated
322+
* @throws IOException
323+
*/
324+
public Map addContext(Map context, String[] publicIds, Map options) throws IOException {
325+
return callContextApi(context, Command.add, publicIds, options);
326+
}
327+
328+
/**
329+
* Add a context keys and values. If a particular key already exists, the value associated with the key is updated.
330+
* @param context Serialized context in the form of "key1=value1|key2=value2"
331+
* @param publicIds the public IDs of the resources to update
332+
* @param options additional options passed to the request
333+
* @return a list of public IDs that were updated
334+
* @throws IOException
335+
*/
336+
public Map addContext(String context, String[] publicIds, Map options) throws IOException {
337+
return callContextApi(context, Command.add, publicIds, options);
338+
}
339+
340+
/**
341+
* Remove all custom context from the specified public IDs.
342+
* @param publicIds the public IDs of the resources to update
343+
* @param options additional options passed to the request
344+
* @return a list of public IDs that were updated
345+
* @throws IOException
346+
*/
347+
public Map removeAllContext(String[] publicIds, Map options) throws IOException {
348+
return callContextApi((String)null, Command.removeAll, publicIds, options);
349+
}
350+
351+
protected Map callContextApi(Map context, String command, String[] publicIds, Map options) throws IOException {
352+
return callContextApi(Util.encodeContext(context), command, publicIds, options);
353+
}
354+
355+
protected Map callContextApi(String context, String command, String[] publicIds, Map options) throws IOException {
356+
if (options == null)
357+
options = ObjectUtils.emptyMap();
358+
Map<String, Object> params = new HashMap<String, Object>();
359+
if (context != null) {
360+
params.put("context", context);
361+
}
362+
params.put("command", command);
363+
params.put("type", (String) options.get("type"));
364+
params.put("public_ids", Arrays.asList(publicIds));
365+
return callApi("context", params, options, null);
366+
}
367+
305368
private final static String[] TEXT_PARAMS = {"public_id", "font_family", "font_size", "font_color", "text_align", "font_weight", "font_style",
306369
"background", "opacity", "text_decoration"};
307370

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.cloudinary.test;
2+
3+
public class ContextTest extends AbstractContextTest {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.cloudinary.test;
2+
3+
public class ContextTest extends AbstractContextTest {
4+
5+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.cloudinary.test;
2+
3+
import com.cloudinary.Api;
4+
import com.cloudinary.Cloudinary;
5+
import com.cloudinary.Transformation;
6+
import com.cloudinary.Uploader;
7+
import com.cloudinary.utils.ObjectUtils;
8+
import org.junit.*;
9+
import org.junit.rules.TestName;
10+
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import static com.cloudinary.utils.ObjectUtils.asMap;
16+
import static org.hamcrest.Matchers.*;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertThat;
19+
import static org.junit.Assume.assumeNotNull;
20+
21+
@SuppressWarnings({"rawtypes", "unchecked"})
22+
abstract public class AbstractContextTest extends MockableTest {
23+
24+
private static final String CONTEXT_TAG = "context_tag_" + String.valueOf(System.currentTimeMillis());
25+
private static Map resource;
26+
public static final Map CONTEXT = asMap("caption", "some cäption", "alt", "alternativè");
27+
private Uploader uploader;
28+
29+
@BeforeClass
30+
public static void setUpClass() throws Exception {
31+
Cloudinary cloudinary = new Cloudinary();
32+
if (cloudinary.config.apiSecret == null) {
33+
System.err.println("Please setup environment for Upload test to run");
34+
}
35+
36+
resource = cloudinary.uploader().upload(SRC_TEST_IMAGE,
37+
asMap( "tags", new String[]{SDK_TEST_TAG, CONTEXT_TAG},
38+
"context", CONTEXT,
39+
"transformation", new Transformation().crop("scale").width(10)));
40+
final String publicId = (String) resource.get("public_id");
41+
resource = cloudinary.api().resource(publicId, asMap("context", true));
42+
assertEquals(asMap("custom", CONTEXT), resource.get("context"));
43+
44+
}
45+
46+
@AfterClass
47+
public static void tearDownClass() {
48+
Api api = MockableTest.cleanUp();
49+
Cloudinary cloudinary = new Cloudinary();
50+
try {
51+
cloudinary.api().deleteResourcesByTag(CONTEXT_TAG, ObjectUtils.emptyMap());
52+
} catch (Exception ignored) {
53+
}
54+
}
55+
56+
@Rule
57+
public TestName currentTest = new TestName();
58+
59+
@Before
60+
public void setUp() throws Exception {
61+
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
62+
cloudinary = new Cloudinary();
63+
uploader = cloudinary.uploader();
64+
assumeNotNull(cloudinary.config.apiSecret);
65+
66+
}
67+
68+
@Test
69+
public void testExplicit() throws Exception {
70+
//should allow sending context
71+
72+
Map differentContext = asMap("caption", "different = caption", "alt2", "alt|alternative alternative");
73+
Map result = uploader.explicit(publicId(), asMap("type", "upload", "context", differentContext));
74+
assertEquals("explicit API should return the new context", asMap("custom", differentContext), result.get("context"));
75+
resource = cloudinary.api().resource(publicId(), asMap("context", true));
76+
assertEquals("explicit API should replace the context", asMap("custom", differentContext), resource.get("context"));
77+
}
78+
79+
@Test
80+
public void testAddContext() throws Exception {
81+
Map context = new HashMap((Map)((Map)resource.get("context")).get("custom"));
82+
context.put("caption", "new caption");
83+
Map result = uploader.addContext(asMap("caption", "new caption"), new String[]{publicId(), "no-such-id"}, null);
84+
assertThat("addContext should return a list of modified public IDs", (List<String>) result.get("public_ids"), contains(publicId()));
85+
86+
resource = cloudinary.api().resource(publicId(), asMap("context", true));
87+
assertEquals(asMap("custom", context), resource.get("context"));
88+
}
89+
90+
@Test
91+
public void testRemoveAllContext() throws Exception {
92+
Map result = uploader.removeAllContext(new String[]{publicId(), "no-such-id"}, null);
93+
assertThat((List<String>) result.get("public_ids"), contains(publicId()));
94+
95+
resource = cloudinary.api().resource(publicId(), asMap("context", true));
96+
assertThat((Map<? extends String, ?>)resource, not(hasKey("context")));
97+
}
98+
99+
private String publicId(){
100+
return (String) resource.get("public_id");
101+
}
102+
}

0 commit comments

Comments
 (0)