Search in sources :

Example 1 with Endpoint

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))));
}
Also used : FooRequest(org.opensearch.client.opensearch.experiments.api.FooRequest) Client(org.opensearch.client.opensearch.experiments.base.Client) Query(org.opensearch.client.opensearch.experiments.api.query2.Query) Endpoint(org.opensearch.client.transport.Endpoint) Bar(org.opensearch.client.opensearch.experiments.api.Bar) Test(org.junit.Test) IOException(java.io.IOException) FooResponse(org.opensearch.client.opensearch.experiments.api.FooResponse) TermsQuery(org.opensearch.client.opensearch.experiments.api.query2.TermsQuery) Collections(java.util.Collections) Endpoint(org.opensearch.client.transport.Endpoint) Query(org.opensearch.client.opensearch.experiments.api.query2.Query) TermsQuery(org.opensearch.client.opensearch.experiments.api.query2.TermsQuery) FooResponse(org.opensearch.client.opensearch.experiments.api.FooResponse) Client(org.opensearch.client.opensearch.experiments.base.Client) TermsQuery(org.opensearch.client.opensearch.experiments.api.query2.TermsQuery) Test(org.junit.Test)

Example 2 with Endpoint

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());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) TransportException(org.opensearch.client.transport.TransportException) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) Endpoint(org.opensearch.client.transport.Endpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) TransportException(org.opensearch.client.transport.TransportException) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IOException(java.io.IOException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) ResponseException(org.opensearch.client.ResponseException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

IOException (java.io.IOException)2 Endpoint (org.opensearch.client.transport.Endpoint)2 JsonParser (jakarta.json.stream.JsonParser)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 HttpEntity (org.apache.http.HttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 Test (org.junit.Test)1 ResponseException (org.opensearch.client.ResponseException)1 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)1 Bar (org.opensearch.client.opensearch.experiments.api.Bar)1 FooRequest (org.opensearch.client.opensearch.experiments.api.FooRequest)1 FooResponse (org.opensearch.client.opensearch.experiments.api.FooResponse)1 Query (org.opensearch.client.opensearch.experiments.api.query2.Query)1 TermsQuery (org.opensearch.client.opensearch.experiments.api.query2.TermsQuery)1 Client (org.opensearch.client.opensearch.experiments.base.Client)1 JsonEndpoint (org.opensearch.client.transport.JsonEndpoint)1 TransportException (org.opensearch.client.transport.TransportException)1 BooleanEndpoint (org.opensearch.client.transport.endpoints.BooleanEndpoint)1 MissingRequiredPropertyException (org.opensearch.client.util.MissingRequiredPropertyException)1