From 0267e16b6b75f5836a08e39b53c13faf03f953ec Mon Sep 17 00:00:00 2001 From: dyma solovei Date: Mon, 7 Jul 2025 15:28:38 +0200 Subject: [PATCH] chore: expose .custom factory method for WeaviateClient(Async) - 'scheme' is passed same as other parameters, via a Builder method - 'httpHost' and 'grpcHost' can tolerate optional https:// prefix --- .../io/weaviate/client6/v1/api/Config.java | 65 +++++++++++++------ .../client6/v1/api/WeaviateClient.java | 15 +++-- .../client6/v1/api/WeaviateClientAsync.java | 15 +++-- 3 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/weaviate/client6/v1/api/Config.java b/src/main/java/io/weaviate/client6/v1/api/Config.java index bd005d167..50abb2f02 100644 --- a/src/main/java/io/weaviate/client6/v1/api/Config.java +++ b/src/main/java/io/weaviate/client6/v1/api/Config.java @@ -19,8 +19,8 @@ public record Config( Map headers, TokenProvider tokenProvider) { - public static Config of(String scheme, Function> fn) { - return fn.apply(new Custom(scheme)).build(); + public static Config of(Function> fn) { + return fn.apply(new Custom()).build(); } public Config(Builder builder) { @@ -43,8 +43,7 @@ public GrpcChannelOptions grpcTransportOptions() { } abstract static class Builder> implements ObjectBuilder { - // Required parameters; - protected final String scheme; + protected String scheme; protected String httpHost; protected int httpPort; @@ -53,8 +52,27 @@ abstract static class Builder> implements ObjectBuild protected TokenProvider tokenProvider; protected Map 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") @@ -93,15 +111,15 @@ public Config build() { public static class Local extends Builder { 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; } @@ -117,29 +135,36 @@ public Local grpcPort(int port) { } public static class WeaviateCloud extends Builder { - 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 { - 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: + * 443 for HTTPS connection and 80 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; } @@ -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; } diff --git a/src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java b/src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java index 7f41fbffc..4fd1728f7 100644 --- a/src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java +++ b/src/main/java/io/weaviate/client6/v1/api/WeaviateClient.java @@ -37,20 +37,23 @@ public static WeaviateClient local() { } public static WeaviateClient local(Function> 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> 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> fn) { + return new WeaviateClient(fn.apply(new Config.Custom()).build()); + } + @Override public void close() throws IOException { this.restTransport.close(); diff --git a/src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java b/src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java index af7d7acc3..12ac88a3f 100644 --- a/src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java +++ b/src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java @@ -29,20 +29,23 @@ public static WeaviateClientAsync local() { } public static WeaviateClientAsync local(Function> 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> 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> fn) { + return new WeaviateClientAsync(Config.of(fn)); + } + @Override public void close() throws IOException { this.restTransport.close();