Skip to content

Commit 17f9a9f

Browse files
REST: Add property for configuring user agent in http client (#13234)
Co-authored-by: Prashant Singh <prashant.singh@snowflake.com>
1 parent 62d9ff5 commit 17f9a9f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

core/src/main/java/org/apache/iceberg/rest/HTTPClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class HTTPClient extends BaseHTTPClient {
8686
static final String REST_PROXY_PORT = "rest.client.proxy.port";
8787
static final String REST_PROXY_USERNAME = "rest.client.proxy.username";
8888
static final String REST_PROXY_PASSWORD = "rest.client.proxy.password";
89+
static final String REST_USER_AGENT = "rest.client.user-agent";
8990

9091
@VisibleForTesting
9192
static final String REST_CONNECTION_TIMEOUT_MS = "rest.client.connection-timeout-ms";
@@ -122,6 +123,11 @@ private HTTPClient(
122123
int maxRetries = PropertyUtil.propertyAsInt(properties, REST_MAX_RETRIES, 5);
123124
clientBuilder.setRetryStrategy(new ExponentialHttpRequestRetryStrategy(maxRetries));
124125

126+
String userAgent = PropertyUtil.propertyAsString(properties, REST_USER_AGENT, null);
127+
if (userAgent != null) {
128+
clientBuilder.setUserAgent(userAgent);
129+
}
130+
125131
if (proxy != null) {
126132
if (proxyCredsProvider != null) {
127133
clientBuilder.setDefaultCredentialsProvider(proxyCredsProvider);

core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public class TestHTTPClient {
7272

7373
private static final int PORT = 1080;
7474
private static final String BEARER_AUTH_TOKEN = "auth_token";
75+
private static final String USER_AGENT = "User-Agent";
76+
private static final String TEST_USER_AGENT = "Test-User-Agent";
7577
private static final String URI = String.format("http://127.0.0.1:%d", PORT);
7678
private static final ObjectMapper MAPPER = RESTObjectMapper.mapper();
7779

@@ -96,7 +98,10 @@ public static class TLSConfigurerMissingNoArgCtor implements TLSConfigurer {
9698
public static void beforeClass() {
9799
mockServer = startClientAndServer(PORT);
98100
restClient =
99-
HTTPClient.builder(ImmutableMap.of()).uri(URI).withAuthSession(AuthSession.EMPTY).build();
101+
HTTPClient.builder(ImmutableMap.of(HTTPClient.REST_USER_AGENT, TEST_USER_AGENT))
102+
.uri(URI)
103+
.withAuthSession(AuthSession.EMPTY)
104+
.build();
100105
icebergBuildGitCommitShort = IcebergBuild.gitCommitShortId();
101106
icebergBuildFullVersion = IcebergBuild.fullVersion();
102107
}
@@ -516,7 +521,8 @@ private static String addRequestTestCaseAndGetPath(HttpMethod method, Item body,
516521
.withMethod(method.name().toUpperCase(Locale.ROOT))
517522
.withHeader("Authorization", "Bearer " + BEARER_AUTH_TOKEN)
518523
.withHeader(HTTPClient.CLIENT_VERSION_HEADER, icebergBuildFullVersion)
519-
.withHeader(HTTPClient.CLIENT_GIT_COMMIT_SHORT_HEADER, icebergBuildGitCommitShort);
524+
.withHeader(HTTPClient.CLIENT_GIT_COMMIT_SHORT_HEADER, icebergBuildGitCommitShort)
525+
.withHeader(USER_AGENT, TEST_USER_AGENT);
520526

521527
if (method.usesRequestBody()) {
522528
mockRequest = mockRequest.withBody(asJson);

0 commit comments

Comments
 (0)