-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add set user agent sample (#2239)
* chore: add set user agent sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: add comments and fix variable names Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
01f826d
commit de313bd
Showing
3 changed files
with
141 additions
and
0 deletions.
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
61 changes: 61 additions & 0 deletions
61
samples/snippets/src/main/java/com/example/bigquery/SetUserAgent.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
// [START bigquery_set_user_agent] | ||
import com.google.api.gax.rpc.FixedHeaderProvider; | ||
import com.google.api.gax.rpc.HeaderProvider; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.io.IOException; | ||
|
||
public class SetUserAgent { | ||
|
||
private static final String USER_AGENT_HEADER = "user-agent"; | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String customUserAgentValue = "my-custom-user-agent-value"; | ||
setUserAgent(projectId, customUserAgentValue); | ||
} | ||
|
||
public static void setUserAgent(String projectId, String customUserAgentValue) | ||
throws IOException { | ||
// Setup the credentials | ||
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault(); | ||
|
||
// Initialize the HeaderProvider object with custom user agent value | ||
HeaderProvider headerProvider = | ||
FixedHeaderProvider.create(ImmutableMap.of(USER_AGENT_HEADER, customUserAgentValue)); | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. | ||
BigQuery bigQuery = | ||
BigQueryOptions.newBuilder() | ||
.setProjectId(projectId) | ||
.setCredentials(googleCredentials) | ||
.setHeaderProvider(headerProvider) | ||
.build() | ||
.getService(); | ||
|
||
System.out.println(bigQuery.getOptions().getUserAgent()); | ||
} | ||
} | ||
// [END bigquery_set_user_agent] |
79 changes: 79 additions & 0 deletions
79
samples/snippets/src/test/java/com/example/bigquery/SetUserAgentTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.bigquery; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class SetUserAgentTest { | ||
|
||
private final Logger log = Logger.getLogger(this.getClass().getName()); | ||
private String customUserAgentValue; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private PrintStream originalPrintStream; | ||
|
||
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
|
||
private static String requireEnvVar(String varName) { | ||
String value = System.getenv(varName); | ||
assertNotNull( | ||
"Environment variable " + varName + " is required to perform these tests.", | ||
System.getenv(varName)); | ||
return value; | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
customUserAgentValue = "CUSTOM_USER_AGENT_" + UUID.randomUUID().toString().substring(0, 8); | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
originalPrintStream = System.out; | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// Clean up | ||
// restores print statements in the original method | ||
System.out.flush(); | ||
System.setOut(originalPrintStream); | ||
log.log(Level.INFO, "\n" + bout.toString()); | ||
} | ||
|
||
@Test | ||
public void setUserAgentTest() throws IOException { | ||
SetUserAgent.setUserAgent(PROJECT_ID, customUserAgentValue); | ||
assertThat(bout.toString()).contains("CUSTOM_USER_AGENT_"); | ||
} | ||
} |