-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from weaviate/java-async-client-poc
Async client PoC
- Loading branch information
Showing
39 changed files
with
2,116 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/io/weaviate/client/base/AsyncBaseClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.weaviate.client.base; | ||
|
||
import io.weaviate.client.Config; | ||
import io.weaviate.client.base.http.async.ResponseParser; | ||
import io.weaviate.client.base.http.async.WeaviateResponseConsumer; | ||
import java.util.concurrent.Future; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpHeaders; | ||
|
||
public abstract class AsyncBaseClient<T> { | ||
private final CloseableHttpAsyncClient client; | ||
private final Config config; | ||
private final Serializer serializer; | ||
|
||
public AsyncBaseClient(CloseableHttpAsyncClient client, Config config) { | ||
this.client = client; | ||
this.config = config; | ||
this.serializer = new Serializer(); | ||
} | ||
|
||
protected Future<Result<T>> sendGetRequest(String endpoint, Class<T> classOfT, FutureCallback<Result<T>> callback) { | ||
return sendRequest(endpoint, null, "GET", classOfT, callback, null); | ||
} | ||
|
||
protected Future<Result<T>> sendGetRequest(String endpoint, FutureCallback<Result<T>> callback, ResponseParser<T> parser) { | ||
return sendRequest(endpoint, null, "GET", null, callback, parser); | ||
} | ||
|
||
protected Future<Result<T>> sendPostRequest(String endpoint, Object payload, Class<T> classOfT, FutureCallback<Result<T>> callback) { | ||
return sendRequest(endpoint, payload, "POST", classOfT, callback, null); | ||
} | ||
|
||
protected Future<Result<T>> sendPostRequest(String endpoint, Object payload, FutureCallback<Result<T>> callback, ResponseParser<T> parser) { | ||
return sendRequest(endpoint, payload, "POST", null, callback, parser); | ||
} | ||
|
||
protected Future<Result<T>> sendPutRequest(String endpoint, Object payload, Class<T> classOfT, FutureCallback<Result<T>> callback) { | ||
return sendRequest(endpoint, payload, "PUT", classOfT, callback, null); | ||
} | ||
|
||
protected Future<Result<T>> sendPutRequest(String endpoint, Object payload, FutureCallback<Result<T>> callback, ResponseParser<T> parser) { | ||
return sendRequest(endpoint, payload, "PUT", null, callback, parser); | ||
} | ||
|
||
protected Future<Result<T>> sendDeleteRequest(String endpoint, Object payload, Class<T> classOfT, FutureCallback<Result<T>> callback) { | ||
return sendRequest(endpoint, payload, "DELETE", classOfT, callback, null); | ||
} | ||
|
||
protected Future<Result<T>> sendDeleteRequest(String endpoint, Object payload, FutureCallback<Result<T>> callback, ResponseParser<T> parser) { | ||
return sendRequest(endpoint, payload, "DELETE", null, callback, parser); | ||
} | ||
|
||
protected Future<Result<T>> sendHeadRequest(String endpoint, Class<T> classOfT, FutureCallback<Result<T>> callback) { | ||
return sendRequest(endpoint, null, "HEAD", classOfT, callback, null); | ||
} | ||
|
||
protected Future<Result<T>> sendHeadRequest(String endpoint, FutureCallback<Result<T>> callback, ResponseParser<T> parser) { | ||
return sendRequest(endpoint, null, "HEAD", null, callback, parser); | ||
} | ||
|
||
private Future<Result<T>> sendRequest(String endpoint, Object payload, String method, Class<T> classOfT, FutureCallback<Result<T>> callback, | ||
ResponseParser<T> parser) { | ||
SimpleHttpRequest req = new SimpleHttpRequest(method, String.format("%s%s", config.getBaseURL(), endpoint)); | ||
req.addHeader(HttpHeaders.ACCEPT, "*/*"); | ||
req.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); | ||
if (payload != null) { | ||
req.setBody(serializer.toJsonString(payload), ContentType.APPLICATION_JSON); | ||
} | ||
return client.execute(SimpleRequestProducer.create(req), new WeaviateResponseConsumer<>(classOfT, parser), callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.weaviate.client.base; | ||
|
||
import java.util.concurrent.Future; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
|
||
public interface AsyncClientResult<T> { | ||
Future<Result<T>> run(); | ||
Future<Result<T>> run(FutureCallback<Result<T>> callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
package io.weaviate.client.base; | ||
|
||
import io.weaviate.client.v1.graphql.model.GraphQLResponse; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.experimental.FieldDefaults; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) | ||
public class Response<T> { | ||
int statusCode; | ||
T body; | ||
WeaviateErrorResponse errors; | ||
|
||
public Response(int statusCode, T body, WeaviateErrorResponse errors) { | ||
this.statusCode = statusCode; | ||
this.body = body; | ||
if (body instanceof GraphQLResponse) { | ||
this.errors = getWeaviateGraphQLErrorResponse((GraphQLResponse) body, statusCode);; | ||
} else { | ||
this.errors = errors; | ||
} | ||
} | ||
|
||
/** | ||
* Extract errors from {@link WeaviateErrorResponse} from a GraphQL response body. | ||
* | ||
* @param gql GraphQL response body. | ||
* @param code HTTP status code to pass in the {@link WeaviateErrorResponse}. | ||
* @return Error response to be returned to the caller. | ||
*/ | ||
private WeaviateErrorResponse getWeaviateGraphQLErrorResponse(GraphQLResponse gql, int code) { | ||
List<WeaviateErrorMessage> messages = gql.errorMessages(); | ||
if (messages == null || messages.isEmpty()) { | ||
return null; | ||
} | ||
return WeaviateErrorResponse.builder().code(code).error(gql.errorMessages()).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/io/weaviate/client/base/http/async/AsyncHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.weaviate.client.base.http.async; | ||
|
||
import io.weaviate.client.Config; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients; | ||
import org.apache.hc.core5.reactor.IOReactorConfig; | ||
import org.apache.hc.core5.util.Timeout; | ||
|
||
public class AsyncHttpClient { | ||
|
||
public static CloseableHttpAsyncClient create(Config config) { | ||
IOReactorConfig ioReactorConfig = IOReactorConfig.custom() | ||
.setSoTimeout(Timeout.ofSeconds(config.getSocketTimeout())) | ||
.build(); | ||
|
||
return HttpAsyncClients.custom() | ||
.setIOReactorConfig(ioReactorConfig) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/weaviate/client/base/http/async/ResponseParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.weaviate.client.base.http.async; | ||
|
||
import io.weaviate.client.base.Result; | ||
import io.weaviate.client.base.Serializer; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
|
||
public abstract class ResponseParser<T> { | ||
protected final Serializer serializer; | ||
|
||
public ResponseParser() { | ||
this.serializer = new Serializer(); | ||
} | ||
|
||
public abstract Result<T> parse(HttpResponse response, String body, ContentType contentType); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/io/weaviate/client/base/http/async/WeaviateResponseConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.weaviate.client.base.http.async; | ||
|
||
import io.weaviate.client.base.Result; | ||
import io.weaviate.client.base.Serializer; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.HttpResponse; | ||
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer; | ||
import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
|
||
public class WeaviateResponseConsumer<T> extends AbstractAsyncResponseConsumer<Result<T>, byte[]> { | ||
private final Serializer serializer; | ||
private final Class<T> classOfT; | ||
private final ResponseParser<T> parser; | ||
|
||
public WeaviateResponseConsumer(Class<T> classOfT, ResponseParser<T> parser) { | ||
super(new BasicAsyncEntityConsumer()); | ||
this.serializer = new Serializer(); | ||
this.classOfT = classOfT; | ||
this.parser = parser; | ||
} | ||
|
||
@Override | ||
protected Result<T> buildResult(HttpResponse response, byte[] entity, ContentType contentType) { | ||
String body = new String(entity, StandardCharsets.UTF_8); | ||
if (this.parser != null) { | ||
return this.parser.parse(response, body, contentType); | ||
} | ||
return serializer.toResult(response.getCode(), body, classOfT); | ||
} | ||
|
||
@Override | ||
public void informationResponse(HttpResponse response, HttpContext context) throws HttpException, IOException { | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/io/weaviate/client/v1/async/WeaviateAsyncClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.weaviate.client.v1.async; | ||
|
||
import io.weaviate.client.Config; | ||
import io.weaviate.client.base.http.async.AsyncHttpClient; | ||
import io.weaviate.client.v1.async.misc.Misc; | ||
import io.weaviate.client.v1.async.schema.Schema; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.core5.io.CloseMode; | ||
|
||
public class WeaviateAsyncClient implements AutoCloseable { | ||
private final Config config; | ||
private final CloseableHttpAsyncClient client; | ||
|
||
public WeaviateAsyncClient(Config config) { | ||
this.config = config; | ||
this.client = AsyncHttpClient.create(config); | ||
// auto start the client | ||
this.start(); | ||
} | ||
|
||
public Misc misc() { | ||
return new Misc(client, config); | ||
} | ||
|
||
public Schema schema() { | ||
return new Schema(client, config); | ||
} | ||
|
||
private void start() { | ||
this.client.start(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
this.client.close(CloseMode.GRACEFUL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.weaviate.client.v1.async.misc; | ||
|
||
import io.weaviate.client.Config; | ||
import io.weaviate.client.v1.async.misc.api.LiveChecker; | ||
import io.weaviate.client.v1.async.misc.api.MetaGetter; | ||
import io.weaviate.client.v1.async.misc.api.OpenIDConfigGetter; | ||
import io.weaviate.client.v1.async.misc.api.ReadyChecker; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
|
||
public class Misc { | ||
private final CloseableHttpAsyncClient client; | ||
private final Config config; | ||
|
||
public Misc(CloseableHttpAsyncClient client, Config config) { | ||
this.client = client; | ||
this.config = config; | ||
} | ||
|
||
public MetaGetter metaGetter() { | ||
return new MetaGetter(client, config); | ||
} | ||
|
||
public OpenIDConfigGetter openIDConfigGetter() { | ||
return new OpenIDConfigGetter(client, config); | ||
} | ||
|
||
public LiveChecker liveChecker() { | ||
return new LiveChecker(client, config); | ||
} | ||
|
||
public ReadyChecker readyChecker() { | ||
return new ReadyChecker(client, config); | ||
} | ||
} |
Oops, something went wrong.