Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Add support for custom static headers #432

Merged
merged 22 commits into from
Dec 14, 2017
Merged

Conversation

vam-google
Copy link
Contributor

@vam-google vam-google commented Nov 28, 2017

  • Remove properties files for version detection (rely on metadata in MANIFEST.MF instead) - greatly simplifies everything, makes things more consistent and reliable.
  • Split custom and library-defined static headers configuration - prevents accidental overriding of intenal gapic headers by custom headers.
  • Support prepending User-Agent header with custom value (the only way User-Agent can be modified).
  • Redesign ApiClientHeaderProvider

@codecov-io
Copy link

codecov-io commented Nov 28, 2017

Codecov Report

Merging #432 into master will increase coverage by 1.24%.
The diff coverage is 83.76%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #432      +/-   ##
============================================
+ Coverage     69.11%   70.35%   +1.24%     
- Complexity      635      685      +50     
============================================
  Files           140      143       +3     
  Lines          3014     3097      +83     
  Branches        230      237       +7     
============================================
+ Hits           2083     2179      +96     
+ Misses          843      831      -12     
+ Partials         88       87       -1
Impacted Files Coverage Δ Complexity Δ
...ava/com/google/longrunning/OperationsSettings.java 75.23% <0%> (+3.41%) 8 <0> (ø) ⬇️
...api/gax/grpc/InstantiatingGrpcChannelProvider.java 40.17% <0%> (ø) 8 <0> (ø) ⬇️
...ogle/api/gax/httpjson/HttpJsonHeaderEnhancers.java 0% <0%> (ø) 0 <0> (ø) ⬇️
...va/com/google/api/gax/rpc/FixedHeaderProvider.java 100% <100%> (ø) 8 <8> (+6) ⬆️
...in/java/com/google/api/gax/rpc/ClientSettings.java 94.44% <100%> (+0.77%) 9 <1> (+1) ⬆️
...ain/java/com/google/api/gax/rpc/ClientContext.java 93.47% <100%> (+8.47%) 8 <0> (+2) ⬆️
...ava/com/google/api/gax/grpc/GaxGrpcProperties.java 90% <100%> (+30%) 5 <5> (+3) ⬆️
.../java/com/google/api/gax/rpc/NoHeaderProvider.java 50% <100%> (-50%) 1 <1> (-1)
...google/api/gax/httpjson/GaxHttpJsonProperties.java 50% <50%> (ø) 2 <2> (?)
...com/google/api/gax/grpc/GrpcHeaderInterceptor.java 80.76% <75%> (-4.24%) 4 <1> (+1)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3a13293...8cf21dd. Read the comment docs.

@vam-google
Copy link
Contributor Author

PTAL

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.1-all.zip

This comment was marked as spam.

This comment was marked as spam.

this.clientLibName = name;
this.clientLibVersion = version;
return this;
public String getJvmToken() {

This comment was marked as spam.

transportToken = null;

resourceHeaderKey = "google-cloud-resource-prefix";
resourceToken = null;

This comment was marked as spam.


/** Returns the current version of GAX. */
public static String getGaxVersion() {
String version = PropertiesProvider.loadProperty(gaxProperties, GAX_PROPERTY_FILE, "version");
return version != null ? version : DEFAULT_VERSION;
return GAX_VERSION;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

gax/build.gradle Outdated
classes {
dependsOn createProperties
}
}

This comment was marked as spam.

This comment was marked as spam.

@Test
public void testGaxGrpcVersion() {
String gaxGrpcVersion = GaxGrpcProperties.getGaxGrpcVersion();
assertNotNull(gaxGrpcVersion);

This comment was marked as spam.

This comment was marked as spam.

Metadata.Key.of(header.getKey(), Metadata.ASCII_STRING_MARSHALLER);

// User-Agent is overridden on gRPC level. The custom User-Agent is supposed to be provided
// differently and only merging with gRPC default value for User-Agent is permitted.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

classes {
dependsOn createProperties
}
}

This comment was marked as spam.

This comment was marked as spam.


/** Provides meta-data properties stored in a properties file. */
@BetaApi
public class PropertiesProvider {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import static com.google.common.truth.Truth.*;

This comment was marked as spam.

@rdayal
Copy link

rdayal commented Dec 7, 2017

Ping. Update on status here?

vam-google added a commit to vam-google/google-cloud-java that referenced this pull request Dec 8, 2017
Dependent on googleapis/gax-java#432

Usage:

// For manual/semi-manual clients:
//   Resource Manager
//   Translate
//   Compute
//   Storage
//   Datastore
//   Dns
//   BigQuery
//
//   Logging
//   Firestore
//   Spanner

HeaderProvider headerProvider =
    FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff");

LanguageServiceSettings settings =
    LanguageServiceSettings.newBuilder().setHeaderProvider(headerProvider).build();

LanguageServiceClient client = LanguageServiceClient.create(settings);

// For generated clients:
// All others, except Pub/Sub

HeaderProvider headerProvider =
    FixedHeaderProvider.create("user-agent", "my-client-name", "custom-header", "custom stuff");

DatastoreOptions options =
    DatastoreOptions.newBuilder().setHeaderProvider(headerProvider).build();

Datastore datastore = options.getService();

// Note, PubSub is supported only partially. Full support is dependent on migration of PubSub from Grpc stubs to Gapic stubs
@vam-google
Copy link
Contributor Author

@rdayal there the second part of this change (much bigger than expected originally): googleapis/google-cloud-java#2690

Copy link
Member

@garrettjonesgoogle garrettjonesgoogle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments on your latest iteration

public static class Builder extends ApiClientHeaderProvider.Builder {
public Builder() {
super();
setTransportToken("grpc", GaxGrpcProperties.getGrpcVersion());

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if (listElementType instanceof Class) {
headerValue = ImmutableList.of(fromString((Class) listElementType, value));
} else {
throw new IllegalArgumentException();

This comment was marked as spam.

This comment was marked as spam.


// It seems only String and Long have chances of being used. Not adding other
// classes to avoid having dead code, even though without them this looks incomplete.
throw new IllegalArgumentException();

This comment was marked as spam.

This comment was marked as spam.

import java.util.Map;
import org.junit.Test;

/*

This comment was marked as spam.

This comment was marked as spam.

return this;
}

public String getGeneratedLibToken() {
return generatedLibToken;
}

public Builder setGeneratedLibToken(String generatedLibToken) {
this.generatedLibToken = generatedLibToken;
public Builder setGeneratedLibToken(String name, String version) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@vam-google
Copy link
Contributor Author

@garrettjonesgoogle PTAL

@vam-google
Copy link
Contributor Author

@garrettjonesgoogle PTAL

* client library making API calls.
*/
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
public class GrpcClientHeaderProvider extends ApiClientHeaderProvider {

This comment was marked as spam.

This comment was marked as spam.

Metadata.Key.of(header.getKey(), Metadata.ASCII_STRING_MARSHALLER);

// User-Agent is overridden on gRPC level. The custom User-Agent is supposed to be provided
// differently and only merging with gRPC default value for User-Agent is permitted.

This comment was marked as spam.

return jvmToken;
}

public Builder setJvmToken(String version) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
return gapicVersion;
return GrpcClientHeaderProvider.newBuilder()
.setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(OperationsSettings.class));

This comment was marked as spam.

This comment was marked as spam.

return new LocalChannelProvider(new LocalAddress(addressString), null);
}

public boolean validateSentHeader(String headerKey, Pattern headerPattern) {

This comment was marked as spam.

This comment was marked as spam.

}

private static class LocalHeaderInterceptor implements ClientInterceptor {
private final ClientInterceptor delegate;

This comment was marked as spam.

This comment was marked as spam.

}

public static Builder newBuilder() {
return ApiClientHeaderProvider.newBuilder().setTransportToken("httpjson", "");

This comment was marked as spam.

This comment was marked as spam.

return generatedRuntimeToken;
}

public Builder setGeneratedRuntimeToken(String version) {

This comment was marked as spam.

}

if (builder.getResourceHeaderKey() != null && builder.getResourceToken() != null) {
headersBuilder.put(builder.getResourceHeaderKey(), builder.getResourceToken());

This comment was marked as spam.

This comment was marked as spam.

@@ -73,6 +73,9 @@
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
public abstract Map<String, String> getHeaders();

@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
protected abstract Map<String, String> getInternalHeaders();

This comment was marked as spam.

This comment was marked as spam.

@vam-google
Copy link
Contributor Author

PTAL

Copy link
Member

@garrettjonesgoogle garrettjonesgoogle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (of course, as usual, wait until the toolkit and google-cloud-java PRs are approved before merging this)

return jvmToken;
}

public Builder setJvmToken(String version) {

This comment was marked as spam.

Removed most of the scary reflection methods, since I'm not sure how performant they are (the setHeaders() method will be called for each request). The simplified version is pretty ugly and not generric, but should be ok for practical usage.
@vam-google vam-google merged commit f2e7142 into googleapis:master Dec 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants