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
Changes from 11 commits
bbb82fe
1cf984f
0de81af
1098731
b5d04b6
fa4f204
16191b3
7938724
9067cf8
9d4b4e3
e0788a0
38be87d
28bb2a6
a75ffd2
3987664
7ba9517
2990265
8f8bba0
72bcab6
ed2975b
c733948
2b2c75e
b4b07b6
c1cc93a
8fe0704
e61fb39
b1d5806
4bbf45b
cd2c786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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.
HTTP request?