-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
- Loading branch information
1 parent
3c5c386
commit e6323e1
Showing
3 changed files
with
257 additions
and
1 deletion.
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
145 changes: 145 additions & 0 deletions
145
java-client/src/main/java/org/opensearch/client/opensearch/request/PutHttpRequest.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,145 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.request; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.opensearch.client.Response; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.json.JsonpSerializable; | ||
import org.opensearch.client.json.JsonpSerializer; | ||
import org.opensearch.client.json.JsonpUtils; | ||
import org.opensearch.client.opensearch._types.ErrorResponse; | ||
import org.opensearch.client.opensearch._types.RequestBase; | ||
import org.opensearch.client.transport.Endpoint; | ||
import org.opensearch.client.transport.endpoints.SimpleEndpoint; | ||
import org.opensearch.client.util.ApiTypeHelper; | ||
import org.opensearch.client.util.ObjectBuilder; | ||
import org.opensearch.client.util.ObjectBuilderBase; | ||
|
||
import jakarta.json.stream.JsonGenerator; | ||
|
||
public class PutHttpRequest<TDocument> extends RequestBase implements JsonpSerializable { | ||
|
||
private final String path; | ||
private final Map<String, String> queryParams; | ||
private final Map<String, String> headers; | ||
|
||
private final TDocument document; | ||
|
||
@Nullable | ||
private final JsonpSerializer<TDocument> tDocumentSerializer; | ||
|
||
private PutHttpRequest(Builder<TDocument> builder) { | ||
this.path = ApiTypeHelper.requireNonNull(builder.path, this, "path"); | ||
this.queryParams = ApiTypeHelper.unmodifiable(builder.queryParams); | ||
this.headers = ApiTypeHelper.unmodifiable(builder.headers); | ||
this.document = ApiTypeHelper.requireNonNull(builder.document, this, "document"); | ||
this.tDocumentSerializer = builder.tDocumentSerializer; | ||
} | ||
|
||
public static <TDocument> PutHttpRequest<TDocument> of( | ||
Function<Builder<TDocument>, ObjectBuilder<PutHttpRequest<TDocument>>> fn) { | ||
return fn.apply(new Builder<>()).build(); | ||
} | ||
|
||
public final String path() { | ||
return this.path; | ||
} | ||
|
||
public final Map<String, String> queryParams() { | ||
return this.queryParams; | ||
} | ||
|
||
public final Map<String, String> headers() { | ||
return this.headers; | ||
} | ||
|
||
public final TDocument document() { | ||
return this.document; | ||
} | ||
|
||
public void serialize(JsonGenerator generator, JsonpMapper mapper) { | ||
JsonpUtils.serialize(this.document, generator, tDocumentSerializer, mapper); | ||
|
||
} | ||
|
||
public static class Builder<TDocument> extends ObjectBuilderBase | ||
implements ObjectBuilder<PutHttpRequest<TDocument>> { | ||
private String path; | ||
private Map<String, String> queryParams; | ||
private Map<String, String> headers; | ||
|
||
private TDocument document; | ||
|
||
@Nullable | ||
private JsonpSerializer<TDocument> tDocumentSerializer; | ||
|
||
public final Builder<TDocument> path(String path) { | ||
this.path = path; | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> queryParams(String key, String value) { | ||
this.queryParams = _mapPut(this.queryParams, key, value); | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> headers(String key, String value) { | ||
this.headers = _mapPut(this.headers, key, value); | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> queryParams(Map<String, String> queryParams) { | ||
this.queryParams = _mapPutAll(this.queryParams, queryParams); | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> headers(Map<String, String> headers) { | ||
this.headers = _mapPutAll(this.headers, headers); | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> document(TDocument value) { | ||
this.document = value; | ||
return this; | ||
} | ||
|
||
public final Builder<TDocument> tDocumentSerializer(@Nullable JsonpSerializer<TDocument> value) { | ||
this.tDocumentSerializer = value; | ||
return this; | ||
} | ||
|
||
public PutHttpRequest<TDocument> build() { | ||
_checkSingleUse(); | ||
|
||
return new PutHttpRequest<TDocument>(this); | ||
} | ||
} | ||
|
||
public static final Endpoint<PutHttpRequest<?>, Response, ErrorResponse> _ENDPOINT = new SimpleEndpoint<>( | ||
|
||
// Request method | ||
request -> { | ||
return "PUT"; | ||
|
||
}, request -> { | ||
return request.path; | ||
|
||
}, request -> { | ||
return request.queryParams; | ||
|
||
}, request -> { | ||
return request.headers; | ||
|
||
}, true, null); | ||
} |
112 changes: 112 additions & 0 deletions
112
java-client/src/main/java/org/opensearch/client/transport/endpoints/GenericEndpoint.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,112 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.transport.endpoints; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import org.apache.hc.core5.net.URLEncodedUtils; | ||
import org.opensearch.client.json.JsonpDeserializer; | ||
import org.opensearch.client.opensearch._types.ErrorResponse; | ||
|
||
public class GenericEndpoint<RequestT, ResponseT> { | ||
|
||
private static final Function<?, Map<String, String>> EMPTY_MAP = x -> Collections.emptyMap(); | ||
|
||
/** | ||
* Returns a function that always returns an empty String to String map. Useful | ||
* to avoid creating lots of | ||
* duplicate lambdas in endpoints that don't have headers or parameters. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> Function<T, Map<String, String>> emptyMap() { | ||
return (Function<T, Map<String, String>>) EMPTY_MAP; | ||
} | ||
|
||
private final Function<RequestT, String> method; | ||
private final Function<RequestT, String> requestUrl; | ||
private final Function<RequestT, Map<String, String>> queryParameters; | ||
private final Function<RequestT, Map<String, String>> headers; | ||
private final Function<RequestT, Boolean> hasRequestBody; | ||
private final JsonpDeserializer<ResponseT> responseParser; | ||
|
||
public GenericEndpoint( | ||
Function<RequestT, String> method, | ||
Function<RequestT, String> requestUrl, | ||
Function<RequestT, Map<String, String>> queryParameters, | ||
Function<RequestT, Map<String, String>> headers, | ||
Function<RequestT, Boolean> hasRequestBody, | ||
JsonpDeserializer<ResponseT> responseParser) { | ||
this.method = method; | ||
this.requestUrl = requestUrl; | ||
this.queryParameters = queryParameters; | ||
this.headers = headers; | ||
this.hasRequestBody = hasRequestBody; | ||
this.responseParser = responseParser; | ||
} | ||
|
||
public String method(RequestT request) { | ||
return this.method.apply(request); | ||
} | ||
|
||
public String requestUrl(RequestT request) { | ||
return this.requestUrl.apply(request); | ||
} | ||
|
||
public Map<String, String> queryParameters(RequestT request) { | ||
return this.queryParameters.apply(request); | ||
} | ||
|
||
public Map<String, String> headers(RequestT request) { | ||
return this.headers.apply(request); | ||
} | ||
|
||
public boolean hasRequestBody(RequestT request) { | ||
return this.hasRequestBody.apply(request); | ||
} | ||
|
||
public JsonpDeserializer<ResponseT> responseDeserializer() { | ||
return this.responseParser; | ||
} | ||
|
||
public boolean isError(int statusCode) { | ||
return statusCode >= 400; | ||
} | ||
|
||
public JsonpDeserializer<ErrorResponse> errorDeserializer(int statusCode) { | ||
return ErrorResponse._DESERIALIZER; | ||
} | ||
|
||
public <NewResponseT> GenericEndpoint<RequestT, NewResponseT> withResponseDeserializer( | ||
JsonpDeserializer<NewResponseT> newResponseParser) { | ||
return new GenericEndpoint<>( | ||
method, | ||
requestUrl, | ||
queryParameters, | ||
headers, | ||
hasRequestBody, | ||
newResponseParser); | ||
} | ||
|
||
public static RuntimeException noPathTemplateFound(String what) { | ||
return new RuntimeException("Could not find a request " + what + " with this set of properties. " + | ||
"Please check the API documentation, or raise an issue if this should be a valid request."); | ||
} | ||
|
||
public static void pathEncode(String src, StringBuilder dest) { | ||
// TODO: avoid dependency on HttpClient here (and use something more efficient) | ||
dest.append(URLEncodedUtils.formatSegments(src).substring(1)); | ||
} | ||
} |