Skip to content

Commit 51fa9b7

Browse files
Merge pull request #24 from microsoftgraph/batch-fix
Added Iterator and map responses over BatchResponse
2 parents b3e88f1 + 78d94c9 commit 51fa9b7

File tree

2 files changed

+103
-30
lines changed

2 files changed

+103
-30
lines changed

src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.HashMap;
5+
import java.util.Iterator;
56
import java.util.Map;
67

78
import org.json.simple.JSONArray;
@@ -18,33 +19,17 @@
1819

1920
public 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,71 @@ 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+
Map<String, Response> responsesMap = getResponses();
111+
return responsesMap != null ? responsesMap.entrySet().iterator() : null;
112+
}
113+
114+
public void update(Response batchResponse) {
115+
if(batchResponse == null)
116+
throw new IllegalArgumentException("Batch Response cannot be null");
117+
118+
Map<String, Request> requestMap = createBatchRequestsHashMap(batchResponse);
119+
if(batchRequestsHashMap == null)
120+
batchRequestsHashMap = new HashMap<>();
121+
if(requestMap != null)
122+
batchRequestsHashMap.putAll(requestMap);
123+
124+
if(batchResponse.body() != null) {
125+
try {
126+
String batchResponseData = batchResponse.body().string();
127+
if(batchResponseData != null) {
128+
JSONObject batchResponseObj = stringToJSONObject(batchResponseData);
129+
if(batchResponseObj != null) {
130+
131+
JSONObject nextLinkObject = (JSONObject) batchResponseObj.get("@odata.nextLink");
132+
if(nextLinkObject!=null)
133+
nextLink = nextLinkObject.toString();
134+
135+
if(batchResponseArray == null)
136+
batchResponseArray = new JSONArray();
137+
138+
JSONArray responseArray = (JSONArray)batchResponseObj.get("responses");
139+
if(responseArray!=null)
140+
batchResponseArray.addAll(responseArray);
141+
}
142+
}
143+
} catch (IOException e) {
144+
e.printStackTrace();
145+
}
146+
}
112147
}
113148

114149
/*
115150
* @return nextLink of batch response
116151
*/
117152
public String nextLink() {
118-
if(batchResponseObj == null) return null;
119-
Object nextLinkObject = batchResponseObj.get("nextLink");
120-
return nextLinkObject != null ? ((JSONObject)nextLinkObject).toString() : null;
153+
return nextLink;
121154
}
122155

123156
private Map<String, Request> createBatchRequestsHashMap(Response batchResponse) {

0 commit comments

Comments
 (0)