Skip to content

Commit 8cdc416

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 12adf4d commit 8cdc416

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
@@ -33,6 +33,8 @@
3333
import java.io.InputStream;
3434
import java.util.ArrayList;
3535
import java.util.List;
36+
import java.util.logging.Level;
37+
import java.util.logging.Logger;
3638

3739
/**
3840
* An instance of this class represents a single batch of requests.
@@ -42,7 +44,8 @@
4244
* </p>
4345
*
4446
* <pre>
45-
BatchRequest batch = new BatchRequest(transport, httpRequestInitializer);
47+
// client is a AbstractGoogleClient (e.g. com.google.api.services.books.Books)
48+
BatchRequest batch = client.batch(httpRequestInitializer);
4649
batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
4750
new BatchCallback&lt;Volumes, GoogleJsonErrorContainer&gt;() {
4851
@@ -96,8 +99,20 @@ public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) {
9699
@SuppressWarnings("deprecation")
97100
public final class BatchRequest {
98101

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

102117
/** The request factory for connections to the server. */
103118
private final HttpRequestFactory requestFactory;
@@ -130,7 +145,10 @@ static class RequestInfo<T, E> {
130145
* @param transport The transport to use for requests
131146
* @param httpRequestInitializer The initializer to use when creating an {@link HttpRequest} or
132147
* {@code null} for none
148+
* @deprecated Please use AbstractGoogleClient#batch(HttpRequestInitializer) to instantiate your
149+
* batch request.
133150
*/
151+
@Deprecated
134152
public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer) {
135153
this.requestFactory = httpRequestInitializer == null
136154
? transport.createRequestFactory() : transport.createRequestFactory(httpRequestInitializer);
@@ -215,6 +233,13 @@ public int size() {
215233
public void execute() throws IOException {
216234
boolean retryAllowed;
217235
Preconditions.checkState(!requestInfos.isEmpty());
236+
237+
// Log a warning if the user is using the global batch endpoint. In the future, we can turn this
238+
// into a preconditions check.
239+
if (GLOBAL_BATCH_ENDPOINT.equals(this.batchUrl.toString())) {
240+
LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
241+
}
242+
218243
HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
219244
// NOTE: batch does not support gzip encoding
220245
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)