22
33import java .io .IOException ;
44import java .util .HashMap ;
5+ import java .util .Iterator ;
56import java .util .Map ;
67
78import org .json .simple .JSONArray ;
1819
1920public class MSBatchResponseContent {
2021
21- private JSONObject batchResponseObj ;
2222 private Response batchResponse ;
2323 private Map <String , Request > batchRequestsHashMap ;
2424 private JSONArray batchResponseArray ;
25+ private String nextLink ;
2526
2627 /*
2728 * @param batchResponse OkHttp batch response on execution of batch requests
2829 */
2930 public MSBatchResponseContent (Response batchResponse ) {
30- if (batchResponse == null )
31- throw new IllegalArgumentException ("Batch Response cannot be null" );
32-
33- this .batchRequestsHashMap = createBatchRequestsHashMap (batchResponse );
3431 this .batchResponse = batchResponse ;
35- if (batchResponse .body () != null ) {
36- try {
37- String batchResponseData = batchResponse .body ().string ();
38- if (batchResponseData != null ) {
39- batchResponseObj = stringToJSONObject (batchResponseData );
40- if (batchResponseObj != null ) {
41- batchResponseArray = (JSONArray )batchResponseObj .get ("responses" );
42- }
43- }
44- } catch (IOException e ) {
45- e .printStackTrace ();
46- }
47- }
32+ update (batchResponse );
4833 }
4934
5035 /*
@@ -54,12 +39,9 @@ public MSBatchResponseContent(Response batchResponse) {
5439 * @return OkHttp Response corresponding to requestId
5540 */
5641 public Response getResponseById (String requestId ) {
57- if (batchResponseObj == null )
58- return null ;
42+ if (batchResponseArray == null ) return null ;
5943
60- JSONArray responses = (JSONArray )batchResponseObj .get ("responses" );
61- if (responses == null )
62- return null ;
44+ JSONArray responses = batchResponseArray ;
6345
6446 for (Object response : responses ) {
6547 JSONObject jsonresponse = (JSONObject )response ;
@@ -104,20 +86,76 @@ public Response getResponseById(String requestId) {
10486 return null ;
10587 }
10688
107- /*
108- * @return responses as a string
89+ /**
90+ * Get map of id and responses
91+ *
92+ * @return responses in Map of id and response
10993 */
110- public String getResponses () {
111- return batchResponseArray != null ? batchResponseArray .toJSONString () : null ;
94+ public Map <String , Response > getResponses () {
95+ if (batchResponseArray == null )
96+ return null ;
97+ Map <String , Response > responsesMap = new HashMap <>();
98+ for (String id : batchRequestsHashMap .keySet ()) {
99+ responsesMap .put (id , getResponseById (id ));
100+ }
101+ return responsesMap ;
102+ }
103+
104+ /**
105+ * Get iterator over the responses
106+ *
107+ * @return iterator for responses
108+ */
109+ public Iterator <Map .Entry <String , Response >> getResponsesIterator () {
110+ if (batchResponseArray == null )
111+ return null ;
112+ Map <String , Response > responsesMap = new HashMap <>();
113+ for (String id : batchRequestsHashMap .keySet ()) {
114+ responsesMap .put (id , getResponseById (id ));
115+ }
116+ return responsesMap .entrySet ().iterator ();
117+ }
118+
119+ public void update (Response batchResponse ) {
120+ if (batchResponse == null )
121+ throw new IllegalArgumentException ("Batch Response cannot be null" );
122+
123+ Map <String , Request > requestMap = createBatchRequestsHashMap (batchResponse );
124+ if (batchRequestsHashMap == null )
125+ batchRequestsHashMap = new HashMap <>();
126+ if (requestMap != null )
127+ batchRequestsHashMap .putAll (requestMap );
128+
129+ if (batchResponse .body () != null ) {
130+ try {
131+ String batchResponseData = batchResponse .body ().string ();
132+ if (batchResponseData != null ) {
133+ JSONObject batchResponseObj = stringToJSONObject (batchResponseData );
134+ if (batchResponseObj != null ) {
135+
136+ JSONObject nextLinkObject = (JSONObject ) batchResponseObj .get ("nextLink" );
137+ if (nextLinkObject !=null )
138+ nextLink = nextLinkObject .toString ();
139+
140+ if (batchResponseArray == null )
141+ batchResponseArray = new JSONArray ();
142+
143+ JSONArray responseArray = (JSONArray )batchResponseObj .get ("responses" );
144+ if (responseArray !=null )
145+ batchResponseArray .addAll (responseArray );
146+ }
147+ }
148+ } catch (IOException e ) {
149+ e .printStackTrace ();
150+ }
151+ }
112152 }
113153
114154 /*
115155 * @return nextLink of batch response
116156 */
117157 public String nextLink () {
118- if (batchResponseObj == null ) return null ;
119- Object nextLinkObject = batchResponseObj .get ("nextLink" );
120- return nextLinkObject != null ? ((JSONObject )nextLinkObject ).toString () : null ;
158+ return nextLink ;
121159 }
122160
123161 private Map <String , Request > createBatchRequestsHashMap (Response batchResponse ) {
0 commit comments