use of org.opensearch.client.transport.Endpoint in project opensearch-java by opensearch-project.
the class ClientTests method testClient.
@Test
public void testClient() throws Exception {
Client client = new Client() {
@Override
protected <RequestT, ResponseT, ErrorT> ResponseT performRequest(RequestT request, Endpoint<RequestT, ResponseT, ErrorT> endpoint) throws IOException {
// don't care for now, we're testing compilation
return null;
}
};
client.foo(fb -> fb.name("z").value(1).routing("fooo").query(q -> q.bool(b -> b.add_must(m -> m.terms((TermsQuery) null))).meta(Collections.emptyMap())));
// Builders everywhere
FooResponse r1 = client.foo(FooRequest.builder().name("z").value(1).bar(Bar.builder().name("Raise the bar").build()).build());
// Illustrates creating an object outside of the client call
TermsQuery filter = TermsQuery.builder().field("foo").term("bar").build();
Query filter2 = new Query.Builder().terms(t -> t.field("foo").term("bar")).build();
// Fluid lambda-based builders
FooResponse r2 = client.foo(f -> f.name("z").value(1).bar(bar -> bar.name("Raise the bar")).query(q -> q.bool(b -> b.add_must(q1 -> q1.terms(filter)).add_must(q1 -> q1.terms(t -> t.field("a").term("b"))).add_must(q1 -> q1.terms(t -> t.field("a").term("b"))).minimumShouldMatch(2))));
}
use of org.opensearch.client.transport.Endpoint in project opensearch-java by opensearch-project.
the class RestClientTransport method getHighLevelResponse.
private <ResponseT, ErrorT> ResponseT getHighLevelResponse(org.opensearch.client.Response clientResp, Endpoint<?, ResponseT, ErrorT> endpoint) throws IOException {
try {
int statusCode = clientResp.getStatusLine().getStatusCode();
if (endpoint.isError(statusCode)) {
JsonpDeserializer<ErrorT> errorDeserializer = endpoint.errorDeserializer(statusCode);
if (errorDeserializer == null) {
throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp));
}
HttpEntity entity = clientResp.getEntity();
if (entity == null) {
throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
}
// We may have to replay it.
entity = new BufferedHttpEntity(entity);
try {
InputStream content = entity.getContent();
try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
ErrorT error = errorDeserializer.deserialize(parser, mapper);
// TODO: have the endpoint provide the exception constructor
throw new OpenSearchException((ErrorResponse) error);
}
} catch (MissingRequiredPropertyException errorEx) {
// Could not decode exception, try the response type
try {
ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint);
return response;
} catch (Exception respEx) {
// No better luck: throw the original error decoding exception
throw new TransportException("Failed to decode error response", new ResponseException(clientResp));
}
}
} else {
return decodeResponse(statusCode, clientResp.getEntity(), clientResp, endpoint);
}
} finally {
EntityUtils.consume(clientResp.getEntity());
}
}
Aggregations