From 243255bfafdfe91f400b62c30462eb304e309742 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 27 Jun 2019 09:07:38 -0700 Subject: [PATCH 1/6] Deprecate the BatchRequest public constructor --- .../com/google/api/client/googleapis/batch/BatchRequest.java | 3 +++ .../api/client/googleapis/services/AbstractGoogleClient.java | 1 + 2 files changed, 4 insertions(+) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java index 5095add2b..2565a3ee0 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java @@ -128,7 +128,10 @@ static class RequestInfo { * @param transport The transport to use for requests * @param httpRequestInitializer The initializer to use when creating an {@link HttpRequest} or * {@code null} for none + * @deprecated Please use AbstractGoogleClient#batch(HttpRequestInitializer) to instantiate your + * batch request. */ + @Deprecated public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer) { this.requestFactory = httpRequestInitializer == null ? transport.createRequestFactory() : transport.createRequestFactory(httpRequestInitializer); diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java index 02780d8fa..d182451fa 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java @@ -232,6 +232,7 @@ public final BatchRequest batch() { * @return newly created Batch request */ public final BatchRequest batch(HttpRequestInitializer httpRequestInitializer) { + @SuppressWarnings("deprecated") BatchRequest batch = new BatchRequest(getRequestFactory().getTransport(), httpRequestInitializer); if (Strings.isNullOrEmpty(batchPath)) { From 2bfea7b3a6528012c9fb6f3cd7f44d93dd6cc9f3 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 27 Jun 2019 09:08:25 -0700 Subject: [PATCH 2/6] Update Javadoc sample for batch request --- .../com/google/api/client/googleapis/batch/BatchRequest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java index 2565a3ee0..01214e646 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java @@ -41,7 +41,8 @@ *

* *
-   BatchRequest batch = new BatchRequest(transport, httpRequestInitializer);
+   // client is a AbstractGoogleClient (e.g. com.google.api.services.books.Books)
+   BatchRequest batch = client.batch(httpRequestInitializer);
    batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class,
        new BatchCallback<Volumes, GoogleJsonErrorContainer>() {
 

From 323c7bc9dcda43aa3dee0d03338674b3d5a7b1ba Mon Sep 17 00:00:00 2001
From: Jeff Ching 
Date: Thu, 27 Jun 2019 09:19:22 -0700
Subject: [PATCH 3/6] Log a warning if a user is using the global batch
 endpoint

---
 .../client/googleapis/batch/BatchRequest.java | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
index 01214e646..e546fa9b7 100644
--- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
+++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
@@ -32,6 +32,8 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * An instance of this class represents a single batch of requests.
@@ -95,8 +97,19 @@ public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) {
  */
 public final class BatchRequest {
 
+  /**
+   * The deprecated global batch endpoint. Users should actually use the per-service batch endpoint
+   * declared by the service configuration.
+   */
+  private static final String GLOBAL_BATCH_ENDPOINT = "https://www.googleapis.com/batch";
+  private static final String GLOBAL_BATCH_ENDPOINT_WARNING = "You are using the global batch "
+      + "endpoint which will soon be shut down. Please instantiate your BatchRequest via your "
+      + "service client's `batch(HttpRequestInitializer)` method.";
+
+  private static final Logger LOGGER = Logger.getLogger(BatchRequest.class.getName());
+
   /** The URL where batch requests are sent. */
-  private GenericUrl batchUrl = new GenericUrl("https://www.googleapis.com/batch");
+  private GenericUrl batchUrl = new GenericUrl(GLOBAL_BATCH_ENDPOINT);
 
   /** The request factory for connections to the server. */
   private final HttpRequestFactory requestFactory;
@@ -217,6 +230,13 @@ public int size() {
   public void execute() throws IOException {
     boolean retryAllowed;
     Preconditions.checkState(!requestInfos.isEmpty());
+
+    // Log a warning if the user is using the global batch endpoint. In the future, we can turn this
+    // into a preconditions check.
+    if (this.batchUrl.toString() == GLOBAL_BATCH_ENDPOINT) {
+      LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
+    }
+
     HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
     // NOTE: batch does not support gzip encoding
     HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor();

From b794af57a29fa010a78a487ba9a489ed1ceb232a Mon Sep 17 00:00:00 2001
From: Jeff Ching 
Date: Thu, 27 Jun 2019 09:29:56 -0700
Subject: [PATCH 4/6] Fix equality check for Strings

---
 .../com/google/api/client/googleapis/batch/BatchRequest.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
index e546fa9b7..f680f53e6 100644
--- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
+++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
@@ -233,7 +233,7 @@ public void execute() throws IOException {
 
     // Log a warning if the user is using the global batch endpoint. In the future, we can turn this
     // into a preconditions check.
-    if (this.batchUrl.toString() == GLOBAL_BATCH_ENDPOINT) {
+    if (this.batchUrl.toString().equals(GLOBAL_BATCH_ENDPOINT)) {
       LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
     }
 

From 74fef56e6719b17141265ac4c030a09c053b7436 Mon Sep 17 00:00:00 2001
From: Jeff Ching 
Date: Mon, 1 Jul 2019 09:54:51 -0700
Subject: [PATCH 5/6] Swap equals comparison to avoid possible NPE

---
 .../com/google/api/client/googleapis/batch/BatchRequest.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
index f680f53e6..6e606aeba 100644
--- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
+++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
@@ -233,7 +233,7 @@ public void execute() throws IOException {
 
     // Log a warning if the user is using the global batch endpoint. In the future, we can turn this
     // into a preconditions check.
-    if (this.batchUrl.toString().equals(GLOBAL_BATCH_ENDPOINT)) {
+    if (GLOBAL_BATCH_ENDPOINT.equals(this.batchUrl.toString())) {
       LOGGER.log(Level.WARNING, GLOBAL_BATCH_ENDPOINT_WARNING);
     }
 

From f99668fa072ede886bb378879410b7665dcce8e2 Mon Sep 17 00:00:00 2001
From: Jeff Ching 
Date: Mon, 1 Jul 2019 11:36:42 -0700
Subject: [PATCH 6/6] Add URL to visit in the warning message

---
 .../com/google/api/client/googleapis/batch/BatchRequest.java   | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
index 6e606aeba..b9e9d35bd 100644
--- a/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
+++ b/google-api-client/src/main/java/com/google/api/client/googleapis/batch/BatchRequest.java
@@ -104,7 +104,8 @@ public final class BatchRequest {
   private static final String GLOBAL_BATCH_ENDPOINT = "https://www.googleapis.com/batch";
   private static final String GLOBAL_BATCH_ENDPOINT_WARNING = "You are using the global batch "
       + "endpoint which will soon be shut down. Please instantiate your BatchRequest via your "
-      + "service client's `batch(HttpRequestInitializer)` method.";
+      + "service client's `batch(HttpRequestInitializer)` method. For an example, please see "
+      + "https://github.com/googleapis/google-api-java-client#batching.";
 
   private static final Logger LOGGER = Logger.getLogger(BatchRequest.class.getName());