Search in sources :

Example 1 with HeaderProvider

use of com.google.api.gax.rpc.HeaderProvider in project java-bigtable by googleapis.

the class HeadersTest method setUp.

@Before
public void setUp() throws Exception {
    serviceHelper = new FakeServiceHelper(new MetadataInterceptor(), new FakeBigtableService());
    serviceHelper.start();
    BigtableDataSettings.Builder settings = BigtableDataSettings.newBuilderForEmulator(serviceHelper.getPort()).setProjectId(PROJECT_ID).setInstanceId(INSTANCE_ID).setAppProfileId(APP_PROFILE_ID);
    HeaderProvider headerProvider = FixedHeaderProvider.create(TEST_FIXED_HEADER_STRING, "test_header_value");
    // Force immediate flush
    settings.stubSettings().setHeaderProvider(headerProvider).bulkMutateRowsSettings().setBatchingSettings(settings.stubSettings().bulkMutateRowsSettings().getBatchingSettings().toBuilder().setElementCountThreshold(1L).build());
    client = BigtableDataClient.create(settings.build());
}
Also used : FakeServiceHelper(com.google.cloud.bigtable.data.v2.FakeServiceHelper) HeaderProvider(com.google.api.gax.rpc.HeaderProvider) FixedHeaderProvider(com.google.api.gax.rpc.FixedHeaderProvider) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings) Before(org.junit.Before)

Example 2 with HeaderProvider

use of com.google.api.gax.rpc.HeaderProvider in project java-storage-nio by googleapis.

the class StorageOptionsUtilTest method ensureAddingUserAgentEntryDoesNotClobberAnyProvidedHeaders.

@Test
public void ensureAddingUserAgentEntryDoesNotClobberAnyProvidedHeaders() {
    StorageOptions base = defaults().toBuilder().setHeaderProvider(FixedHeaderProvider.create("user-agent", "custom/", "x-custom-header", "value")).build();
    StorageOptions merged = StorageOptionsUtil.mergeOptionsWithUserAgent(base);
    String mergedUserAgent = merged.getUserAgent();
    HeaderProvider mergedHeaderProvider = StorageOptionsUtil.getHeaderProvider(merged);
    Map<String, String> headers = mergedHeaderProvider.getHeaders();
    String customHeader = headers.get("x-custom-header");
    assertNotNull(mergedUserAgent);
    assertNotNull(customHeader);
    assertEquals("value", customHeader);
    assertTrue(mergedUserAgent.contains("custom/ gcloud-java-nio/"));
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions) HeaderProvider(com.google.api.gax.rpc.HeaderProvider) FixedHeaderProvider(com.google.api.gax.rpc.FixedHeaderProvider) Test(org.junit.Test)

Example 3 with HeaderProvider

use of com.google.api.gax.rpc.HeaderProvider in project java-storage-nio by googleapis.

the class StorageOptionsUtil method mergeOptionsWithUserAgent.

static StorageOptions mergeOptionsWithUserAgent(StorageOptions providedStorageOptions) {
    if (providedStorageOptions == DEFAULT_STORAGE_OPTIONS_INSTANCE) {
        return providedStorageOptions;
    }
    String userAgent = providedStorageOptions.getUserAgent();
    if (userAgent == null) {
        return nullSafeSet(providedStorageOptions, DEFAULT_HEADER_PROVIDER);
    } else {
        if (!userAgent.contains(USER_AGENT_ENTRY_NAME)) {
            HeaderProvider providedHeaderProvider = getHeaderProvider(providedStorageOptions);
            Map<String, String> newHeaders = new HashMap<>(providedHeaderProvider.getHeaders());
            newHeaders.put("user-agent", String.format("%s %s", userAgent, USER_AGENT_ENTRY));
            FixedHeaderProvider headerProvider = FixedHeaderProvider.create(ImmutableMap.copyOf(newHeaders));
            return nullSafeSet(providedStorageOptions, headerProvider);
        } else {
            return providedStorageOptions;
        }
    }
}
Also used : HashMap(java.util.HashMap) FixedHeaderProvider(com.google.api.gax.rpc.FixedHeaderProvider) HeaderProvider(com.google.api.gax.rpc.HeaderProvider) FixedHeaderProvider(com.google.api.gax.rpc.FixedHeaderProvider)

Example 4 with HeaderProvider

use of com.google.api.gax.rpc.HeaderProvider in project java-spanner by googleapis.

the class GapicSpannerRpcTest method testCustomUserAgent.

@Test
public void testCustomUserAgent() {
    for (String headerId : new String[] { "user-agent", "User-Agent", "USER-AGENT" }) {
        final HeaderProvider userAgentHeaderProvider = () -> {
            final Map<String, String> headers = new HashMap<>();
            headers.put(headerId, "test-agent");
            return headers;
        };
        final SpannerOptions options = createSpannerOptions().toBuilder().setHeaderProvider(userAgentHeaderProvider).build();
        try (Spanner spanner = options.getService()) {
            final DatabaseClient databaseClient = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
            try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
                rs.next();
            }
            assertThat(lastSeenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER))).contains("test-agent " + defaultUserAgent);
        }
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) HeaderProvider(com.google.api.gax.rpc.HeaderProvider) ResultSet(com.google.cloud.spanner.ResultSet) Map(java.util.Map) HashMap(java.util.HashMap) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Aggregations

HeaderProvider (com.google.api.gax.rpc.HeaderProvider)4 FixedHeaderProvider (com.google.api.gax.rpc.FixedHeaderProvider)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 BigtableDataSettings (com.google.cloud.bigtable.data.v2.BigtableDataSettings)1 FakeServiceHelper (com.google.cloud.bigtable.data.v2.FakeServiceHelper)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Spanner (com.google.cloud.spanner.Spanner)1 SpannerOptions (com.google.cloud.spanner.SpannerOptions)1 StorageOptions (com.google.cloud.storage.StorageOptions)1 Map (java.util.Map)1 Before (org.junit.Before)1