Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
899 additions
and 10 deletions.
- +2 −0 build.gradle
- +2 −0 gax-httpjson/BUILD.bazel
- +2 −0 gax-httpjson/build.gradle
- +10 −2 gax-httpjson/src/main/java/com/google/api/gax/httpjson/ApiMessageHttpResponseParser.java
- +41 −0 gax-httpjson/src/main/java/com/google/api/gax/httpjson/FieldsExtractor.java
- +17 −2 gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpRequestRunnable.java
- +13 −6 gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpResponseParser.java
- +123 −0 gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java
- +79 −0 gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageResponseParser.java
- +156 −0 gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoRestSerializer.java
- +50 −0 gax-httpjson/src/main/java/com/google/api/gax/httpjson/RestSerializationException.java
- +19 −0 gax-httpjson/src/test/java/com/google/api/gax/httpjson/HttpRequestRunnableTest.java
- +133 −0 gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatterTest.java
- +99 −0 gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoMessageResponseParserTest.java
- +145 −0 gax-httpjson/src/test/java/com/google/api/gax/httpjson/ProtoRestSerializerTest.java
- +8 −0 repositories.bzl
There are no files selected for viewing
This file contains 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 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 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 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 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
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.httpjson; | ||
|
||
import com.google.api.core.BetaApi; | ||
|
||
/** | ||
* A functional interface to be implemented for each request message to extract specific fields from | ||
* it. For advanced usage only. | ||
*/ | ||
@BetaApi | ||
public interface FieldsExtractor<RequestT, ParamsT> { | ||
ParamsT extract(RequestT request); | ||
} |
This file contains 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 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 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
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.httpjson; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.pathtemplate.PathTemplate; | ||
import com.google.protobuf.Message; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** Creates parts of a HTTP request from a protobuf message. */ | ||
@BetaApi | ||
public class ProtoMessageRequestFormatter<RequestT extends Message> | ||
implements HttpRequestFormatter<RequestT> { | ||
|
||
private final FieldsExtractor<RequestT, String> requestBodyExtractor; | ||
// Using of triple nested generics (which is not pretty) is predetermined by the | ||
// Map<String, List<String>> returned value type of the getQueryParamNames interface method | ||
// implemented by this class. | ||
private final FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor; | ||
private final PathTemplate pathTemplate; | ||
private final FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; | ||
|
||
private ProtoMessageRequestFormatter( | ||
FieldsExtractor<RequestT, String> requestBodyExtractor, | ||
FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor, | ||
PathTemplate pathTemplate, | ||
FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor) { | ||
this.requestBodyExtractor = requestBodyExtractor; | ||
this.queryParamsExtractor = queryParamsExtractor; | ||
this.pathTemplate = pathTemplate; | ||
this.pathVarsExtractor = pathVarsExtractor; | ||
} | ||
|
||
public static <RequestT extends Message> | ||
ProtoMessageRequestFormatter.Builder<RequestT> newBuilder() { | ||
return new Builder<>(); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public Map<String, List<String>> getQueryParamNames(RequestT apiMessage) { | ||
return queryParamsExtractor.extract(apiMessage); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public String getRequestBody(RequestT apiMessage) { | ||
return requestBodyExtractor.extract(apiMessage); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public String getPath(RequestT apiMessage) { | ||
return pathTemplate.instantiate(pathVarsExtractor.extract(apiMessage)); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public PathTemplate getPathTemplate() { | ||
return pathTemplate; | ||
} | ||
|
||
// This has class has compound setter methods (multiple arguments in setters), that is why not | ||
// using @AutoValue. | ||
public static class Builder<RequestT extends Message> { | ||
private FieldsExtractor<RequestT, String> requestBodyExtractor; | ||
private FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor; | ||
private String path; | ||
private FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; | ||
|
||
public Builder<RequestT> setRequestBodyExtractor( | ||
FieldsExtractor<RequestT, String> requestBodyExtractor) { | ||
this.requestBodyExtractor = requestBodyExtractor; | ||
return this; | ||
} | ||
|
||
public Builder<RequestT> setQueryParamsExtractor( | ||
FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor) { | ||
this.queryParamsExtractor = queryParamsExtractor; | ||
return this; | ||
} | ||
|
||
public Builder<RequestT> setPath( | ||
String path, FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor) { | ||
this.path = path; | ||
this.pathVarsExtractor = pathVarsExtractor; | ||
return this; | ||
} | ||
|
||
public ProtoMessageRequestFormatter<RequestT> build() { | ||
return new ProtoMessageRequestFormatter<>( | ||
requestBodyExtractor, queryParamsExtractor, PathTemplate.create(path), pathVarsExtractor); | ||
} | ||
} | ||
} |
This file contains 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
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.google.api.gax.httpjson; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.protobuf.Message; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** The implementation of {@link HttpResponseParser} which works with protobuf messages. */ | ||
@BetaApi | ||
public class ProtoMessageResponseParser<ResponseT extends Message> | ||
implements HttpResponseParser<ResponseT> { | ||
|
||
private final ResponseT defaultInstance; | ||
|
||
private ProtoMessageResponseParser(ResponseT defaultInstance) { | ||
this.defaultInstance = defaultInstance; | ||
} | ||
|
||
public static <RequestT extends Message> | ||
ProtoMessageResponseParser.Builder<RequestT> newBuilder() { | ||
return new ProtoMessageResponseParser.Builder<>(); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public ResponseT parse(InputStream httpContent) { | ||
return ProtoRestSerializer.<ResponseT>create() | ||
.fromJson(httpContent, StandardCharsets.UTF_8, defaultInstance.newBuilderForType()); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
@Override | ||
public String serialize(ResponseT response) { | ||
return ProtoRestSerializer.create().toJson(response); | ||
} | ||
|
||
// Convert to @AutoValue if this class gets more complicated | ||
public static class Builder<ResponseT extends Message> { | ||
private ResponseT defaultInstance; | ||
|
||
public Builder<ResponseT> setDefaultInstance(ResponseT defaultInstance) { | ||
this.defaultInstance = defaultInstance; | ||
return this; | ||
} | ||
|
||
public ProtoMessageResponseParser<ResponseT> build() { | ||
return new ProtoMessageResponseParser<>(defaultInstance); | ||
} | ||
} | ||
} |
Oops, something went wrong.