Skip to content

Commit 07d2437

Browse files
committed
Deprecate BatchRequest constructor (#1333)
* Deprecate the BatchRequest public constructor * Update Javadoc sample for batch request * Log a warning if a user is using the global batch endpoint * Fix equality check for Strings * Swap equals comparison to avoid possible NPE * Add URL to visit in the warning message
1 parent 91917b0 commit 07d2437

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.InputStream;
3333
import java.util.ArrayList;
3434
import java.util.List;
35+
import java.util.logging.Level;
36+
import java.util.logging.Logger;
3537

3638
/**
3739
* An instance of this class represents a single batch of requests.
@@ -41,7 +43,8 @@
4143
* </p>
4244
*
4345
* <pre>
44-
BatchRequest batch = new BatchRequest(transport, httpRequestInitializer);
46+
// client is a AbstractGoogleClient (e.g. com.google.api.services.books.Books)
47+
BatchRequest batch = client.batch(httpRequestInitializer);
4548
batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
4649
new BatchCallback&lt;Volumes, GoogleJsonErrorContainer&gt;() {
4750
@@ -94,8 +97,20 @@ public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) {
9497
*/
9598
public final class BatchRequest {
9699

100+
/**
101+
* The deprecated global batch endpoint. Users should actually use the per-service batch endpoint
102+
* declared by the service configuration.
103+
*/
104+
private static final String GLOBAL_BATCH_ENDPOINT = "https://www.googleapis.com/batch";
105+
private static final String GLOBAL_BATCH_ENDPOINT_WARNING = "You are using the global batch "
106+
+ "endpoint which will soon be shut down. Please instantiate your BatchRequest via your "
107+
+ "service client's `batch(HttpRequestInitializer)` method. For an example, please see "
108+
+ "https://github.com/googleapis/google-api-java-client#batching.";
109+
110+
private static final Logger LOGGER = Logger.getLogger(BatchRequest.class.getName());
111+
97112
/** The URL where batch requests are sent. */
98-
private GenericUrl batchUrl = new GenericUrl("https://www.googleapis.com/batch");
113+
private GenericUrl batchUrl = new GenericUrl(GLOBAL_BATCH_ENDPOINT);
99114

100115
/** The request factory for connections to the server. */
101116
private final HttpRequestFactory requestFactory;
@@ -128,7 +143,10 @@ static class RequestInfo<T, E> {
128143
* @param transport The transport to use for requests
129144
* @param httpRequestInitializer The initializer to use when creating an {@link HttpRequest} or
130145
* {@code null} for none
146+
* @deprecated Please use AbstractGoogleClient#batch(HttpRequestInitializer) to instantiate your
147+
* batch request.
131148
*/
149+
@Deprecated
132150
public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer) {
133151
this.requestFactory = httpRequestInitializer == null
134152
? transport.createRequestFactory() : transport.createRequestFactory(httpRequestInitializer);
@@ -213,6 +231,13 @@ public int size() {
213231
public void execute() throws IOException {
214232
boolean retryAllowed;
215233
Preconditions.checkState(!requestInfos.isEmpty());
234+
235+
// Log a warning if the user is using the global batch endpoint. In the future, we can turn this
236+
// into a preconditions check.
237+
if (GLOBAL_BATCH_ENDPOINT.equals(this.batchUrl.toString())) {
238+
LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
239+
}
240+
216241
HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
217242
// NOTE: batch does not support gzip encoding
218243
HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor();

google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public final BatchRequest batch() {
232232
* @return newly created Batch request
233233
*/
234234
public final BatchRequest batch(HttpRequestInitializer httpRequestInitializer) {
235+
@SuppressWarnings("deprecated")
235236
BatchRequest batch =
236237
new BatchRequest(getRequestFactory().getTransport(), httpRequestInitializer);
237238
if (Strings.isNullOrEmpty(batchPath)) {

0 commit comments

Comments
 (0)