|
1 | 1 | package com.microsoft.graph.content; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.HashMap; |
5 | 6 | import java.util.List; |
6 | 7 | import java.util.Map; |
| 8 | +import java.util.concurrent.ThreadLocalRandom; |
7 | 9 |
|
8 | 10 | import com.google.gson.JsonArray; |
9 | 11 | import com.google.gson.JsonObject; |
@@ -50,12 +52,30 @@ public MSBatchRequestContent() { |
50 | 52 | * given |
51 | 53 | */ |
52 | 54 | public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) { |
53 | | - if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId())) |
| 55 | + if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()) || |
| 56 | + batchRequestStepsHashMap.size() >= MAX_NUMBER_OF_REQUESTS) |
54 | 57 | return false; |
55 | 58 | batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
56 | 59 | return true; |
57 | 60 | } |
58 | 61 |
|
| 62 | + /** |
| 63 | + * Add steps to batch from OkHttp.Request |
| 64 | + * @param request the request to add to the batch |
| 65 | + * @param arrayOfDependsOnIds ids of steps this step depends on |
| 66 | + * @return the step id |
| 67 | + */ |
| 68 | + public String addBatchRequestStep(final Request request, final String... arrayOfDependsOnIds) { |
| 69 | + String requestId; |
| 70 | + do { |
| 71 | + requestId = Integer.toString(ThreadLocalRandom.current().nextInt()); |
| 72 | + } while(batchRequestStepsHashMap.keySet().contains(requestId)); |
| 73 | + if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds)))) |
| 74 | + return requestId; |
| 75 | + else |
| 76 | + throw new IllegalArgumentException("unable to add step to batch. Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
| 77 | + } |
| 78 | + |
59 | 79 | /* |
60 | 80 | * @param requestId Id of Batch request step to be removed |
61 | 81 | * |
|
0 commit comments