Skip to content

Commit

Permalink
add builder method
Browse files Browse the repository at this point in the history
  • Loading branch information
adutra committed Jan 29, 2025
1 parent 1effdbd commit 429fb31
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ private RESTClient httpClient() {
if (null == client) {
synchronized (this) {
if (null == client) {
client = HTTPClient.builder(properties).uri(properties.get(URI)).build();
client =
HTTPClient.builder(properties)
.uri(properties.get(URI))
.withAuthSession(AuthSession.EMPTY)
.build();
}
}
}
Expand All @@ -85,7 +89,6 @@ private RESTClient httpClient() {

private LoadCredentialsResponse fetchCredentials() {
return httpClient()
.withAuthSession(AuthSession.EMPTY)
.get(
properties.get(URI),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ private RESTClient httpClient() {
HTTPClient.builder(properties())
.uri(baseSignerUri())
.withObjectMapper(S3ObjectMapper.mapper())
.build()
.withAuthSession(org.apache.iceberg.rest.auth.AuthSession.EMPTY);
.withAuthSession(org.apache.iceberg.rest.auth.AuthSession.EMPTY)
.build();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public static void beforeClass() {
HTTPClient.builder(properties)
.uri("http://localhost:" + mockServer.getLocalPort())
.withHeader(HttpHeaders.AUTHORIZATION, "Bearer existing_token")
.build()
.withAuthSession(AuthSession.EMPTY);
.withAuthSession(AuthSession.EMPTY)
.build();
}

@AfterAll
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/java/org/apache/iceberg/rest/HTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ private HTTPClient(
ObjectMapper objectMapper,
HttpRequestInterceptor requestInterceptor,
Map<String, String> properties,
HttpClientConnectionManager connectionManager) {
HttpClientConnectionManager connectionManager,
AuthSession session) {
this.baseUri = baseUri;
this.baseHeaders = baseHeaders;
this.mapper = objectMapper;
this.authSession = null;
this.authSession = session;

HttpClientBuilder clientBuilder = HttpClients.custom();

Expand Down Expand Up @@ -429,6 +430,7 @@ public static class Builder {
private ObjectMapper mapper = RESTObjectMapper.mapper();
private HttpHost proxy;
private CredentialsProvider proxyCredentialsProvider;
private AuthSession authSession;

private Builder(Map<String, String> properties) {
this.properties = properties;
Expand Down Expand Up @@ -478,6 +480,11 @@ public Builder withObjectMapper(ObjectMapper objectMapper) {
return this;
}

public Builder withAuthSession(AuthSession session) {
this.authSession = session;
return this;
}

public HTTPClient build() {
withHeader(CLIENT_VERSION_HEADER, IcebergBuild.fullVersion());
withHeader(CLIENT_GIT_COMMIT_SHORT_HEADER, IcebergBuild.gitCommitShortId());
Expand All @@ -501,7 +508,8 @@ public HTTPClient build() {
mapper,
interceptor,
properties,
configureConnectionManager(properties));
configureConnectionManager(properties),
authSession);
}
}
}
26 changes: 13 additions & 13 deletions core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class TestHTTPClient {
public static void beforeClass() {
mockServer = startClientAndServer(PORT);
restClient =
HTTPClient.builder(ImmutableMap.of()).uri(URI).build().withAuthSession(AuthSession.EMPTY);
HTTPClient.builder(ImmutableMap.of()).uri(URI).withAuthSession(AuthSession.EMPTY).build();
icebergBuildGitCommitShort = IcebergBuild.gitCommitShortId();
icebergBuildFullVersion = IcebergBuild.fullVersion();
}
Expand Down Expand Up @@ -145,8 +145,8 @@ public void testProxyServer() throws IOException {
HTTPClient.builder(ImmutableMap.of())
.uri(URI)
.withProxy("localhost", proxyPort)
.build()
.withAuthSession(AuthSession.EMPTY)) {
.withAuthSession(AuthSession.EMPTY)
.build()) {
String path = "v1/config";
HttpRequest mockRequest =
request("/" + path).withMethod(HttpMethod.HEAD.name().toUpperCase(Locale.ROOT));
Expand Down Expand Up @@ -191,19 +191,19 @@ public void testProxyAuthenticationFailure() throws IOException {
new AuthScope(proxy),
new UsernamePasswordCredentials(authorizedUsername, invalidPassword.toCharArray()));

try (ClientAndServer proxyServer =
startClientAndServer(
new Configuration()
.proxyAuthenticationUsername(authorizedUsername)
.proxyAuthenticationPassword(authorizedPassword),
proxyPort);
RESTClient clientWithProxy =
try (RESTClient clientWithProxy =
HTTPClient.builder(ImmutableMap.of())
.uri(URI)
.withProxy(proxyHostName, proxyPort)
.withProxyCredentialsProvider(credentialsProvider)
.build()
.withAuthSession(AuthSession.EMPTY)) {
.withAuthSession(AuthSession.EMPTY)
.build();
ClientAndServer proxyServer =
startClientAndServer(
new Configuration()
.proxyAuthenticationUsername(authorizedUsername)
.proxyAuthenticationPassword(authorizedPassword),
proxyPort)) {

ErrorHandler onError =
new ErrorHandler() {
Expand Down Expand Up @@ -296,7 +296,7 @@ public void testSocketTimeout() throws IOException {
String path = "socket/timeout/path";

try (HTTPClient client =
HTTPClient.builder(properties).uri(URI).build().withAuthSession(AuthSession.EMPTY)) {
HTTPClient.builder(properties).uri(URI).withAuthSession(AuthSession.EMPTY).build()) {
HttpRequest mockRequest =
request()
.withPath("/" + path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private OAuth2RefreshCredentialsHandler(Map<String, String> properties) {
@Override
public AccessToken refreshAccessToken() {
LoadCredentialsResponse response;
try (RESTClient client = httpClient().withAuthSession(AuthSession.EMPTY)) {
try (RESTClient client = httpClient()) {
response =
client.get(
properties.get(GCPProperties.GCS_OAUTH2_REFRESH_CREDENTIALS_ENDPOINT),
Expand Down Expand Up @@ -96,6 +96,7 @@ public static OAuth2RefreshCredentialsHandler create(Map<String, String> propert
private RESTClient httpClient() {
return HTTPClient.builder(properties)
.uri(properties.get(GCPProperties.GCS_OAUTH2_REFRESH_CREDENTIALS_ENDPOINT))
.withAuthSession(AuthSession.EMPTY)
.build();
}
}

0 comments on commit 429fb31

Please sign in to comment.