Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions src/main/java/io/weaviate/client6/v1/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public record Config(
Map<String, String> headers,
TokenProvider tokenProvider) {

public static Config of(String scheme, Function<Custom, ObjectBuilder<Config>> fn) {
return fn.apply(new Custom(scheme)).build();
public static Config of(Function<Custom, ObjectBuilder<Config>> fn) {
return fn.apply(new Custom()).build();
}

public Config(Builder<?> builder) {
Expand All @@ -43,8 +43,7 @@ public GrpcChannelOptions grpcTransportOptions() {
}

abstract static class Builder<SELF extends Builder<SELF>> implements ObjectBuilder<Config> {
// Required parameters;
protected final String scheme;
protected String scheme;

protected String httpHost;
protected int httpPort;
Expand All @@ -53,8 +52,27 @@ abstract static class Builder<SELF extends Builder<SELF>> implements ObjectBuild
protected TokenProvider tokenProvider;
protected Map<String, String> headers = new HashMap<>();

protected Builder(String scheme) {
@SuppressWarnings("unchecked")
protected SELF scheme(String scheme) {
this.scheme = scheme;
return (SELF) this;
}

@SuppressWarnings("unchecked")
protected SELF httpHost(String httpHost) {
this.httpHost = trimScheme(httpHost);
return (SELF) this;
}

@SuppressWarnings("unchecked")
protected SELF grpcHost(String grpcHost) {
this.grpcHost = trimScheme(grpcHost);
return (SELF) this;
}

/** Remove leading http(s):// prefix from a URL, if present. */
private String trimScheme(String url) {
return url.replaceFirst("^https?\\/\\/", "");
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -93,15 +111,15 @@ public Config build() {

public static class Local extends Builder<Local> {
public Local() {
super("http");
scheme("http");
host("localhost");
httpPort(8080);
grpcPort(50051);
}

public Local host(String host) {
this.httpHost = host;
this.grpcHost = host;
httpHost(host);
grpcHost(host);
return this;
}

Expand All @@ -117,29 +135,36 @@ public Local grpcPort(int port) {
}

public static class WeaviateCloud extends Builder<WeaviateCloud> {
public WeaviateCloud(String clusterUrl, TokenProvider tokenProvider) {
this(URI.create(clusterUrl), tokenProvider);
public WeaviateCloud(String httpHost, TokenProvider tokenProvider) {
this(URI.create(httpHost), tokenProvider);
}

public WeaviateCloud(URI clusterUrl, TokenProvider tokenProvider) {
super("https");
this.httpHost = clusterUrl.getHost();
public WeaviateCloud(URI clusterUri, TokenProvider tokenProvider) {
scheme("https");
super.httpHost(clusterUri.getHost() != null
? clusterUri.getHost() // https://[example.com]/about
: clusterUri.getPath().split("/")[0]); // [example.com]/about
this.httpPort = 443;
this.grpcHost = "grpc-" + httpPort;
super.grpcHost("grpc-" + this.httpHost);
this.grpcPort = 443;
this.tokenProvider = tokenProvider;
}
}

public static class Custom extends Builder<Custom> {
public Custom(String scheme) {
super(scheme);
/**
* Scheme controls which protocol will be used for the database connection.
* REST and gRPC ports will be automatically inferred from it:
* <strong>443</strong> for HTTPS connection and <strong>80</strong> for HTTP.
*/
public Custom scheme(String scheme) {
httpPort(scheme == "https" ? 443 : 80);
grpcPort(scheme == "https" ? 443 : 80);
return super.scheme(scheme);
}

public Custom httpHost(String host) {
this.httpHost = host;
public Custom httpHost(String httpHost) {
super.httpHost(httpHost);
return this;
}

Expand All @@ -148,8 +173,8 @@ public Custom httpPort(int port) {
return this;
}

public Custom grpcHost(String host) {
this.grpcHost = host;
public Custom grpcHost(String grpcHost) {
super.grpcHost(grpcHost);
return this;
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ public static WeaviateClient local() {
}

public static WeaviateClient local(Function<Config.Local, ObjectBuilder<Config>> fn) {
var config = new Config.Local();
return new WeaviateClient(fn.apply(config).build());
return new WeaviateClient(fn.apply(new Config.Local()).build());
}

public static WeaviateClient wcd(String clusterUrl, String apiKey) {
return wcd(clusterUrl, apiKey, ObjectBuilder.identity());
public static WeaviateClient wcd(String httpHost, String apiKey) {
return wcd(httpHost, apiKey, ObjectBuilder.identity());
}

public static WeaviateClient wcd(String clusterUrl, String apiKey,
public static WeaviateClient wcd(String httpHost, String apiKey,
Function<Config.WeaviateCloud, ObjectBuilder<Config>> fn) {
var config = new Config.WeaviateCloud(clusterUrl, Authorization.apiKey(apiKey));
var config = new Config.WeaviateCloud(httpHost, Authorization.apiKey(apiKey));
return new WeaviateClient(fn.apply(config).build());
}

public static WeaviateClient custom(Function<Config.Custom, ObjectBuilder<Config>> fn) {
return new WeaviateClient(fn.apply(new Config.Custom()).build());
}

@Override
public void close() throws IOException {
this.restTransport.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ public static WeaviateClientAsync local() {
}

public static WeaviateClientAsync local(Function<Config.Local, ObjectBuilder<Config>> fn) {
var config = new Config.Local();
return new WeaviateClientAsync(fn.apply(config).build());
return new WeaviateClientAsync(fn.apply(new Config.Local()).build());
}

public static WeaviateClientAsync wcd(String clusterUrl, String apiKey) {
return wcd(clusterUrl, apiKey, ObjectBuilder.identity());
public static WeaviateClientAsync wcd(String httpHost, String apiKey) {
return wcd(httpHost, apiKey, ObjectBuilder.identity());
}

public static WeaviateClientAsync wcd(String clusterUrl, String apiKey,
public static WeaviateClientAsync wcd(String httpHost, String apiKey,
Function<Config.WeaviateCloud, ObjectBuilder<Config>> fn) {
var config = new Config.WeaviateCloud(clusterUrl, Authorization.apiKey(apiKey));
var config = new Config.WeaviateCloud(httpHost, Authorization.apiKey(apiKey));
return new WeaviateClientAsync(fn.apply(config).build());
}

public static WeaviateClientAsync custom(Function<Config.Custom, ObjectBuilder<Config>> fn) {
return new WeaviateClientAsync(Config.of(fn));
}

@Override
public void close() throws IOException {
this.restTransport.close();
Expand Down