11package com .bitmovin .api ;
22
3+ import com .bitmovin .api .analytics .query .AnalyticsQuery ;
34import com .bitmovin .api .encoding .analysis .AnalysisDetails ;
45import com .bitmovin .api .encoding .status .Message ;
56import com .bitmovin .api .encoding .status .Task ;
67import com .bitmovin .api .enums .AnswerStatus ;
78import com .bitmovin .api .exceptions .BitmovinApiException ;
8- import com .bitmovin .api .resource .AbstractResourcePatch ;
9- import com .bitmovin .api .rest .ResponseEnvelope ;
109import com .bitmovin .api .http .JsonRestClient ;
1110import com .bitmovin .api .http .RequestMethod ;
1211import com .bitmovin .api .http .RestException ;
12+ import com .bitmovin .api .resource .AbstractResourcePatch ;
13+ import com .bitmovin .api .rest .ResponseEnvelope ;
1314import com .fasterxml .jackson .core .JsonProcessingException ;
1415import com .fasterxml .jackson .core .type .TypeReference ;
1516import com .fasterxml .jackson .databind .ObjectMapper ;
1617import com .mashape .unirest .http .exceptions .UnirestException ;
1718import org .json .JSONArray ;
19+ import org .json .JSONException ;
1820import org .json .JSONObject ;
1921
2022import java .io .IOException ;
@@ -156,6 +158,13 @@ public static <T> T postRaw(String resource, Map<String, String> additionalHeade
156158 return request (resource , additionalHeaders , content , classOfT , RequestMethod .POST );
157159 }
158160
161+ public static JSONArray postAnalyticsQuery (String resource , Map <String , String > additionalHeaders , AnalyticsQuery query ) throws BitmovinApiException , URISyntaxException , UnirestException , JsonProcessingException
162+ {
163+ ResponseEnvelope responseEnvelope = request (resource , additionalHeaders , query , ResponseEnvelope .class , RequestMethod .POST );
164+ JSONObject responseObject = convertToJsonObject (responseEnvelope .getData ());
165+ return responseObject .getJSONObject ("result" ).getJSONArray ("rows" );
166+ }
167+
159168 public static List <String > getListOfIds (String url , Map <String , String > headers ) throws BitmovinApiException , UnirestException , URISyntaxException , JsonProcessingException
160169 {
161170 int offset = 0 ;
@@ -237,6 +246,23 @@ public static <T> List<T> getItems(String url, Map<String, String> headers, Clas
237246 return items ;
238247 }
239248
249+ public static <T extends AbstractApiResponse > List <T > postAndGetResults (Map <String , String > headers , String url , T body , Class <T > classOfT ) throws BitmovinApiException , UnirestException , URISyntaxException , IOException
250+ {
251+ List <T > items = new ArrayList <>();
252+
253+ ResponseEnvelope responseEnvelope = request (url , headers , body , ResponseEnvelope .class , RequestMethod .POST );
254+ JSONObject responseObject = convertToJsonObject (responseEnvelope );
255+ JSONArray jsonItems = responseObject .getJSONObject ("data" ).getJSONArray ("result" );
256+
257+ for (int i = 0 ; i < jsonItems .length (); i ++)
258+ {
259+ JSONObject idObject = jsonItems .getJSONObject (i );
260+ items .add (convertFromJsonObjectToPojo (idObject , classOfT ));
261+ }
262+
263+ return items ;
264+ }
265+
240266 public static <T > List <T > getAllItemsIterative (String url , Map <String , String > headers , Class <T > clazz ) throws IOException , BitmovinApiException , UnirestException , URISyntaxException
241267 {
242268 Long totalCount = getTotalCount (url , headers );
@@ -246,15 +272,14 @@ public static <T> List<T> getAllItemsIterative(String url, Map<String, String> h
246272 int offset = 0 ;
247273
248274 List <T > pageItems ;
249- while (items .size () <= totalCount )
275+ while (items .size () <= totalCount )
250276 {
251277 pageItems = getItems (url , headers , clazz , limit , offset );
252- if (pageItems .size () > 0 )
278+ if (pageItems .size () > 0 )
253279 {
254280 items .addAll (pageItems );
255281 offset += pageItems .size ();
256- }
257- else
282+ } else
258283 {
259284 return items ;
260285 }
@@ -294,15 +319,7 @@ public static <T extends AbstractApiResponse> T post(Map<String, String> headers
294319 JSONObject responseObject = convertToJsonObject (responseEnvelope );
295320 item .setStatus (AnswerStatus .valueOf (responseObject .getString ("status" )));
296321
297- //read Messages
298- JSONArray messagesArray = responseObject .getJSONObject ("data" ).getJSONArray ("messages" );
299- List <Message > messages = new ArrayList <>();
300-
301- for (int i = 0 ; i < messagesArray .length (); i ++)
302- {
303- JSONObject idObject = messagesArray .getJSONObject (i );
304- messages .add (convertFromJsonObjectToPojo (idObject , Message .class ));
305- }
322+ List <Message > messages = readMessagesFromResponse (responseObject );
306323 item .setMessages (messages );
307324 return item ;
308325 }
@@ -317,14 +334,7 @@ public static <T extends AbstractApiResponse> T post(Map<String, String> headers
317334 JSONObject responseObject = convertToJsonObject (responseEnvelope );
318335 body .setStatus (AnswerStatus .valueOf (responseObject .getString ("status" )));
319336
320- JSONArray messagesArray = responseObject .getJSONObject ("data" ).getJSONArray ("messages" );
321- List <Message > messages = new ArrayList <>();
322-
323- for (int i = 0 ; i < messagesArray .length (); i ++)
324- {
325- JSONObject idObject = messagesArray .getJSONObject (i );
326- messages .add (convertFromJsonObjectToPojo (idObject , Message .class ));
327- }
337+ List <Message > messages = readMessagesFromResponse (responseObject );
328338 body .setMessages (messages );
329339 return body ;
330340 }
@@ -339,16 +349,32 @@ public static <T extends AbstractApiResponse> T patch(Map<String, String> header
339349 JSONObject responseObject = convertToJsonObject (responseEnvelope );
340350 item .setStatus (AnswerStatus .valueOf (responseObject .getString ("status" )));
341351
342- JSONArray messagesArray = responseObject .getJSONObject ("data" ).getJSONArray ("messages" );
352+ List <Message > messages = readMessagesFromResponse (responseObject );
353+ item .setMessages (messages );
354+ return item ;
355+ }
356+
357+ private static List <Message > readMessagesFromResponse (JSONObject responseObject )
358+ {
343359 List <Message > messages = new ArrayList <>();
344360
345- for (int i = 0 ; i < messagesArray .length (); i ++)
361+ try
362+ {
363+ JSONArray messagesArray = responseObject .getJSONObject ("data" ).getJSONArray ("messages" );
364+ for (int i = 0 ; i < messagesArray .length (); i ++)
365+ {
366+ JSONObject idObject = messagesArray .getJSONObject (i );
367+ messages .add (convertFromJsonObjectToPojo (idObject , Message .class ));
368+ }
369+ } catch (JSONException e )
370+ {
371+ // Ignore exception on missing messages field.
372+ return messages ;
373+ } catch (IOException e )
346374 {
347- JSONObject idObject = messagesArray .getJSONObject (i );
348- messages .add (convertFromJsonObjectToPojo (idObject , Message .class ));
375+ e .printStackTrace ();
349376 }
350- item .setMessages (messages );
351- return item ;
377+ return messages ;
352378 }
353379
354380
0 commit comments