Skip to content

Commit 95331fc

Browse files
committed
feat: add x-goog-request-params to header (googleapis#940)
1 parent 577539f commit 95331fc

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/Datastore.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private DatastoreException invalidResponseException(String method, IOException e
6767
}
6868

6969
public AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreException {
70-
try (InputStream is = remoteRpc.call("allocateIds", request)) {
70+
try (InputStream is =
71+
remoteRpc.call("allocateIds", request, request.getProjectId(), request.getDatabaseId())) {
7172
return AllocateIdsResponse.parseFrom(is);
7273
} catch (IOException exception) {
7374
throw invalidResponseException("allocateIds", exception);
@@ -76,47 +77,54 @@ public AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws Datast
7677

7778
public BeginTransactionResponse beginTransaction(BeginTransactionRequest request)
7879
throws DatastoreException {
79-
try (InputStream is = remoteRpc.call("beginTransaction", request)) {
80+
try (InputStream is =
81+
remoteRpc.call(
82+
"beginTransaction", request, request.getProjectId(), request.getDatabaseId())) {
8083
return BeginTransactionResponse.parseFrom(is);
8184
} catch (IOException exception) {
8285
throw invalidResponseException("beginTransaction", exception);
8386
}
8487
}
8588

8689
public CommitResponse commit(CommitRequest request) throws DatastoreException {
87-
try (InputStream is = remoteRpc.call("commit", request)) {
90+
try (InputStream is =
91+
remoteRpc.call("commit", request, request.getProjectId(), request.getDatabaseId())) {
8892
return CommitResponse.parseFrom(is);
8993
} catch (IOException exception) {
9094
throw invalidResponseException("commit", exception);
9195
}
9296
}
9397

9498
public LookupResponse lookup(LookupRequest request) throws DatastoreException {
95-
try (InputStream is = remoteRpc.call("lookup", request)) {
99+
try (InputStream is =
100+
remoteRpc.call("lookup", request, request.getProjectId(), request.getDatabaseId())) {
96101
return LookupResponse.parseFrom(is);
97102
} catch (IOException exception) {
98103
throw invalidResponseException("lookup", exception);
99104
}
100105
}
101106

102107
public ReserveIdsResponse reserveIds(ReserveIdsRequest request) throws DatastoreException {
103-
try (InputStream is = remoteRpc.call("reserveIds", request)) {
108+
try (InputStream is =
109+
remoteRpc.call("reserveIds", request, request.getProjectId(), request.getDatabaseId())) {
104110
return ReserveIdsResponse.parseFrom(is);
105111
} catch (IOException exception) {
106112
throw invalidResponseException("reserveIds", exception);
107113
}
108114
}
109115

110116
public RollbackResponse rollback(RollbackRequest request) throws DatastoreException {
111-
try (InputStream is = remoteRpc.call("rollback", request)) {
117+
try (InputStream is =
118+
remoteRpc.call("rollback", request, request.getProjectId(), request.getDatabaseId())) {
112119
return RollbackResponse.parseFrom(is);
113120
} catch (IOException exception) {
114121
throw invalidResponseException("rollback", exception);
115122
}
116123
}
117124

118125
public RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreException {
119-
try (InputStream is = remoteRpc.call("runQuery", request)) {
126+
try (InputStream is =
127+
remoteRpc.call("runQuery", request, request.getProjectId(), request.getDatabaseId())) {
120128
return RunQueryResponse.parseFrom(is);
121129
} catch (IOException exception) {
122130
throw invalidResponseException("runQuery", exception);
@@ -125,7 +133,9 @@ public RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreExcept
125133

126134
public RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryRequest request)
127135
throws DatastoreException {
128-
try (InputStream is = remoteRpc.call("runAggregationQuery", request)) {
136+
try (InputStream is =
137+
remoteRpc.call(
138+
"runAggregationQuery", request, request.getProjectId(), request.getDatabaseId())) {
129139
return RunAggregationQueryResponse.parseFrom(is);
130140
} catch (IOException exception) {
131141
throw invalidResponseException("runAggregationQuery", exception);

datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/RemoteRpc.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.api.client.http.protobuf.ProtoHttpContent;
2525
import com.google.api.client.util.IOUtils;
2626
import com.google.common.annotations.VisibleForTesting;
27+
import com.google.common.base.Strings;
2728
import com.google.protobuf.MessageLite;
2829
import com.google.rpc.Code;
2930
import com.google.rpc.Status;
@@ -46,6 +47,8 @@ class RemoteRpc {
4647
@VisibleForTesting static final String API_FORMAT_VERSION_HEADER = "X-Goog-Api-Format-Version";
4748
private static final String API_FORMAT_VERSION = "2";
4849

50+
@VisibleForTesting static final String X_GOOG_REQUEST_PARAMS_HEADER = "x-goog-request-params";
51+
4952
private final HttpRequestFactory client;
5053
private final HttpRequestInitializer initializer;
5154
private final String url;
@@ -74,7 +77,9 @@ class RemoteRpc {
7477
*
7578
* @throws DatastoreException if the RPC fails.
7679
*/
77-
public InputStream call(String methodName, MessageLite request) throws DatastoreException {
80+
public InputStream call(
81+
String methodName, MessageLite request, String projectId, String databaseId)
82+
throws DatastoreException {
7883
logger.fine("remote datastore call " + methodName);
7984

8085
long startTime = System.currentTimeMillis();
@@ -84,7 +89,7 @@ public InputStream call(String methodName, MessageLite request) throws Datastore
8489
rpcCount.incrementAndGet();
8590
ProtoHttpContent payload = new ProtoHttpContent(request);
8691
HttpRequest httpRequest = client.buildPostRequest(resolveURL(methodName), payload);
87-
setHeaders(request, httpRequest);
92+
setHeaders(request, httpRequest, projectId, databaseId);
8893
// Don't throw an HTTPResponseException on error. It converts the response to a String and
8994
// throws away the original, whereas we need the raw bytes to parse it as a proto.
9095
httpRequest.setThrowExceptionOnExecuteError(false);
@@ -123,8 +128,16 @@ public InputStream call(String methodName, MessageLite request) throws Datastore
123128
}
124129

125130
@VisibleForTesting
126-
void setHeaders(MessageLite request, HttpRequest httpRequest) {
131+
void setHeaders(
132+
MessageLite request, HttpRequest httpRequest, String projectId, String databaseId) {
127133
httpRequest.getHeaders().put(API_FORMAT_VERSION_HEADER, API_FORMAT_VERSION);
134+
StringBuilder builder = new StringBuilder("project_id=");
135+
builder.append(projectId);
136+
if (!Strings.isNullOrEmpty(databaseId)) {
137+
builder.append("&database_id=");
138+
builder.append(databaseId);
139+
}
140+
httpRequest.getHeaders().put(X_GOOG_REQUEST_PARAMS_HEADER, builder.toString());
128141
if (enableE2EChecksum && request != null) {
129142
String checksum = EndToEndChecksumHandler.computeChecksum(request.toByteArray());
130143
if (checksum != null) {

datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public void testGzip() throws IOException, DatastoreException {
146146
new InjectedTestValues(gzip(response), new byte[1], true);
147147
RemoteRpc rpc = newRemoteRpc(injectedTestValues);
148148

149-
InputStream is = rpc.call("beginTransaction", BeginTransactionResponse.getDefaultInstance());
149+
InputStream is =
150+
rpc.call("beginTransaction", BeginTransactionResponse.getDefaultInstance(), "", "");
150151
BeginTransactionResponse parsedResponse = BeginTransactionResponse.parseFrom(is);
151152
is.close();
152153

@@ -159,14 +160,15 @@ public void testGzip() throws IOException, DatastoreException {
159160
public void testHttpHeaders_expectE2eChecksumHeader() throws IOException {
160161
// Enable E2E-Checksum system env variable
161162
RemoteRpc.setSystemEnvE2EChecksum(true);
163+
String projectId = "project-id";
162164
MessageLite request =
163-
RollbackRequest.newBuilder().setTransaction(ByteString.copyFromUtf8("project-id")).build();
165+
RollbackRequest.newBuilder().setTransaction(ByteString.copyFromUtf8(projectId)).build();
164166
RemoteRpc rpc =
165167
newRemoteRpc(
166168
new InjectedTestValues(gzip(newBeginTransactionResponse()), new byte[1], true));
167169
HttpRequest httpRequest =
168170
rpc.getClient().buildPostRequest(rpc.resolveURL("blah"), new ProtoHttpContent(request));
169-
rpc.setHeaders(request, httpRequest);
171+
rpc.setHeaders(request, httpRequest, projectId, "");
170172
assertNotNull(
171173
httpRequest.getHeaders().getFirstHeaderStringValue(RemoteRpc.API_FORMAT_VERSION_HEADER));
172174
// Expect to find e2e-checksum header
@@ -181,14 +183,15 @@ public void testHttpHeaders_expectE2eChecksumHeader() throws IOException {
181183
public void testHttpHeaders_doNotExpectE2eChecksumHeader() throws IOException {
182184
// disable E2E-Checksum system env variable
183185
RemoteRpc.setSystemEnvE2EChecksum(false);
186+
String projectId = "project-id";
184187
MessageLite request =
185-
RollbackRequest.newBuilder().setTransaction(ByteString.copyFromUtf8("project-id")).build();
188+
RollbackRequest.newBuilder().setTransaction(ByteString.copyFromUtf8(projectId)).build();
186189
RemoteRpc rpc =
187190
newRemoteRpc(
188191
new InjectedTestValues(gzip(newBeginTransactionResponse()), new byte[1], true));
189192
HttpRequest httpRequest =
190193
rpc.getClient().buildPostRequest(rpc.resolveURL("blah"), new ProtoHttpContent(request));
191-
rpc.setHeaders(request, httpRequest);
194+
rpc.setHeaders(request, httpRequest, projectId, "");
192195
assertNotNull(
193196
httpRequest.getHeaders().getFirstHeaderStringValue(RemoteRpc.API_FORMAT_VERSION_HEADER));
194197
// Do not expect to find e2e-checksum header
@@ -198,6 +201,38 @@ public void testHttpHeaders_doNotExpectE2eChecksumHeader() throws IOException {
198201
.getFirstHeaderStringValue(EndToEndChecksumHandler.HTTP_REQUEST_CHECKSUM_HEADER));
199202
}
200203

204+
@Test
205+
public void testHttpHeaders_prefixHeader() throws IOException {
206+
String projectId = "my-project";
207+
String databaseId = "my-db";
208+
MessageLite request =
209+
RollbackRequest.newBuilder()
210+
.setTransaction(ByteString.copyFromUtf8(projectId))
211+
.setDatabaseId(databaseId)
212+
.build();
213+
RemoteRpc rpc =
214+
newRemoteRpc(
215+
new InjectedTestValues(gzip(newBeginTransactionResponse()), new byte[1], true));
216+
HttpRequest httpRequest =
217+
rpc.getClient().buildPostRequest(rpc.resolveURL("blah"), new ProtoHttpContent(request));
218+
rpc.setHeaders(request, httpRequest, projectId, databaseId);
219+
assertEquals(
220+
"project_id=my-project&database_id=my-db",
221+
httpRequest.getHeaders().get(RemoteRpc.X_GOOG_REQUEST_PARAMS_HEADER));
222+
223+
MessageLite request2 =
224+
RollbackRequest.newBuilder().setTransaction(ByteString.copyFromUtf8(projectId)).build();
225+
RemoteRpc rpc2 =
226+
newRemoteRpc(
227+
new InjectedTestValues(gzip(newBeginTransactionResponse()), new byte[1], true));
228+
HttpRequest httpRequest2 =
229+
rpc2.getClient().buildPostRequest(rpc2.resolveURL("blah"), new ProtoHttpContent(request2));
230+
rpc2.setHeaders(request, httpRequest2, projectId, "");
231+
assertEquals(
232+
"project_id=my-project",
233+
httpRequest2.getHeaders().get(RemoteRpc.X_GOOG_REQUEST_PARAMS_HEADER));
234+
}
235+
201236
private static BeginTransactionResponse newBeginTransactionResponse() {
202237
return BeginTransactionResponse.newBuilder()
203238
.setTransaction(ByteString.copyFromUtf8("blah-blah-blah"))

0 commit comments

Comments
 (0)