-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bbb82fe
Low level client
nik9000 1cf984f
WIP
nik9000 0de81af
Merge branch 'master' into rest_requests
nik9000 1098731
Revert "WIP"
nik9000 b5d04b6
Merge branch 'master' into rest_requests
nik9000 fa4f204
Update
nik9000 16191b3
Remove deprecateds
nik9000 7938724
WIP
nik9000 9067cf8
Merge branch 'master' into rest_requests
nik9000 9d4b4e3
Rework and drop some assertions
nik9000 e0788a0
Docs
nik9000 38be87d
Merge branch 'master' into rest_requests
nik9000 28bb2a6
Fixup words
nik9000 a75ffd2
Rename to RequestConverters
nik9000 3987664
Make parameters final and make clients add to it
nik9000 7ba9517
Cleanup
nik9000 2990265
Merge branch 'master' into rest_requests
nik9000 8f8bba0
Update merged code
nik9000 72bcab6
Ooops
nik9000 ed2975b
Woops, null has to work
nik9000 c733948
Merge branch 'master' into rest_requests
nik9000 2b2c75e
changelog
nik9000 b4b07b6
Drop deprecation. We'll deprecate in a followup
nik9000 c1cc93a
Cleanup
nik9000 8fe0704
Merge branch 'master' into rest_requests
nik9000 e61fb39
Fix changelog
nik9000 b1d5806
Merge branch 'master' into rest_requests
nik9000 4bbf45b
Merge branch 'master' into rest_requests
nik9000 cd2c786
Fixup changelog
nik9000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
94 changes: 47 additions & 47 deletions
94
client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java
Large diffs are not rendered by default.
Oops, something went wrong.
353 changes: 172 additions & 181 deletions
353
...ava/org/elasticsearch/client/Request.java → ...asticsearch/client/RequestConverters.java
Large diffs are not rendered by default.
Oops, something went wrong.
72 changes: 41 additions & 31 deletions
72
client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
215 changes: 80 additions & 135 deletions
215
...rg/elasticsearch/client/RequestTests.java → ...search/client/RequestConvertersTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
102 changes: 23 additions & 79 deletions
102
client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
200 changes: 200 additions & 0 deletions
200
client/rest/src/main/java/org/elasticsearch/client/Request.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,200 @@ | ||
/* | ||
* 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 org.apache.http.nio.protocol.HttpAsyncResponseConsumer; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static java.util.Collections.unmodifiableMap; | ||
|
||
/** | ||
* HTTP Request to Elasticsearch. | ||
*/ | ||
public final class Request { | ||
private static final Header[] NO_HEADERS = new Header[0]; | ||
private final String method; | ||
private final String endpoint; | ||
private final Map<String, String> parameters = new HashMap<>(); | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* Add a query string parameter. | ||
* @throws IllegalArgumentException if a parameter with that name has already been set | ||
*/ | ||
public void addParameter(String name, String value) { | ||
Objects.requireNonNull(name, "url parameter name cannot be null"); | ||
Objects.requireNonNull(value, "url parameter value cannot be null"); | ||
// .putIfAbsent(name, value) except we are in Java 7 which doesn't have that. | ||
String oldValue = parameters.get(name); | ||
if (oldValue == null) { | ||
parameters.put(name, value); | ||
} else { | ||
throw new IllegalArgumentException("url parameter [" + name + "] has already been set to [" + oldValue + "]"); | ||
} | ||
} | ||
|
||
/** | ||
* Query string parameters. The returned map cannot be modifed but calls | ||
* to {@link #addParameter(String, String)} are viewable live in the map | ||
* but changes are not synchronized. | ||
*/ | ||
public Map<String, String> getParameters() { | ||
return unmodifiableMap(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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can shorten this comment and just say that the returned parameters are not modifiable? I don't quite get the last bit "changes are not synchronized". What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that the whole thing is not thread safe. I can probably drop that - folks should assume that everything that we don't explicitly document as thread safe is not thread safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I think it's implicit that this class, like pretty much every pojo, is not thread-safe