Skip to content

Commit d36415b

Browse files
author
Amir Tocker
committed
Add streaming profiles API
1 parent 56bb360 commit d36415b

File tree

5 files changed

+297
-7
lines changed

5 files changed

+297
-7
lines changed

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

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

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
3+
import java.util.*;
4+
import java.util.concurrent.ExecutionException;
85

96
import com.cloudinary.api.ApiResponse;
107
import com.cloudinary.api.AuthorizationRequired;
@@ -16,11 +13,13 @@
1613
import com.cloudinary.api.exceptions.RateLimited;
1714
import com.cloudinary.strategies.AbstractApiStrategy;
1815
import com.cloudinary.utils.ObjectUtils;
16+
import org.cloudinary.json.JSONArray;
1917

2018
@SuppressWarnings({"rawtypes", "unchecked"})
2119
public class Api {
22-
public enum HttpMethod {GET, POST, PUT, DELETE}
2320

21+
22+
public enum HttpMethod {GET, POST, PUT, DELETE;}
2423
public final static Map<Integer, Class<? extends Exception>> CLOUDINARY_API_ERROR_CLASSES = new HashMap<Integer, Class<? extends Exception>>();
2524

2625
static {
@@ -34,8 +33,8 @@ public enum HttpMethod {GET, POST, PUT, DELETE}
3433
}
3534

3635
public final Cloudinary cloudinary;
37-
private AbstractApiStrategy strategy;
3836

37+
private AbstractApiStrategy strategy;
3938
protected ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
4039
return this.strategy.callApi(method, uri, params, options);
4140
}
@@ -299,4 +298,146 @@ private ApiResponse publishResource(String byKey, Object value, Map options) thr
299298
params.putAll(ObjectUtils.only(options, "invalidate", "overwrite"));
300299
return callApi(HttpMethod.POST, uri, params, options);
301300
}
301+
302+
/**
303+
* Create a new streaming profile
304+
*
305+
* @param name the of the profile
306+
* @param displayName the display name of the profile
307+
* @param representations a collection of Maps with a transformation key
308+
* @param options additional options
309+
* @return the new streaming profile
310+
* @throws Exception an exception
311+
*/
312+
public ApiResponse createStreamingProfile(String name, String displayName, List<Map> representations, Map options) throws Exception {
313+
if (options == null)
314+
options = ObjectUtils.emptyMap();
315+
List<Map> serializedRepresentations = new ArrayList<Map>(representations.size());
316+
for (Map t : representations) {
317+
final Object transformation = t.get("transformation");
318+
serializedRepresentations.add(ObjectUtils.asMap("transformation", transformation.toString()));
319+
}
320+
List<String> uri = Collections.singletonList("streaming_profiles");
321+
final Map params = ObjectUtils.asMap(
322+
"name", name,
323+
"representations", new JSONArray(serializedRepresentations.toArray())
324+
);
325+
if (displayName != null) {
326+
params.put("display_name", displayName);
327+
}
328+
return callApi(HttpMethod.POST, uri, params, options);
329+
}
330+
331+
/**
332+
* @see Api#createStreamingProfile(String, String, List, Map)
333+
*/
334+
public ApiResponse createStreamingProfile(String name, String displayName, List<Map> representations) throws Exception {
335+
return createStreamingProfile(name, displayName, representations, null);
336+
}
337+
338+
/**
339+
* Get a streaming profile information
340+
* @param name the name of the profile to fetch
341+
* @param options additional options
342+
* @return a streaming profile
343+
* @throws Exception an exception
344+
*/
345+
public ApiResponse getStreamingProfile(String name, Map options) throws Exception {
346+
if (options == null)
347+
options = ObjectUtils.emptyMap();
348+
List<String> uri = Arrays.asList("streaming_profiles", name);
349+
350+
return callApi(HttpMethod.GET, uri, ObjectUtils.emptyMap(), options);
351+
352+
}
353+
354+
/**
355+
* @see Api#getStreamingProfile(String, Map)
356+
*/
357+
public ApiResponse getStreamingProfile(String name) throws Exception {
358+
return getStreamingProfile(name, null);
359+
}
360+
361+
/**
362+
* List Streaming profiles
363+
* @param options additional options
364+
* @return a list of all streaming profiles defined for the current cloud
365+
* @throws Exception an exception
366+
*/
367+
public ApiResponse listStreamingProfiles(Map options) throws Exception {
368+
if (options == null)
369+
options = ObjectUtils.emptyMap();
370+
List<String> uri = Collections.singletonList("streaming_profiles");
371+
return callApi(HttpMethod.GET, uri, ObjectUtils.emptyMap(), options);
372+
373+
}
374+
375+
/**
376+
* @see Api#listStreamingProfiles(Map)
377+
*/
378+
public ApiResponse listStreamingProfiles() throws Exception {
379+
return listStreamingProfiles(null);
380+
}
381+
382+
/**
383+
* Delete a streaming profile information. Predefined profiles are restored to the default setting.
384+
* @param name the name of the profile to delete
385+
* @param options additional options
386+
* @return a streaming profile
387+
* @throws Exception an exception
388+
*/
389+
public ApiResponse deleteStreamingProfile(String name, Map options) throws Exception {
390+
if (options == null)
391+
options = ObjectUtils.emptyMap();
392+
List<String> uri = Arrays.asList("streaming_profiles", name);
393+
394+
return callApi(HttpMethod.DELETE, uri, ObjectUtils.emptyMap(), options);
395+
396+
}
397+
398+
/**
399+
* @see Api#deleteStreamingProfile(String, Map)
400+
*/
401+
public ApiResponse deleteStreamingProfile(String name) throws Exception {
402+
return getStreamingProfile(name, null);
403+
}
404+
405+
/**
406+
* Create a new streaming profile
407+
*
408+
* @param name the of the profile
409+
* @param displayName the display name of the profile
410+
* @param representations a collection of Maps with a transformation key
411+
* @param options additional options
412+
* @return the new streaming profile
413+
* @throws Exception an exception
414+
*/
415+
public ApiResponse updateStreamingProfile(String name, String displayName, List<Map> representations, Map options) throws Exception {
416+
if (options == null)
417+
options = ObjectUtils.emptyMap();
418+
List<Map> serializedRepresentations;
419+
final Map params = new HashMap();
420+
List<String> uri = Arrays.asList("streaming_profiles", name);
421+
422+
if (representations != null) {
423+
serializedRepresentations = new ArrayList<Map>(representations.size());
424+
for (Map t : representations) {
425+
final Object transformation = t.get("transformation");
426+
serializedRepresentations.add(ObjectUtils.asMap("transformation", transformation.toString()));
427+
}
428+
params.put("representations", new JSONArray(serializedRepresentations.toArray()));
429+
}
430+
if (displayName != null) {
431+
params.put("display_name", displayName);
432+
}
433+
return callApi(HttpMethod.PUT, uri, params, options);
434+
}
435+
436+
/**
437+
* @see Api#updateStreamingProfile(String, String, List, Map)
438+
*/
439+
public ApiResponse updateStreamingProfile(String name, String displayName, List<Map> representations) throws Exception {
440+
return createStreamingProfile(name, displayName, representations);
441+
}
442+
302443
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cloudinary.test;
2+
3+
/**
4+
* Created by amir on 25/10/2016.
5+
*/
6+
public class StreamingProfilesApiTest extends AbstractStreamingProfilesApiTest {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cloudinary.test;
2+
3+
/**
4+
* Created by amir on 25/10/2016.
5+
*/
6+
public class StreamingProfilesApiTest extends AbstractStreamingProfilesApiTest {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cloudinary.test;
2+
3+
/**
4+
* Created by amir on 25/10/2016.
5+
*/
6+
public class StreamingProfilesApiTest extends AbstractStreamingProfilesApiTest {
7+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.api.ApiResponse;
7+
import com.cloudinary.api.exceptions.AlreadyExists;
8+
import com.cloudinary.utils.ObjectUtils;
9+
import org.hamcrest.Matcher;
10+
import org.hamcrest.Matchers;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Rule;
14+
import org.junit.Test;
15+
import org.junit.rules.TestName;
16+
17+
import java.io.IOException;
18+
import java.util.*;
19+
20+
import static org.hamcrest.Matchers.*;
21+
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assume.assumeNotNull;
24+
25+
abstract public class AbstractStreamingProfilesApiTest extends MockableTest {
26+
private static final String PROFILE_NAME = "api_test_streaming_profile" + SUFFIX;
27+
protected Api api;
28+
private static final List<String> PREDEFINED_PROFILES = Arrays.asList("4k", "full_hd", "hd", "sd", "full_hd_wifi", "full_hd_lean", "hd_lean");
29+
30+
@BeforeClass
31+
public static void setUpClass() throws IOException {
32+
Cloudinary cloudinary = new Cloudinary();
33+
if (cloudinary.config.apiSecret == null) {
34+
System.err.println("Please setup environment for Upload test to run");
35+
}
36+
}
37+
38+
@Rule
39+
public TestName currentTest = new TestName();
40+
41+
@Before
42+
public void setUp() {
43+
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
44+
this.cloudinary = new Cloudinary();
45+
assumeNotNull(cloudinary.config.apiSecret);
46+
this.api = cloudinary.api();
47+
}
48+
49+
@Test
50+
public void testCreate() throws Exception {
51+
final String name = PROFILE_NAME + "_create";
52+
ApiResponse result = api.createStreamingProfile(name, null, Collections.singletonList(ObjectUtils.asMap(
53+
"transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m")
54+
)), ObjectUtils.emptyMap());
55+
56+
assertTrue(result.containsKey("data"));
57+
Map profile = (Map) result.get("data");
58+
assertThat(profile, (Matcher) hasEntry("name", (Object) name));
59+
}
60+
61+
@Test
62+
public void testGet() throws Exception {
63+
ApiResponse result = api.getStreamingProfile(PREDEFINED_PROFILES.get(0));
64+
assertTrue(result.containsKey("data"));
65+
Map profile = (Map) result.get("data");
66+
assertThat(profile, (Matcher) hasEntry("name", (Object) (PREDEFINED_PROFILES.get(0))));
67+
68+
}
69+
70+
@Test
71+
public void testList() throws Exception {
72+
ApiResponse result = api.listStreamingProfiles();
73+
assertTrue(result.containsKey("data"));
74+
List profiles = (List) result.get("data");
75+
// check that the list contains all predefined profiles
76+
for (String p :
77+
PREDEFINED_PROFILES) {
78+
assertThat(profiles, (Matcher) hasItem(hasEntry("name", p)));
79+
}
80+
}
81+
82+
@Test
83+
public void testDelete() throws Exception {
84+
ApiResponse result;
85+
final String name = PROFILE_NAME + "_delete";
86+
try {
87+
api.createStreamingProfile(name, null, Collections.singletonList(ObjectUtils.asMap(
88+
"transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m")
89+
)), ObjectUtils.emptyMap());
90+
} catch (AlreadyExists ignored) {
91+
}
92+
93+
result = api.deleteStreamingProfile(name);
94+
assertTrue(result.containsKey("data"));
95+
Map profile = (Map) result.get("data");
96+
assertThat(profile, (Matcher) hasEntry("name", (Object) (name)));
97+
98+
}
99+
100+
@Test
101+
public void testUpdate() throws Exception {
102+
final String name = PROFILE_NAME + "_update";
103+
try {
104+
api.createStreamingProfile(name, null, Collections.singletonList(ObjectUtils.asMap(
105+
"transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m")
106+
)), ObjectUtils.emptyMap());
107+
} catch (AlreadyExists ignored) {
108+
}
109+
Map result = api.updateStreamingProfile(name, null, Collections.singletonList(
110+
ObjectUtils.asMap("transformation",
111+
new Transformation().crop("limit").width(800).height(800).bitRate("5m")
112+
)), ObjectUtils.emptyMap());
113+
114+
assertTrue(result.containsKey("data"));
115+
assertThat(result, (Matcher) hasEntry("message", (Object) "updated"));
116+
Map profile = (Map) result.get("data");
117+
assertThat(profile, (Matcher) hasEntry("name", (Object) (name)));
118+
assertThat(profile, Matchers.hasEntry(equalTo("representations"), (Matcher) hasItem(hasKey("transformation"))));
119+
final Map representation = (Map) ((List) profile.get("representations")).get(0);
120+
Map transformation = (Map) ((List)representation.get("transformation")).get(0);
121+
assertThat(transformation, allOf(
122+
(Matcher) hasEntry("width", 800),
123+
(Matcher) hasEntry("height", 800),
124+
(Matcher) hasEntry("crop", "limit"),
125+
(Matcher) hasEntry("bit_rate", "5m")
126+
));
127+
}
128+
}

0 commit comments

Comments
 (0)