Skip to content

Commit 7e313a5

Browse files
authored
Merge pull request #640 from microsoftgraph/feature/lean-batches
feature/lean batches
2 parents ad99d82 + 494ecfe commit 7e313a5

File tree

2 files changed

+23
-42
lines changed

2 files changed

+23
-42
lines changed

docs/upgrade-to-v3.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,12 @@ The object model for JSON batching has been improved to provide a cleaner API su
174174
By:
175175
176176
```Java
177-
final MSBatchRequestContent batchRequestContent = new MSBatchRequestContent();
178-
final String meGetId = batchRequestContent.addbatchRequestStep(graphClient.me().buildRequest().withHttpMethod(HttpMethod.GET).getHttpRequest());
179-
final MSBatchResponseContent batchResponseContent = batchRequestContent.execute(graphClient);
180-
final Response userResponse = batchResponseContent.getResponseById(meGetId);
177+
final BatchRequestContent batchRequestContent = new BatchRequestContent();
178+
final String meGetId = batchRequestContent.addbatchRequestStep(graphClient.me().buildRequest().getHttpRequest());
179+
final BatchResponseContent batchResponseContent = graphClient.batch().buildRequest().post(graphClient);
180+
final User me = batchResponseContent.getResponseById(meGetId).getDeserializedBody(User.class);
181181
```
182182
183-
> Note: this change also replaces the `MSBatchRequestContent` constuctor accepting a list of steps by a vararg constructor and `getArrayOfDependsOnIds` by `getDependsOnIds` (HashSet\<String>).
184-
185183
### IJsonBackedObject interface
186184
187185
The interface has been simplified to remove the `getRawObject` and `getSerializer` methods. This avoid entity and collection objects storing a copy of the JSON on top of the properties they already have, which improves drastically the memory impacts:

src/test/java/com/microsoft/graph/functional/BatchTests.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import java.io.IOException;
77

8-
import com.microsoft.graph.content.MSBatchRequestContent;
9-
import com.microsoft.graph.content.MSBatchResponseContent;
8+
import com.microsoft.graph.content.BatchRequestContent;
9+
import com.microsoft.graph.content.BatchResponseContent;
1010
import com.microsoft.graph.http.HttpMethod;
1111
import com.microsoft.graph.httpcore.HttpClients;
1212
import com.microsoft.graph.requests.GraphServiceClient;
@@ -17,54 +17,37 @@
1717
import org.junit.jupiter.api.Disabled;
1818
import org.junit.jupiter.api.Test;
1919

20-
import okhttp3.MediaType;
21-
import okhttp3.OkHttpClient;
22-
import okhttp3.Request;
23-
import okhttp3.RequestBody;
24-
import okhttp3.Response;
25-
2620
@Disabled
2721
public class BatchTests {
2822
@Test
2923
public void GetsABatchFromRequests() throws IOException{
3024
final TestBase testBase = new TestBase();
31-
final GraphServiceClient graphServiceClient = testBase.graphClient;
32-
final MSBatchRequestContent batchContent = new MSBatchRequestContent();
25+
final GraphServiceClient graphServiceClient = testBase.graphClient;
26+
final BatchRequestContent batchContent = new BatchRequestContent();
3327
final String meGetId = batchContent.addBatchRequestStep(graphServiceClient.me()
34-
.buildRequest()
35-
.withHttpMethod(HttpMethod.GET)
36-
.getHttpRequest());
28+
.buildRequest());
3729
assertNotNull(meGetId);
3830
final String usersGetId = batchContent.addBatchRequestStep(graphServiceClient.users()
3931
.buildRequest()
4032
.filter("accountEnabled eq true")
4133
.expand("manager")
42-
.top(5)
43-
.withHttpMethod(HttpMethod.GET)
44-
.getHttpRequest(),
34+
.top(5),
35+
HttpMethod.GET,
36+
null,
4537
meGetId);
4638
final User userToAdd = new User();
4739
userToAdd.givenName = "Darrel";
4840
final String userPostId = batchContent.addBatchRequestStep(graphServiceClient.users()
49-
.buildRequest()
50-
.withHttpMethod(HttpMethod.POST)
51-
.getHttpRequest(userToAdd), usersGetId);
52-
53-
final String serializedBatchContent = batchContent.getBatchRequestContent();
54-
55-
final Request batchRequest = new Request.Builder()
56-
.url("https://graph.microsoft.com/v1.0/$batch")
57-
.post(RequestBody.create(serializedBatchContent, MediaType.parse("application/json")))
58-
.build();
59-
60-
final OkHttpClient client = HttpClients.createDefault(testBase.getAuthenticationProvider());
61-
try (final Response batchResponse = client.newCall(batchRequest).execute()) {
62-
assertEquals(200, batchResponse.code());
63-
64-
final MSBatchResponseContent responseContent = new MSBatchResponseContent(batchResponse);
65-
assertEquals(400, responseContent.getResponseById(userPostId).code()); //400:we're not providing enough properties for the call to go through
66-
assertEquals(200, responseContent.getResponseById(meGetId).code());
67-
assertEquals(200, responseContent.getResponseById(usersGetId).code());
68-
}
41+
.buildRequest(),
42+
HttpMethod.POST,
43+
userToAdd,
44+
usersGetId);
45+
46+
final BatchResponseContent responseContent = testBase.graphClient.batch().buildRequest().post(batchContent);
47+
assertEquals(400, responseContent.getResponseById(userPostId).status); //400:we're not providing enough properties for the call to go through
48+
assertEquals(200, responseContent.getResponseById(meGetId).status);
49+
assertEquals(200, responseContent.getResponseById(usersGetId).status);
50+
final User me = responseContent.getResponseById(meGetId).getDeserializedBody(User.class);
51+
assertNotNull(me.displayName);
6952
}
7053
}

0 commit comments

Comments
 (0)