|
32 | 32 | import java.io.InputStream; |
33 | 33 | import java.util.ArrayList; |
34 | 34 | import java.util.List; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
35 | 37 |
|
36 | 38 | /** |
37 | 39 | * An instance of this class represents a single batch of requests. |
|
41 | 43 | * </p> |
42 | 44 | * |
43 | 45 | * <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); |
45 | 48 | batch.queue(volumesList, Volumes.class, GoogleJsonErrorContainer.class, |
46 | 49 | new BatchCallback<Volumes, GoogleJsonErrorContainer>() { |
47 | 50 |
|
@@ -94,8 +97,20 @@ public void onFailure(GoogleJsonErrorContainer e, HttpHeaders responseHeaders) { |
94 | 97 | */ |
95 | 98 | public final class BatchRequest { |
96 | 99 |
|
| 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 | + |
97 | 112 | /** 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); |
99 | 114 |
|
100 | 115 | /** The request factory for connections to the server. */ |
101 | 116 | private final HttpRequestFactory requestFactory; |
@@ -128,7 +143,10 @@ static class RequestInfo<T, E> { |
128 | 143 | * @param transport The transport to use for requests |
129 | 144 | * @param httpRequestInitializer The initializer to use when creating an {@link HttpRequest} or |
130 | 145 | * {@code null} for none |
| 146 | + * @deprecated Please use AbstractGoogleClient#batch(HttpRequestInitializer) to instantiate your |
| 147 | + * batch request. |
131 | 148 | */ |
| 149 | + @Deprecated |
132 | 150 | public BatchRequest(HttpTransport transport, HttpRequestInitializer httpRequestInitializer) { |
133 | 151 | this.requestFactory = httpRequestInitializer == null |
134 | 152 | ? transport.createRequestFactory() : transport.createRequestFactory(httpRequestInitializer); |
@@ -213,6 +231,13 @@ public int size() { |
213 | 231 | public void execute() throws IOException { |
214 | 232 | boolean retryAllowed; |
215 | 233 | 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 | + |
216 | 241 | HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null); |
217 | 242 | // NOTE: batch does not support gzip encoding |
218 | 243 | HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor(); |
|
0 commit comments