Class RestApiBase
java.lang.Object
io.helidon.integrations.common.rest.RestApiBase
- All Implemented Interfaces:
RestApi
- Direct Known Subclasses:
VaultRestApi
Base REST API implementation.
Each integration module is expected to have its own implementation of this class to handle system specific operations,
such as security, processing of headers etc.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.helidon.integrations.common.rest.RestApi
RestApi.Builder<B extends RestApi.Builder<B,
T>, T extends RestApi> -
Constructor Summary
ModifierConstructorDescriptionprotected
RestApiBase
(RestApi.Builder<?, ?> builder) A new instance, requires a subclass of theRestApi.Builder
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addHeaders
(HttpClientRequest request, Map<String, List<String>> headers) Add HTTP headers.protected void
addQueryParams
(HttpClientRequest request, Map<String, List<String>> queryParams) Add HTTP query parameters.protected <T> T
emptyResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ResponseBuilder<?, T, ?> responseBuilder) Empty response, may be because of aStatus.NOT_FOUND_404
, or some other status, such asStatus.NOT_MODIFIED_304
.protected FtHandler
Fault tolerance handler to use to invoke requests.protected <R,
T extends ApiOptionalResponse<R>>
ThandleBytesResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, byte[], R> responseBuilder) Handle bytes response for optional bytes entity.protected <R,
T extends ApiOptionalResponse<R>>
ThandleEntityResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, InputStream, R> responseBuilder) Handle response for optional publisher entity.protected <T extends ApiEntityResponse>
ThandleJsonResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiEntityResponse.Builder<?, T, JsonObject> responseBuilder) Reads JsonObject from response entity and either calls thejsonOkResponse
.protected <R,
T extends ApiOptionalResponse<R>>
ThandleOptionalJsonResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, JsonObject, R> responseBuilder) Handle response for optional JSON entity.protected <T extends ApiResponse>
ThandleResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?, T> responseBuilder) Handle response for a request not expecting an entity.<T extends ApiResponse>
Tinvoke
(Method method, String path, ApiRequest<?> request, ApiResponse.Builder<?, T> responseBuilder) Invoke a request that is not expected to yield an entity.<T extends ApiResponse>
TinvokeBytesRequest
(Method method, String path, ApiRequest<?> apiRequest, InputStream is, ApiResponse.Builder<?, T> responseBuilder) The request media type should be provided in request, falls back toMediaTypes.APPLICATION_OCTET_STREAM
.<R,
T extends ApiOptionalResponse<R>>
TinvokeBytesResponse
(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, byte[], R> responseBuilder) Invoke API call that is expected to return bytes.<R,
T extends ApiOptionalResponse<R>>
TinvokeEntityResponse
(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, InputStream, R> responseBuilder) Invoke API call that is expected to return bytes as a publisher.<R,
T extends ApiOptionalResponse<R>>
TinvokeOptional
(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, JsonObject, R> responseBuilder) Invoke a request that may yield an entity.<T extends ApiEntityResponse>
TinvokeWithResponse
(Method method, String path, ApiRequest<?> request, ApiEntityResponse.Builder<?, T, JsonObject> responseBuilder) Invoke a request that is expected to yield an entity.protected boolean
isEntityExpected
(String path, ApiRequest<?> request, Method method, String requestId, Status status) This method is only called for methods that return an optional entity.protected boolean
Provide information whether the response is a success response for requests with optional entity.protected JsonBuilderFactory
JSON builder factory.protected <T> T
jsonOkResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject json, ResponseBuilder<?, T, JsonObject> responseBuilder) Builds the response using the response builder provided.protected JsonReaderFactory
JSON reader factory.protected JsonWriterFactory
JSON writer factory.protected <T extends ApiResponse>
TnoEntityOkResponse
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?, T> responseBuilder) Create a response for no entity.protected ApiRestException
readError
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response) Read error with no entity (content length set to 0).protected ApiRestException
readError
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject errorObject) Read error with a JSON entity.protected ApiRestException
readError
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, String entity) Read error with an entity that failed to be parsed into a JSON object.protected ApiRestException
readErrorFailedEntity
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, Throwable throwable) Read error information when we failed to read response entity.protected Supplier
<HttpClientResponse> requestBytesPayload
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, InputStream is) Create a supplier for a response with publisher request.protected String
requestId
(ApiRequest<?> request) Attempts to find a good request id.protected Supplier
<HttpClientResponse> requestJsonPayload
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, JsonObject jsonObject) Create a supplier for a response with JSON request.protected Supplier
<HttpClientResponse> requestPayload
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder) Create a supplier for a response.protected ApiRestException
responseError
(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response) Create anApiRestException
.protected Supplier
<HttpClientResponse> responseSupplier
(Method method, String path, ApiRequest<?> request, String requestId) Create a response supplier from the request.protected HttpClientRequest
updateRequestBuilder
(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder with no request payload.protected HttpClientRequest
updateRequestBuilder
(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId, JsonObject jsonObject) Update request builder with no request payload.protected HttpClientRequest
updateRequestBuilderBytesPayload
(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder with publisher request payload.protected HttpClientRequest
updateRequestBuilderCommon
(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder used by all default implementation inRestApiBase
.protected WebClient
WebClient to be used to invoke requests.
-
Constructor Details
-
RestApiBase
A new instance, requires a subclass of theRestApi.Builder
.- Parameters:
builder
- builder to set this instance from
-
-
Method Details
-
invoke
public <T extends ApiResponse> T invoke(Method method, String path, ApiRequest<?> request, ApiResponse.Builder<?, T> responseBuilder) Description copied from interface:RestApi
Invoke a request that is not expected to yield an entity.- Specified by:
invoke
in interfaceRestApi
- Type Parameters:
T
- type of the response- Parameters:
method
- HTTP method to invokepath
- path to invokerequest
- request to use, should be an instance ofApiJsonRequest
if and entity is desiredresponseBuilder
- builder to construct response from API call- Returns:
- response
-
invokeWithResponse
public <T extends ApiEntityResponse> T invokeWithResponse(Method method, String path, ApiRequest<?> request, ApiEntityResponse.Builder<?, T, JsonObject> responseBuilder) Description copied from interface:RestApi
Invoke a request that is expected to yield an entity.- Specified by:
invokeWithResponse
in interfaceRestApi
- Type Parameters:
T
- type of the response- Parameters:
method
- HTTP method to invokepath
- path to invokerequest
- request to use, should be an instance ofApiJsonRequest
if and entity is desiredresponseBuilder
- builder to construct response from API call- Returns:
- future with entity and metadata if successful, future with error otherwise
-
invokeBytesRequest
public <T extends ApiResponse> T invokeBytesRequest(Method method, String path, ApiRequest<?> apiRequest, InputStream is, ApiResponse.Builder<?, T> responseBuilder) Description copied from interface:RestApi
The request media type should be provided in request, falls back toMediaTypes.APPLICATION_OCTET_STREAM
.- Specified by:
invokeBytesRequest
in interfaceRestApi
- Type Parameters:
T
- type of the response- Parameters:
method
- method to invokepath
- path to invokeapiRequest
- request used to configure query parameters and headersis
- entity bytesresponseBuilder
- builder to construct response from API call- Returns:
- future with the response or error
-
invokeEntityResponse
public <R,T extends ApiOptionalResponse<R>> T invokeEntityResponse(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, InputStream, R> responseBuilder) Description copied from interface:RestApi
Invoke API call that is expected to return bytes as a publisher.The accepted media type must be provided in request, falls back to
MediaTypes.APPLICATION_OCTET_STREAM
.- Specified by:
invokeEntityResponse
in interfaceRestApi
- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
method
- method to invokepath
- path to invokerequest
- request used to configure query parameters and headersresponseBuilder
- builder to construct response from API call with appropriate processor to handle the returned publisher- Returns:
- future with the response
-
invokeBytesResponse
public <R,T extends ApiOptionalResponse<R>> T invokeBytesResponse(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, byte[], R> responseBuilder) Description copied from interface:RestApi
Invoke API call that is expected to return bytes. This method collects all bytes in memory, so it cannot be used for large data. SeeRestApi.invokeEntityResponse(io.helidon.http.Method, String, ApiRequest, BuilderBase)
.The accepted media type must be provided in request, falls back to
MediaTypes.APPLICATION_OCTET_STREAM
.- Specified by:
invokeBytesResponse
in interfaceRestApi
- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
method
- method to invokepath
- path to invokerequest
- request used to configure query parameters and headersresponseBuilder
- builder to construct response from API call with appropriate processor to handle the returned bytes- Returns:
- response
-
invokeOptional
public <R,T extends ApiOptionalResponse<R>> T invokeOptional(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?, T, JsonObject, R> responseBuilder) Description copied from interface:RestApi
Invoke a request that may yield an entity. The entity is expected to be missing ifStatus.NOT_FOUND_404
is returned by the API call (and for some other cases, such as not modified).- Specified by:
invokeOptional
in interfaceRestApi
- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
method
- HTTP method to invokepath
- path to invokerequest
- request to use, should be an instance ofApiJsonRequest
if and entity is desiredresponseBuilder
- response builder with appropriate processor to create the optional part- Returns:
- response
-
responseSupplier
protected Supplier<HttpClientResponse> responseSupplier(Method method, String path, ApiRequest<?> request, String requestId) Create a response supplier from the request. This method checks if there is a payload, and prepares the supplier based on this information.- Parameters:
method
- HTTP method to invokepath
- path to invokerequest
- request that may contain a JSON entityrequestId
- request ID to use for this request- Returns:
- supplier of response that is used with fault tolerance
-
addQueryParams
Add HTTP query parameters.- Parameters:
request
- client requestqueryParams
- query parameters
-
addHeaders
Add HTTP headers.- Parameters:
request
- client requestheaders
- headers to add
-
handleBytesResponse
protected <R,T extends ApiOptionalResponse<R>> T handleBytesResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, byte[], R> responseBuilder) Handle bytes response for optional bytes entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
path
- requested pathrequest
- API requestmethod
- HTTP methodrequestId
- request IDresponse
- the web client responseresponseBuilder
- builder to configure success response- Returns:
- typed response
- Throws:
ApiRestException
- if an error occurs
-
handleEntityResponse
protected <R,T extends ApiOptionalResponse<R>> T handleEntityResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, InputStream, R> responseBuilder) Handle response for optional publisher entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
path
- requested pathrequest
- API requestmethod
- HTTP methodrequestId
- request IDresponse
- the web client responseresponseBuilder
- builder to configure success response- Returns:
- typed response
- Throws:
ApiRestException
- if an error occurs
-
isSuccess
protected boolean isSuccess(String path, ApiRequest<?> request, Method method, String requestId, Status status) Provide information whether the response is a success response for requests with optional entity.- Parameters:
path
- requested pathrequest
- API requestmethod
- HTTP methodrequestId
- request IDstatus
- returned HTTP status- Returns:
true
for success states,false
for errors
-
isEntityExpected
protected boolean isEntityExpected(String path, ApiRequest<?> request, Method method, String requestId, Status status) This method is only called for methods that return an optional entity. If a method (such asRestApi.invokeWithResponse(Method, String, ApiRequest, io.helidon.integrations.common.rest.ApiEntityResponse.Builder)
) receives a status that would not yield an entity (such as 404), it is automatically an error. Also this method is never called for codes in the success family.- Parameters:
path
- requested pathrequest
- API requestmethod
- HTTP methodrequestId
- request IDstatus
- returned HTTP status- Returns:
true
if entity is expected,false
otherwise (such as for 404 status code)
-
handleOptionalJsonResponse
protected <R,T extends ApiOptionalResponse<R>> T handleOptionalJsonResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?, T, JsonObject, R> responseBuilder) Handle response for optional JSON entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.- Type Parameters:
R
- type of the optional part of the responseT
- type of the response- Parameters:
path
- requested pathrequest
- API requestmethod
- HTTP methodrequestId
- request IDresponse
- the web client responseresponseBuilder
- builder to configure success response- Returns:
- typed response
- Throws:
ApiRestException
- if an error occurs
-
emptyResponse
protected <T> T emptyResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ResponseBuilder<?, T, ?> responseBuilder) Empty response, may be because of aStatus.NOT_FOUND_404
, or some other status, such asStatus.NOT_MODIFIED_304
.- Type Parameters:
T
- type of the response- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entityresponseBuilder
- builder to create a response instance- Returns:
- typed response with no entity
-
jsonOkResponse
protected <T> T jsonOkResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject json, ResponseBuilder<?, T, JsonObject> responseBuilder) Builds the response using the response builder provided. This is the last chance to update the response builder with system specific information.- Type Parameters:
T
- type of the response- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entityjson
- the JsonObject parsed from entityresponseBuilder
- builder to create a response instance- Returns:
- typed response
-
handleJsonResponse
protected <T extends ApiEntityResponse> T handleJsonResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiEntityResponse.Builder<?, T, JsonObject> responseBuilder) Reads JsonObject from response entity and either calls thejsonOkResponse
.- Type Parameters:
T
- type of the response- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entityresponseBuilder
- builder to create a response instance- Returns:
- typed response
- Throws:
ApiRestException
- if an error occurs
-
handleResponse
protected <T extends ApiResponse> T handleResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?, T> responseBuilder) Handle response for a request not expecting an entity.- Type Parameters:
T
- type of the response- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entityresponseBuilder
- builder to create a response instance- Returns:
- typed response
- Throws:
ApiRestException
- if an error occurs
-
responseError
protected ApiRestException responseError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response) Create anApiRestException
. This method attempts to read the response entity as a string, parse it into a JsonObject and depending on result, calls methods to create a proper exception.- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entity- Returns:
- ApiException
-
readErrorFailedEntity
protected ApiRestException readErrorFailedEntity(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, Throwable throwable) Read error information when we failed to read response entity.- Parameters:
path
- requested pathrequest
- original requestmethod
- HTTP methodrequestId
- ID of the requestresponse
- actual response where we do not expect an entitythrowable
- throwable that caused this problem (such as parsing exception)- Returns:
- an ApiRestException
-
readError
protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, String entity) Read error with an entity that failed to be parsed into a JSON object.- Parameters:
path
- requested pathrequest
- original API requestmethod
- HTTP methodrequestId
- request IDresponse
- web client response with entity consumedentity
- entity as a string- Returns:
- an ApiRestException
-
readError
protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response) Read error with no entity (content length set to 0).- Parameters:
path
- requested pathrequest
- original API requestmethod
- HTTP methodrequestId
- request IDresponse
- web client response with entity consumed- Returns:
- an ApiRestException
-
readError
protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject errorObject) Read error with a JSON entity.- Parameters:
path
- requested pathrequest
- original API requestmethod
- HTTP methodrequestId
- request IDresponse
- web client response with entity consumederrorObject
- entity as a JSON object- Returns:
- an ApiRestException
-
noEntityOkResponse
protected <T extends ApiResponse> T noEntityOkResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?, T> responseBuilder) Create a response for no entity. This method builds the response from builder.- Type Parameters:
T
- type of the response- Parameters:
path
- requested pathrequest
- original API requestmethod
- HTTP methodrequestId
- request IDresponse
- web client response with entity consumedresponseBuilder
- response builder- Returns:
- typed response
-
requestJsonPayload
protected Supplier<HttpClientResponse> requestJsonPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, JsonObject jsonObject) Create a supplier for a response with JSON request. Defaults to "() -> clientRequest.submit(jsonObject)
". Also configures content type and accept headers.- Parameters:
path
- path requestedrequest
- API requestmethod
- HTTP methodrequestId
- ID of this requestrequestBuilder
-HttpClientRequest
request builderjsonObject
- JSON object that should be sent as a request entity- Returns:
- supplier of a client response
-
requestBytesPayload
protected Supplier<HttpClientResponse> requestBytesPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, InputStream is) Create a supplier for a response with publisher request. Defaults to "() -> clientRequest.submit(publisher)
". Also configures content type and accept headers.- Parameters:
path
- path requestedrequest
- API requestmethod
- HTTP methodrequestId
- ID of this requestrequestBuilder
-HttpClientRequest
request builderis
- entity input stream- Returns:
- supplier of a client response
-
requestPayload
protected Supplier<HttpClientResponse> requestPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder) Create a supplier for a response. Defaults torequestBuilder::request
.- Parameters:
path
- path requestedrequest
- API requestmethod
- HTTP methodrequestId
- ID of this requestrequestBuilder
-HttpClientRequest
request builder- Returns:
- supplier of a client response
-
updateRequestBuilder
protected HttpClientRequest updateRequestBuilder(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder with no request payload. Default implementation does nothing.- Parameters:
requestBuilder
- current request builderpath
- path to be executedrequest
- API requestmethod
- methodrequestId
- request ID- Returns:
- updated builder
-
updateRequestBuilderBytesPayload
protected HttpClientRequest updateRequestBuilderBytesPayload(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder with publisher request payload. Default implementation does nothing.- Parameters:
requestBuilder
- current request builderpath
- path to be executedrequest
- API requestmethod
- methodrequestId
- request ID- Returns:
- updated builder
-
updateRequestBuilder
protected HttpClientRequest updateRequestBuilder(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId, JsonObject jsonObject) Update request builder with no request payload. Default implementation does nothing.- Parameters:
requestBuilder
- current request builderpath
- path to be executedrequest
- API requestmethod
- methodrequestId
- request IDjsonObject
- json object with the request- Returns:
- updated builder
-
updateRequestBuilderCommon
protected HttpClientRequest updateRequestBuilderCommon(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId) Update request builder used by all default implementation inRestApiBase
.- Parameters:
requestBuilder
- current request builderpath
- path to be executedrequest
- API requestmethod
- methodrequestId
- request ID- Returns:
- updated builder
-
requestId
Attempts to find a good request id. This looks in the following (sequentially):ApiRestRequest.requestId()
- Trace ID of the current span if one is present (obtained from
Context
- Random UUID
- Parameters:
request
- request- Returns:
- request ID
-
webClient
WebClient to be used to invoke requests.- Returns:
- web client
-
ftHandler
Fault tolerance handler to use to invoke requests.- Returns:
- fault tolerance handler
-
jsonBuilderFactory
JSON builder factory.- Returns:
- builder factory
-
jsonReaderFactory
JSON reader factory.- Returns:
- reader factory
-
jsonWriterFactory
JSON writer factory.- Returns:
- writer factory
-