Skip to content

Commit 918129d

Browse files
committed
- add a method on the client to execute batches
- replaces older, native client dependent implementation by a new implementation for batches
1 parent 93cdf2f commit 918129d

16 files changed

Lines changed: 704 additions & 921 deletions
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2021 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.content;
24+
25+
import java.util.List;
26+
27+
import javax.annotation.Nonnull;
28+
import javax.annotation.Nullable;
29+
30+
import com.microsoft.graph.core.ClientException;
31+
import com.microsoft.graph.core.IBaseClient;
32+
import com.microsoft.graph.http.BaseRequest;
33+
import com.microsoft.graph.http.HttpMethod;
34+
import com.microsoft.graph.options.Option;
35+
36+
/** Request for batch requests */
37+
public class BatchRequest extends BaseRequest<BatchResponseContent> {
38+
39+
/**
40+
* Instantiates a new batch request
41+
*
42+
* @param requestUrl the URL to send the request to
43+
* @param client the client to use to execute the request
44+
* @param options the options to apply to the request
45+
*/
46+
public BatchRequest(@Nonnull final String requestUrl, @Nonnull final IBaseClient client, @Nonnull final List<? extends Option> options) {
47+
super(requestUrl, client, options, BatchResponseContent.class);
48+
}
49+
50+
/**
51+
* Send this request
52+
*
53+
* @return the response object
54+
* @param content content of the batch request to execute
55+
* @throws ClientException an exception occurs if there was an error while the request was sent
56+
*/
57+
@Nullable
58+
protected BatchResponseContent post(@Nonnull final BatchRequestContent content) throws ClientException {
59+
this.setHttpMethod(HttpMethod.POST);
60+
return this.getClient().getHttpProvider().send(this, BatchResponseContent.class, content);
61+
}
62+
/**
63+
* Send this request
64+
*
65+
* @return the response object
66+
* @param content content of the batch request to execute
67+
* @throws ClientException an exception occurs if there was an error while the request was sent
68+
*/
69+
@Nullable
70+
protected java.util.concurrent.CompletableFuture<BatchResponseContent> postAsync(@Nonnull final BatchRequestContent content) throws ClientException {
71+
this.setHttpMethod(HttpMethod.POST);
72+
return this.getClient().getHttpProvider().sendAsync(this, BatchResponseContent.class, content);
73+
}
74+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2021 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.content;
24+
25+
import java.util.List;
26+
27+
import javax.annotation.Nonnull;
28+
import javax.annotation.Nullable;
29+
30+
import com.microsoft.graph.core.IBaseClient;
31+
import com.microsoft.graph.http.BaseRequestBuilder;
32+
import com.microsoft.graph.options.Option;
33+
34+
/** Request builder for batch requests */
35+
public class BatchRequestBuilder extends BaseRequestBuilder<BatchResponseContent> {
36+
/**
37+
* Instantiates a new batch request builder
38+
* @param requestUrl the URL for the request
39+
* @param client the client to use to execute the request
40+
* @param options the request options
41+
*/
42+
public BatchRequestBuilder(@Nonnull final String requestUrl, @Nonnull final IBaseClient client, @Nonnull final List<? extends Option> options) {
43+
super(requestUrl, client, options);
44+
}
45+
/**
46+
* Creates the request
47+
*
48+
* @param requestOptions the options for this request
49+
* @return the IUserRequest instance
50+
*/
51+
@Nonnull
52+
public BatchRequest buildRequest(@Nullable final com.microsoft.graph.options.Option... requestOptions) {
53+
return buildRequest(getOptions(requestOptions));
54+
}
55+
56+
/**
57+
* Creates the request
58+
*
59+
* @param requestOptions the options for this request
60+
* @return the IUserRequest instance
61+
*/
62+
@Nonnull
63+
public BatchRequest buildRequest(@Nonnull final java.util.List<? extends com.microsoft.graph.options.Option> requestOptions) {
64+
return new BatchRequest(this.getRequestUrl(), getClient(), requestOptions);
65+
}
66+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2021 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.content;
24+
25+
import java.util.ArrayList;
26+
import java.util.HashMap;
27+
import java.util.HashSet;
28+
import java.util.List;
29+
import java.util.Objects;
30+
import java.util.concurrent.ThreadLocalRandom;
31+
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
33+
34+
import javax.annotation.Nonnull;
35+
import javax.annotation.Nullable;
36+
37+
import com.google.gson.annotations.Expose;
38+
import com.google.gson.annotations.SerializedName;
39+
import com.microsoft.graph.http.HttpMethod;
40+
import com.microsoft.graph.http.IHttpRequest;
41+
import com.microsoft.graph.options.HeaderOption;
42+
43+
public class BatchRequestContent {
44+
@Expose
45+
@SerializedName("requests")
46+
public List<BatchRequestStep<?>> requests;
47+
48+
@Nonnull
49+
public String addBatchRequestStep(@Nonnull final IHttpRequest request) {
50+
Objects.requireNonNull(request, "request parameter cannot be null");
51+
return addBatchRequestStep(request, request.getHttpMethod() == null ? HttpMethod.GET : request.getHttpMethod());
52+
}
53+
@Nonnull
54+
public String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod) {
55+
return addBatchRequestStep(request, httpMethod, null);
56+
}
57+
@Nonnull
58+
public <T> String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod, @Nullable final T serializableBody) {
59+
return addBatchRequestStep(request, httpMethod, serializableBody, null);
60+
}
61+
public <T> String addBatchRequestStep(@Nonnull final IHttpRequest request, @Nonnull final HttpMethod httpMethod, @Nullable final T serializableBody, @Nullable final HashSet<String> dependsOnRequestsIds) {
62+
Objects.requireNonNull(request, "request parameter cannot be null");
63+
Objects.requireNonNull(httpMethod, "httpMethod parameter cannot be null");
64+
if(dependsOnRequestsIds != null)
65+
for(final String id : dependsOnRequestsIds) {
66+
if(getStepById(id) == null)
67+
throw new IllegalArgumentException("the current request depends on a inexisting request");
68+
}
69+
70+
if(requests == null)
71+
requests = new ArrayList<>();
72+
73+
final Matcher protocolAndHostReplacementMatcher = protocolAndHostReplacementPattern.matcher(request.getRequestUrl().toString());
74+
final BatchRequestStep<T> step = new BatchRequestStep<T>() {{
75+
url = protocolAndHostReplacementMatcher.replaceAll("");
76+
body = serializableBody;
77+
method = httpMethod;
78+
dependsOn = dependsOnRequestsIds;
79+
id = getNextRequestId();
80+
}};
81+
82+
if(!request.getHeaders().isEmpty()) {
83+
step.headers = new HashMap<>();
84+
for(final HeaderOption headerOption : request.getHeaders())
85+
step.headers.putIfAbsent(headerOption.getName(), headerOption.getValue().toString());
86+
}
87+
requests.add(step);
88+
return step.id;
89+
}
90+
public void removeBatchRequestStepWithId(@Nonnull final String ...stepIds) {
91+
if(requests == null) return;
92+
93+
for(final String stepId : stepIds) {
94+
Objects.requireNonNull(stepId, "parameter stepIds cannot contain null values");
95+
requests.removeIf(x -> x.id == stepId);
96+
for(final BatchRequestStep<?> step : requests) {
97+
if(step.dependsOn != null) {
98+
step.dependsOn.removeIf(x -> x == stepId);
99+
if(step.dependsOn.isEmpty())
100+
step.dependsOn = null; // so we don't send dependsOn: [] over the wire
101+
}
102+
}
103+
}
104+
}
105+
@Nullable
106+
@SuppressWarnings("unchecked")
107+
public <T> BatchRequestStep<T> getStepById(@Nonnull final String stepId) {
108+
Objects.requireNonNull(stepId, "parameter stepId cannot be null");
109+
if(requests == null) return null;
110+
111+
for(final BatchRequestStep<?> step : requests) {
112+
if(stepId.equals(step.id))
113+
return (BatchRequestStep<T>)step;
114+
}
115+
return null;
116+
}
117+
private static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive
118+
private String getNextRequestId() {
119+
String requestId;
120+
do {
121+
requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE));
122+
} while(getStepById(requestId) != null);
123+
return requestId;
124+
}
125+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2021 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.content;
24+
25+
import java.util.HashSet;
26+
27+
import javax.annotation.Nullable;
28+
29+
import com.google.gson.annotations.Expose;
30+
import com.google.gson.annotations.SerializedName;
31+
import com.microsoft.graph.http.HttpMethod;
32+
33+
/** the http request for the batch step */
34+
public class BatchRequestStep<T> extends BatchStep<T> {
35+
/** The URL to query for the step */
36+
@Nullable
37+
@Expose
38+
@SerializedName("url")
39+
public String url;
40+
@Expose
41+
@SerializedName("method")
42+
public HttpMethod method;
43+
@Expose
44+
@SerializedName("dependsOn")
45+
public HashSet<String> dependsOn;
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2021 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.content;
24+
25+
import java.util.List;
26+
import java.util.Objects;
27+
28+
import javax.annotation.Nonnull;
29+
import javax.annotation.Nullable;
30+
31+
import com.google.gson.annotations.Expose;
32+
import com.google.gson.annotations.SerializedName;
33+
34+
public class BatchResponseContent {
35+
@Expose
36+
@SerializedName("responses")
37+
public List<BatchResponseStep<?>> responses;
38+
39+
@Nullable
40+
@SuppressWarnings("unchecked")
41+
public <T> BatchResponseStep<T> getResponseById(@Nonnull final String stepId) {
42+
Objects.requireNonNull(stepId, "parameter stepId cannot be null");
43+
if(responses == null) return null;
44+
45+
for(final BatchResponseStep<?> step : responses) {
46+
if(stepId.equals(step.id))
47+
return (BatchResponseStep<T>)step;
48+
}
49+
return null;
50+
}
51+
}

0 commit comments

Comments
 (0)