Skip to content

Commit

Permalink
review: make PolarisApiClient serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
dimas-b committed Jan 6, 2025
1 parent 489325e commit bb4b6fb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,35 @@
*/
package org.apache.polaris.service.it.env;

import static java.util.concurrent.TimeUnit.MINUTES;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.ext.ContextResolver;
import java.io.Serializable;
import java.net.URI;
import org.apache.iceberg.rest.RESTSerializers;
import org.apache.polaris.core.admin.model.PrincipalWithCredentials;

public record PolarisApiClient(Client client, String realm, URI baseUri) {
public final class PolarisApiClient implements Serializable, AutoCloseable {

public static String REALM_HEADER = "realm";

private final URI baseUri;
private final String realm;

private transient Client client;

public PolarisApiClient(URI baseUri, String realm) {
this.baseUri = baseUri;
this.realm = realm;
}

public URI catalogApiEndpoint() {
return baseUri.resolve("api/catalog");
}
Expand All @@ -46,4 +67,44 @@ public AuthToken obtainToken(PrincipalWithCredentials principal) {
public ManagementApi managementApi(AuthToken token) {
return new ManagementApi(this, token, baseUri.resolve("api/management"));
}

public String realm() {
return realm;
}

public Client client() {
if (client != null) {
return client;
}

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.KebabCaseStrategy());
RESTSerializers.registerAll(mapper);

// Note: converting to lambda apparently breaks discovery/injections in the client below
//noinspection Convert2Lambda
ContextResolver<ObjectMapper> mapperProvider =
new ContextResolver<>() {
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
};

return client =
ClientBuilder.newBuilder()
.readTimeout(5, MINUTES)
.connectTimeout(1, MINUTES)
.register(mapperProvider)
.build();
}

@Override
public void close() throws Exception {
if (client != null) {
client.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@

import static java.util.concurrent.TimeUnit.*;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.ext.ContextResolver;
import java.util.ServiceLoader;
import org.apache.iceberg.rest.RESTSerializers;
import org.apache.polaris.service.it.env.AuthToken;
import org.apache.polaris.service.it.env.ClientCredentials;
import org.apache.polaris.service.it.env.PolarisApiClient;
Expand Down Expand Up @@ -99,37 +90,15 @@ private ExtensionContext classContext(ExtensionContext context) {

private static class Env implements CloseableResource {
private final Server server;
private final Client client;
private final PolarisApiClient client;

private Env(Server server) {
this.server = server;

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.KebabCaseStrategy());
RESTSerializers.registerAll(mapper);

// Note: converting to lambda apparently breaks discovery/injections in the client below
//noinspection Convert2Lambda
ContextResolver<ObjectMapper> mapperProvider =
new ContextResolver<>() {
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
};

this.client =
ClientBuilder.newBuilder()
.readTimeout(5, MINUTES)
.connectTimeout(1, MINUTES)
.register(mapperProvider)
.build();
this.client = new PolarisApiClient(server.baseUri(), server.realmId());
}

PolarisApiClient client() {
return new PolarisApiClient(client, server.realmId(), server.baseUri());
return client;
}

@Override
Expand Down

0 comments on commit bb4b6fb

Please sign in to comment.