Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST Client: Add Request object flavored methods #29623

Merged
merged 29 commits into from May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bbb82fe
Low level client
nik9000 Apr 2, 2018
1cf984f
WIP
nik9000 Apr 2, 2018
0de81af
Merge branch 'master' into rest_requests
nik9000 Apr 3, 2018
1098731
Revert "WIP"
nik9000 Apr 3, 2018
b5d04b6
Merge branch 'master' into rest_requests
nik9000 Apr 19, 2018
fa4f204
Update
nik9000 Apr 19, 2018
16191b3
Remove deprecateds
nik9000 Apr 19, 2018
7938724
WIP
nik9000 Apr 19, 2018
9067cf8
Merge branch 'master' into rest_requests
nik9000 Apr 19, 2018
9d4b4e3
Rework and drop some assertions
nik9000 Apr 19, 2018
e0788a0
Docs
nik9000 Apr 19, 2018
38be87d
Merge branch 'master' into rest_requests
nik9000 Apr 26, 2018
28bb2a6
Fixup words
nik9000 Apr 26, 2018
a75ffd2
Rename to RequestConverters
nik9000 Apr 26, 2018
3987664
Make parameters final and make clients add to it
nik9000 Apr 26, 2018
7ba9517
Cleanup
nik9000 Apr 26, 2018
2990265
Merge branch 'master' into rest_requests
nik9000 Apr 26, 2018
8f8bba0
Update merged code
nik9000 Apr 26, 2018
72bcab6
Ooops
nik9000 Apr 26, 2018
ed2975b
Woops, null has to work
nik9000 Apr 26, 2018
c733948
Merge branch 'master' into rest_requests
nik9000 Apr 30, 2018
2b2c75e
changelog
nik9000 Apr 30, 2018
b4b07b6
Drop deprecation. We'll deprecate in a followup
nik9000 Apr 30, 2018
c1cc93a
Cleanup
nik9000 Apr 30, 2018
8fe0704
Merge branch 'master' into rest_requests
nik9000 Apr 30, 2018
e61fb39
Fix changelog
nik9000 Apr 30, 2018
b1d5806
Merge branch 'master' into rest_requests
nik9000 May 1, 2018
4bbf45b
Merge branch 'master' into rest_requests
nik9000 May 1, 2018
cd2c786
Fixup changelog
nik9000 May 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -48,7 +48,7 @@ public final class ClusterClient {
*/
public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, Header... headers)
throws IOException {
return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, Request::clusterPutSettings,
return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, HighLevelRequests::clusterPutSettings,
ClusterUpdateSettingsResponse::fromXContent, emptySet(), headers);
}

Expand All @@ -60,7 +60,7 @@ public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest cl
*/
public void putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest,
ActionListener<ClusterUpdateSettingsResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, Request::clusterPutSettings,
restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, HighLevelRequests::clusterPutSettings,
ClusterUpdateSettingsResponse::fromXContent, listener, emptySet(), headers);
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.client;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
Expand Down Expand Up @@ -52,14 +51,9 @@
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -79,14 +73,15 @@ public void initClients() throws IOException {
final RestClient restClient = mock(RestClient.class);
restHighLevelClient = new CustomRestClient(restClient);

doAnswer(mock -> mockPerformRequest((Header) mock.getArguments()[4]))
doAnswer(inv -> mockPerformRequest(((Request) inv.getArguments()[0]).getHeaders()[0]))
.when(restClient)
.performRequest(eq(HttpGet.METHOD_NAME), eq(ENDPOINT), anyMapOf(String.class, String.class), anyObject(), anyVararg());
.performRequest(any(Request.class));

doAnswer(mock -> mockPerformRequestAsync((Header) mock.getArguments()[5], (ResponseListener) mock.getArguments()[4]))
doAnswer(inv -> mockPerformRequestAsync(
((Request) inv.getArguments()[0]).getHeaders()[0],
(ResponseListener) inv.getArguments()[1]))
.when(restClient)
.performRequestAsync(eq(HttpGet.METHOD_NAME), eq(ENDPOINT), anyMapOf(String.class, String.class),
any(HttpEntity.class), any(ResponseListener.class), anyVararg());
.performRequestAsync(any(Request.class), any(ResponseListener.class));
}
}

Expand Down Expand Up @@ -193,7 +188,7 @@ void customAndParseAsync(MainRequest mainRequest, ActionListener<MainResponse> l
}

Request toRequest(MainRequest mainRequest) throws IOException {
return new Request(HttpGet.METHOD_NAME, ENDPOINT, emptyMap(), null);
return new Request(HttpGet.METHOD_NAME, ENDPOINT);
}

MainResponse toResponse(Response response) throws IOException {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

187 changes: 187 additions & 0 deletions client/rest/src/main/java/org/elasticsearch/client/Request.java
@@ -0,0 +1,187 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client;

import org.apache.http.Header;
import org.apache.http.HttpEntity;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
* Request to Elasticsearch.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP request?

*/
public final class Request {
private static final Header[] NO_HEADERS = new Header[0];
private final String method;
private final String endpoint;

private Map<String, String> parameters = Collections.<String, String>emptyMap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: shall we also make the parameters final? We could expose an add method if we want to rather than set. not too sure myself though about this, do what you prefer, just an idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check on that!

private HttpEntity entity;
private Header[] headers = NO_HEADERS;
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory =
HttpAsyncResponseConsumerFactory.DEFAULT;

/**
* Create the {@linkplain Request}.
* @param method the HTTP method
* @param endpoint the path of the request (without scheme, host, port, or prefix)
*/
public Request(String method, String endpoint) {
this.method = Objects.requireNonNull(method, "method cannot be null");
this.endpoint = Objects.requireNonNull(endpoint, "endpoint cannot be null");
}

/**
* The HTTP method.
*/
public String getMethod() {
return method;
}

/**
* The path of the request (without scheme, host, port, or prefix).
*/
public String getEndpoint() {
return endpoint;
}

/**
* Set the query string parameters. Polite users will not manipulate the
* Map after setting it.
*/
public void setParameters(Map<String, String> parameters) {
this.parameters = Objects.requireNonNull(parameters, "parameters cannot be null");;
}

/**
* Query string parameters.
*/
public Map<String, String> getParameters() {
return parameters;
}

/**
* Set the body of the request. If not set or set to {@code null} then no
* body is sent with the request.
*/
public void setEntity(HttpEntity entity) {
this.entity = entity;
}

/**
* The body of the request. If {@code null} then no body
* is sent with the request.
*/
public HttpEntity getEntity() {
return entity;
}

/**
* Set the headers to attach to the request.
*/
public void setHeaders(Header... headers) {
Objects.requireNonNull(headers, "headers cannot be null");
for (Header header : headers) {
Objects.requireNonNull(header, "header cannot be null");
}
this.headers = headers;
}

/**
* Headers to attach to the request.
*/
public Header[] getHeaders() {
return headers;
}

/**
* set the {@link HttpAsyncResponseConsumerFactory} used to create one
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
* response body gets streamed from a non-blocking HTTP connection on the
* client side.
*/
public void setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) {
this.httpAsyncResponseConsumerFactory =
Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null");
}

/**
* The {@link HttpAsyncResponseConsumerFactory} used to create one
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
* response body gets streamed from a non-blocking HTTP connection on the
* client side.
*/
public HttpAsyncResponseConsumerFactory getHttpAsyncResponseConsumerFactory() {
return httpAsyncResponseConsumerFactory;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("Request{");
b.append("method='").append(method).append('\'');
b.append(", endpoint='").append(endpoint).append('\'');
if (false == parameters.isEmpty()) {
b.append(", params=").append(parameters);
}
if (entity != null) {
b.append(", entity=").append(entity);
}
if (headers.length > 0) {
b.append(", headers=");
for (int h = 0; h < headers.length; h++) {
if (h != 0) {
b.append(',');
}
b.append(headers[h].toString());
}
}
if (httpAsyncResponseConsumerFactory != HttpAsyncResponseConsumerFactory.DEFAULT) {
b.append(", consumerFactory=").append(httpAsyncResponseConsumerFactory);
}
return b.append('}').toString();
}

@Override
public boolean equals(Object obj) {
if (obj == null || (obj.getClass() != getClass())) {
return false;
}
if (obj == this) {
return true;
}

Request other = (Request) obj;
return method.equals(other.method)
&& endpoint.equals(other.endpoint)
&& parameters.equals(other.parameters)
&& Objects.equals(entity, other.entity)
&& Arrays.equals(headers, other.headers)
&& httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory);
}

@Override
public int hashCode() {
return Objects.hash(method, endpoint, parameters, entity, Arrays.hashCode(headers), httpAsyncResponseConsumerFactory);
}
}