11package 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
96import com .cloudinary .api .ApiResponse ;
107import com .cloudinary .api .AuthorizationRequired ;
1613import com .cloudinary .api .exceptions .RateLimited ;
1714import com .cloudinary .strategies .AbstractApiStrategy ;
1815import com .cloudinary .utils .ObjectUtils ;
16+ import org .cloudinary .json .JSONArray ;
1917
2018@ SuppressWarnings ({"rawtypes" , "unchecked" })
2119public 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}
0 commit comments