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

Add identity/authorization headers into Rest Client calls #613

Merged
merged 11 commits into from
Apr 13, 2023
39 changes: 24 additions & 15 deletions src/main/java/org/opensearch/sdk/SDKClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public static class SDKRestClient implements Closeable {

private final SDKClient sdkClient;
private final RestHighLevelClient restHighLevelClient;
private RequestOptions options = RequestOptions.DEFAULT;

/**
* Instantiate this class wrapping a {@link RestHighLevelClient}.
Expand All @@ -356,6 +357,10 @@ public SDKRestClient(SDKClient sdkClient, RestHighLevelClient restHighLevelClien
this.restHighLevelClient = restHighLevelClient;
}

public void setOptions(RequestOptions options) {
this.options = options;
}

/**
* The admin client that can be used to perform administrative operations.
*
Expand Down Expand Up @@ -410,7 +415,7 @@ public SDKIndicesClient indices() {
* @see Requests#indexRequest(String)
*/
public void index(IndexRequest request, ActionListener<IndexResponse> listener) {
restHighLevelClient.indexAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.indexAsync(request, options, listener);
}

/**
Expand All @@ -421,7 +426,7 @@ public void index(IndexRequest request, ActionListener<IndexResponse> listener)
* @see Requests#getRequest(String)
*/
public void get(GetRequest request, ActionListener<GetResponse> listener) {
restHighLevelClient.getAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.getAsync(request, options, listener);
}

/**
Expand All @@ -431,7 +436,7 @@ public void get(GetRequest request, ActionListener<GetResponse> listener) {
* @param listener A listener to be notified with a result
*/
public void multiGet(MultiGetRequest request, ActionListener<MultiGetResponse> listener) {
restHighLevelClient.mgetAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.mgetAsync(request, options, listener);
}

/**
Expand All @@ -441,7 +446,7 @@ public void multiGet(MultiGetRequest request, ActionListener<MultiGetResponse> l
* @param listener A listener to be notified with a result
*/
public void update(UpdateRequest request, ActionListener<UpdateResponse> listener) {
restHighLevelClient.updateAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.updateAsync(request, options, listener);
}

/**
Expand All @@ -452,7 +457,7 @@ public void update(UpdateRequest request, ActionListener<UpdateResponse> listene
* @see Requests#deleteRequest(String)
*/
public void delete(DeleteRequest request, ActionListener<DeleteResponse> listener) {
restHighLevelClient.deleteAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.deleteAsync(request, options, listener);
}

/**
Expand All @@ -463,7 +468,7 @@ public void delete(DeleteRequest request, ActionListener<DeleteResponse> listene
* @see Requests#searchRequest(String...)
*/
public void search(SearchRequest request, ActionListener<SearchResponse> listener) {
restHighLevelClient.searchAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.searchAsync(request, options, listener);
}

/**
Expand All @@ -473,12 +478,11 @@ public void search(SearchRequest request, ActionListener<SearchResponse> listene
* @param listener A listener to be notified with a result
*/
public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) {
restHighLevelClient.msearchAsync(request, RequestOptions.DEFAULT, listener);
restHighLevelClient.msearchAsync(request, options, listener);
}

/**
* Sends a request to the OpenSearch cluster that the client points to.
*
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
* @param request the request to perform
* @return the response returned by OpenSearch
* @throws IOException in case of a problem or the connection was aborted
Expand Down Expand Up @@ -545,6 +549,11 @@ public SDKClusterAdminClient(ClusterClient clusterClient) {
public static class SDKIndicesClient {

private final IndicesClient indicesClient;
private RequestOptions options = RequestOptions.DEFAULT;

public void setOptions(RequestOptions options) {
this.options = options;
}
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

/**
* Instantiate this class wrapping an {@link IndicesClient}.
Expand All @@ -563,7 +572,7 @@ public SDKIndicesClient(IndicesClient indicesClient) {
* @return cancellable that may be used to cancel the request
*/
public Cancellable create(CreateIndexRequest createIndexRequest, ActionListener<CreateIndexResponse> listener) {
return indicesClient.createAsync(createIndexRequest, RequestOptions.DEFAULT, listener);
return indicesClient.createAsync(createIndexRequest, options, listener);
}

/**
Expand All @@ -574,7 +583,7 @@ public Cancellable create(CreateIndexRequest createIndexRequest, ActionListener<
* @return cancellable that may be used to cancel the request
*/
public Cancellable delete(DeleteIndexRequest deleteIndexRequest, ActionListener<AcknowledgedResponse> listener) {
return indicesClient.deleteAsync(deleteIndexRequest, RequestOptions.DEFAULT, listener);
return indicesClient.deleteAsync(deleteIndexRequest, options, listener);
}

/**
Expand All @@ -585,7 +594,7 @@ public Cancellable delete(DeleteIndexRequest deleteIndexRequest, ActionListener<
* @return cancellable that may be used to cancel the request
*/
public Cancellable putSettings(UpdateSettingsRequest updateSettingsRequest, ActionListener<AcknowledgedResponse> listener) {
return indicesClient.putSettingsAsync(updateSettingsRequest, RequestOptions.DEFAULT, listener);
return indicesClient.putSettingsAsync(updateSettingsRequest, options, listener);
}

/**
Expand All @@ -596,7 +605,7 @@ public Cancellable putSettings(UpdateSettingsRequest updateSettingsRequest, Acti
* @return cancellable that may be used to cancel the request
*/
public Cancellable putMapping(PutMappingRequest putMappingRequest, ActionListener<AcknowledgedResponse> listener) {
return this.indicesClient.putMappingAsync(putMappingRequest, RequestOptions.DEFAULT, listener);
return this.indicesClient.putMappingAsync(putMappingRequest, options, listener);
}

/**
Expand All @@ -607,7 +616,7 @@ public Cancellable putMapping(PutMappingRequest putMappingRequest, ActionListene
* @return cancellable that may be used to cancel the request
*/
public Cancellable getMapping(GetMappingsRequest getMappingsRequest, ActionListener<GetMappingsResponse> listener) {
return this.indicesClient.getMappingAsync(getMappingsRequest, RequestOptions.DEFAULT, listener);
return this.indicesClient.getMappingAsync(getMappingsRequest, options, listener);
}

/**
Expand All @@ -618,7 +627,7 @@ public Cancellable getMapping(GetMappingsRequest getMappingsRequest, ActionListe
* @return cancellable that may be used to cancel the request
*/
public Cancellable rolloverIndex(RolloverRequest rolloverRequest, ActionListener<RolloverResponse> listener) {
return this.indicesClient.rolloverAsync(rolloverRequest, RequestOptions.DEFAULT, listener);
return this.indicesClient.rolloverAsync(rolloverRequest, options, listener);
}

/**
Expand All @@ -629,7 +638,7 @@ public Cancellable rolloverIndex(RolloverRequest rolloverRequest, ActionListener
* @return cancellable that may be used to cancel the request
*/
public Cancellable getAliases(GetAliasesRequest getAliasesRequest, ActionListener<GetAliasesResponse> listener) {
return this.indicesClient.getAliasAsync(getAliasesRequest, RequestOptions.DEFAULT, listener);
return this.indicesClient.getAliasAsync(getAliasesRequest, options, listener);
}
}
}