Skip to content

Commit

Permalink
Add identity/authorization headers into Rest Client calls (#613)
Browse files Browse the repository at this point in the history
* added method headersToRequest

Signed-off-by: Nurym <[email protected]>

* creating a new equest ptions object

Signed-off-by: Nurym <[email protected]>

* set on the client

Signed-off-by: Nurym <[email protected]>

* added setter for requestoptions

Signed-off-by: Nurym <[email protected]>

* Merge branch 'main' into RepositoryExtension

Signed-off-by: Nurym <[email protected]>

* added options for all request

Signed-off-by: Nurym <[email protected]>

* added options for all request

Signed-off-by: Nurym <[email protected]>

---------

Signed-off-by: Nurym <[email protected]>
Co-authored-by: Owais Kazi <[email protected]>
  • Loading branch information
kokibas and owaiskazi19 authored Apr 13, 2023
1 parent e3dfb58 commit 0506955
Showing 1 changed file with 24 additions and 15 deletions.
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.
*
* @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;
}

/**
* 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);
}
}
}

0 comments on commit 0506955

Please sign in to comment.