Skip to content

Commit

Permalink
cleaned up append token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ldetmer committed Sep 16, 2024
1 parent 5a918f4 commit 94d3f45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static String getRuntimeVersion() {
/**
* Returns the current library version as reported by {BUNDLE_VERSION_KEY} in library's
* META-INF/MANIFEST. This should only be used if MANIFEST file does not contain a widely
* recognized version declaration such as SPECIFIC_VERSION OR IMPLEMENTATION_VERSION, otherwise
* recognized version declaration such as Specific-Version OR Implementation-Version, otherwise
* please use #getLibraryVersion
*/
@VisibleForTesting
Expand All @@ -143,7 +143,7 @@ static Optional<String> getBundleVersion(Class<?> clazz) {
return Optional.ofNullable(attributes.getValue(BUNDLE_VERSION_KEY));
}
} catch (URISyntaxException | IOException e) {
// Unable to read Protobuf runtime version. Recover gracefully.
// Unable to read Bundle-Version from manifest. Recover gracefully.
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Implementation of HeaderProvider that provides headers describing the API client library making
Expand All @@ -52,7 +54,7 @@ public class ApiClientHeaderProvider implements HeaderProvider, Serializable {

protected ApiClientHeaderProvider(Builder builder) {
ImmutableMap.Builder<String, String> headersBuilder = ImmutableMap.builder();
tokensToAppendProfobufVersionTo = "(gccl|gapic).*";
// tokensToAppendProfobufVersionTo = "(gccl|gapic).*";

if (builder.getApiClientHeaderKey() != null) {
StringBuilder apiClientHeaderValue = new StringBuilder();
Expand All @@ -62,9 +64,12 @@ protected ApiClientHeaderProvider(Builder builder) {
appendToken(apiClientHeaderValue, builder.getGeneratedLibToken());
appendToken(apiClientHeaderValue, builder.getGeneratedRuntimeToken());
appendToken(apiClientHeaderValue, builder.getTransportToken());
appendToken(apiClientHeaderValue, builder.protobufRuntimeToken);
appendToken(apiClientHeaderValue, builder.getProtobufRuntimeToken());

if (apiClientHeaderValue.length() > 0) {
headersBuilder.put(builder.getApiClientHeaderKey(), apiClientHeaderValue.toString());
headersBuilder.put(
builder.getApiClientHeaderKey(),
checkAndAppendProtobufVersionIfNecessary(apiClientHeaderValue));
}
}

Expand All @@ -82,6 +87,20 @@ protected ApiClientHeaderProvider(Builder builder) {
this.headers = headersBuilder.build();
}

private static String checkAndAppendProtobufVersionIfNecessary(
StringBuilder apiClientHeaderValue) {
// TODO(b/366417603): appending protobuf version to existing client library token until resolved
// temporary fix while waiting for dedicated field to be added in concord
Pattern pattern = Pattern.compile("(gccl|gapic)\\S*");
Matcher matcher = pattern.matcher(apiClientHeaderValue);
if (matcher.find()) {
return apiClientHeaderValue.substring(0, matcher.end())
+ protobufVersionAppendValue
+ apiClientHeaderValue.substring(matcher.end());
}
return apiClientHeaderValue.toString();
}

@Override
public Map<String, String> getHeaders() {
return headers;
Expand All @@ -93,18 +112,6 @@ protected static void appendToken(StringBuilder sb, String token) {
sb.append(' ');
}
sb.append(token);
checkAndAppendProtobufVersionIfNecessary(sb, token);
}
}

private static void checkAndAppendProtobufVersionIfNecessary(StringBuilder sb, String token) {
// TODO(b/366417603): appending protobuf version to existing client library token until resolved
// temporary fix while waiting for dedicated field to be added in concord
if (token.matches(tokensToAppendProfobufVersionTo)) {
sb.append(protobufVersionAppendValue);
// once protobuf version as been appended to a token do not need to append to any additional
// tokens
tokensToAppendProfobufVersionTo = "";
}
}

Expand Down Expand Up @@ -243,6 +250,10 @@ public Builder setApiVersionToken(String apiVersionToken) {
return this;
}

public String getProtobufRuntimeToken() {
return protobufRuntimeToken;
}

private String constructToken(String name, String version) {
if (version == null) {
return null;
Expand Down

0 comments on commit 94d3f45

Please sign in to comment.