-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(gax): allow non-JSON HttpContent and absolute request URLs in HttpRequestRunnable #14134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+142
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,26 +29,20 @@ | |
| */ | ||
| package com.google.api.gax.httpjson; | ||
|
|
||
| import com.google.api.client.http.EmptyContent; | ||
| import com.google.api.client.http.GenericUrl; | ||
| import com.google.api.client.http.HttpContent; | ||
| import com.google.api.client.http.HttpMediaType; | ||
| import com.google.api.client.http.HttpMethods; | ||
| import com.google.api.client.http.HttpRequest; | ||
| import com.google.api.client.http.HttpRequestFactory; | ||
| import com.google.api.client.http.HttpResponse; | ||
| import com.google.api.client.http.HttpResponseException; | ||
| import com.google.api.client.http.HttpTransport; | ||
| import com.google.api.client.http.json.JsonHttpContent; | ||
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.api.client.json.JsonObjectParser; | ||
| import com.google.api.client.json.gson.GsonFactory; | ||
| import com.google.api.client.util.GenericData; | ||
| import com.google.api.gax.tracing.ApiTracer; | ||
| import com.google.auth.Credentials; | ||
| import com.google.auth.http.HttpCredentialsAdapter; | ||
| import com.google.auto.value.AutoValue; | ||
| import com.google.common.base.Strings; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
@@ -154,8 +148,6 @@ public void run() { | |
| } | ||
|
|
||
| HttpRequest createHttpRequest() throws IOException { | ||
| GenericData tokenRequest = new GenericData(); | ||
|
|
||
| HttpRequestFormatter<RequestT> requestFormatter = methodDescriptor.getRequestFormatter(); | ||
|
|
||
| HttpRequestFactory requestFactory; | ||
|
|
@@ -166,24 +158,18 @@ HttpRequest createHttpRequest() throws IOException { | |
| requestFactory = httpTransport.createRequestFactory(); | ||
| } | ||
|
|
||
| JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); | ||
| // Create HTTP request body. | ||
| String requestBody = requestFormatter.getRequestBody(request); | ||
| HttpContent jsonHttpContent; | ||
| if (!Strings.isNullOrEmpty(requestBody)) { | ||
| jsonFactory.createJsonParser(requestBody).parse(tokenRequest); | ||
| jsonHttpContent = | ||
| new JsonHttpContent(jsonFactory, tokenRequest) | ||
| .setMediaType((new HttpMediaType("application/json; charset=utf-8"))); | ||
| } else { | ||
| // Force underlying HTTP lib to set Content-Length header to avoid 411s. | ||
| // See EmptyContent.java. | ||
| jsonHttpContent = new EmptyContent(); | ||
| } | ||
| HttpContent httpContent = requestFormatter.getHttpContent(request); | ||
|
|
||
| // Populate URL path and query parameters. | ||
| String normalizedEndpoint = normalizeEndpoint(endpoint); | ||
| GenericUrl url = new GenericUrl(normalizedEndpoint + requestFormatter.getPath(request)); | ||
| String path = requestFormatter.getPath(request); | ||
| GenericUrl url; | ||
| if (path.startsWith("http://") || path.startsWith("https://")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this for the upload URL that is returned from the start request?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's the use case for this. |
||
| url = new GenericUrl(path); | ||
| } else { | ||
| String normalizedEndpoint = normalizeEndpoint(endpoint); | ||
| url = new GenericUrl(normalizedEndpoint + path); | ||
| } | ||
| Map<String, List<String>> queryParams = requestFormatter.getQueryParamNames(request); | ||
| for (Entry<String, List<String>> queryParam : queryParams.entrySet()) { | ||
| if (queryParam.getValue() != null) { | ||
|
|
@@ -196,20 +182,20 @@ HttpRequest createHttpRequest() throws IOException { | |
| tracer.requestUrlResolved(url.build()); | ||
| } | ||
|
|
||
| HttpRequest httpRequest = buildRequest(requestFactory, url, jsonHttpContent); | ||
| HttpRequest httpRequest = buildRequest(requestFactory, url, httpContent); | ||
|
|
||
| for (Map.Entry<String, Object> entry : headers.getHeaders().entrySet()) { | ||
| HttpHeadersUtils.setHeader( | ||
| httpRequest.getHeaders(), entry.getKey(), (String) entry.getValue()); | ||
| } | ||
|
|
||
| httpRequest.setParser(new JsonObjectParser(jsonFactory)); | ||
| httpRequest.setParser(new JsonObjectParser(GsonFactory.getDefaultInstance())); | ||
|
|
||
| return httpRequest; | ||
| } | ||
|
|
||
| private HttpRequest buildRequest( | ||
| HttpRequestFactory requestFactory, GenericUrl url, HttpContent jsonHttpContent) | ||
| HttpRequestFactory requestFactory, GenericUrl url, HttpContent httpContent) | ||
| throws IOException { | ||
| // A workaround to support PATCH request. This assumes support of "X-HTTP-Method-Override" | ||
| // header on the server side, which GCP services usually do. | ||
|
|
@@ -235,7 +221,7 @@ private HttpRequest buildRequest( | |
| if (HttpMethods.PATCH.equals(actualHttpMethod)) { | ||
| actualHttpMethod = HttpMethods.POST; | ||
| } | ||
| HttpRequest httpRequest = requestFactory.buildRequest(actualHttpMethod, url, jsonHttpContent); | ||
| HttpRequest httpRequest = requestFactory.buildRequest(actualHttpMethod, url, httpContent); | ||
| if (originalHttpMethod != null && !originalHttpMethod.equals(actualHttpMethod)) { | ||
| HttpHeadersUtils.setHeader( | ||
| httpRequest.getHeaders(), "X-HTTP-Method-Override", originalHttpMethod); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect this method to be used by classes other than
HttpRequestRunnable? If not, I would prefer it to be a private helper method inHttpRequestRunnableinstead of a public method in this interface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only caller is expected to be
HttpRequestRunnable, but the point of this being a method on the formatter interface is for formatters that process binary message bodies (e.g. for chunk uploads) to be able to override this to be not-JSON. See e.g. in the unit test for this functionality.If this JSON implementation were a private helper in
HttpRequestRunnablethen IIUC the special case logic for the binary case would have to live there as well (e.g. switching behavior based on instanceof the request). IMO it's cleaner for any special casing required for a particular formatter to live in that formatter. I can play around with alternative ways to express that differently if you feel strongly though.