java.lang.Object
io.helidon.integrations.common.rest.RestApiBase
All Implemented Interfaces:
RestApi
Direct Known Subclasses:
VaultRestApi

public abstract class RestApiBase extends Object implements RestApi
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.
  • Constructor Details

    • RestApiBase

      protected RestApiBase(RestApi.Builder<?,?> builder)
      A new instance, requires a subclass of the RestApi.Builder.
      Parameters:
      builder - builder to set this instance from
  • Method Details

    • invoke

      public <T extends ApiResponse> Single<T> invoke(Http.RequestMethod 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 interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - builder to construct response from API call
      Returns:
      future with metadata if successful. future with error otherwise
    • invokeWithResponse

      public <T extends ApiEntityResponse> Single<T> invokeWithResponse(Http.RequestMethod 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 interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - builder to construct response from API call
      Returns:
      future with entity and metadata if successful, future with error otherwise
    • invokeBytesRequest

      public <T extends ApiResponse> Single<T> invokeBytesRequest(Http.RequestMethod method, String path, ApiRequest<?> request, Flow.Publisher<DataChunk> byteRequest, ApiResponse.Builder<?,T> responseBuilder)
      Description copied from interface: RestApi
      The request media type should be provided in request, falls back to MediaType.APPLICATION_OCTET_STREAM.
      Specified by:
      invokeBytesRequest in interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      request - request used to configure query parameters and headers
      byteRequest - bytes of the entity
      responseBuilder - builder to construct response from API call
      Returns:
      future with the response or error
    • invokePublisherResponse

      public <R, T extends ApiOptionalResponse<R>> Single<T> invokePublisherResponse(Http.RequestMethod method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?,T,Multi<DataChunk>,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 MediaType.APPLICATION_OCTET_STREAM.
      Specified by:
      invokePublisherResponse in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      request - request used to configure query parameters and headers
      responseBuilder - 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>> Single<T> invokeBytesResponse(Http.RequestMethod 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. See RestApi.invokePublisherResponse(io.helidon.common.http.Http.RequestMethod, String, ApiRequest, io.helidon.integrations.common.rest.ApiOptionalResponse.BuilderBase). The accepted media type must be provided in request, falls back to MediaType.APPLICATION_OCTET_STREAM.
      Specified by:
      invokeBytesResponse in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      request - request used to configure query parameters and headers
      responseBuilder - builder to construct response from API call with appropriate processor to handle the returned bytes
      Returns:
      future with the response
    • invokeOptional

      public <R, T extends ApiOptionalResponse<R>> Single<T> invokeOptional(Http.RequestMethod 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 if Http.Status.NOT_FOUND_404 is returned by the API call (and for some other cases, such as not modified).
      Specified by:
      invokeOptional in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - response builder with appropriate processor to create the optional part
      Returns:
      future with optional entity, and metadata if successful; future with error otherwise
    • responseSupplier

      protected Supplier<Single<WebClientResponse>> responseSupplier(Http.RequestMethod 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 invoke
      path - path to invoke
      request - request that may contain a JSON entity
      requestId - request ID to use for this request
      Returns:
      supplier of response that is used with fault tolerance
    • addQueryParams

      protected void addQueryParams(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId)
      Add HTTP query parameters. This method adds query parameter configured on the provided ApiRequest.
      Parameters:
      requestBuilder - web client request builder to configure query parameters on
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
    • addHeaders

      protected void addHeaders(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId)
      Add HTTP headers. This method adds headers configured on the provided ApiRequest.
      Parameters:
      requestBuilder - web client request builder to configure headers on
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
    • handleBytesResponse

      protected <R, T extends ApiOptionalResponse<R>> Single<T> handleBytesResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse 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. For failures, returns an error.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      future with response
    • handlePublisherResponse

      protected <R, T extends ApiOptionalResponse<R>> Single<T> handlePublisherResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, ApiOptionalResponse.BuilderBase<?,T,Multi<DataChunk>,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. For failures, returns an error.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      future with response
    • isSuccess

      protected boolean isSuccess(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, Http.ResponseStatus status)
      Provide information whether the response is a success response for requests with optional entity.
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      status - returned HTTP status
      Returns:
      true for success states, false for errors
    • isEntityExpected

      protected boolean isEntityExpected(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, Http.ResponseStatus status)
      This method is only called for methods that return an optional entity. If a method (such as RestApi.invokeWithResponse(io.helidon.common.http.Http.RequestMethod, 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 path
      request - API request
      method - HTTP method
      requestId - request ID
      status - returned HTTP status
      Returns:
      true if entity is expected, false otherwise (such as for 404 status code)
    • handleOptionalJsonResponse

      protected <R, T extends ApiOptionalResponse<R>> Single<T> handleOptionalJsonResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse 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. For failures, returns an error.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      future with response
    • emptyResponse

      protected <T> Single<T> emptyResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, ResponseBuilder<?,T,?> responseBuilder)
      Empty response, may be because of a Http.Status.NOT_FOUND_404, or some other status, such as Http.Status.NOT_MODIFIED_304.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response with no entity
    • jsonOkResponse

      protected <T> T jsonOkResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse 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 path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      json - the JsonObject parsed from entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response
    • handleJsonResponse

      protected <T extends ApiEntityResponse> Single<T> handleJsonResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, ApiEntityResponse.Builder<?,T,JsonObject> responseBuilder)
      Reads JsonObject from response entity and either calls the jsonOkResponse or errorResponse depending on its success.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      future with typed response
    • handleResponse

      protected <T extends ApiResponse> Single<T> handleResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, ApiResponse.Builder<?,T> responseBuilder)
      Handle response for a request not expecting an entity.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      future with typed response
    • errorResponse

      protected <T extends ApiResponse> Single<T> errorResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response)
      Create an error response. 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.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      Returns:
      future with error
    • readErrorFailedEntity

      protected Throwable readErrorFailedEntity(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, Throwable throwable)
      Read error information when we failed to read resposen entity.
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      throwable - throwable that caused this problem (such as parsing exception)
      Returns:
      throwable to be used in response
    • readError

      protected Throwable readError(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, String entity)
      Read error with an entity that failed to be parsed into a JSON object.
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      entity - entity as a string
      Returns:
      a throwable to be used in response
    • readError

      protected Throwable readError(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response)
      Read error with no entity (content length set to 0).
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      Returns:
      a throwable to be used in response
    • readError

      protected Throwable readError(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse response, JsonObject errorObject)
      Read error with a JSON entity.
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      errorObject - entity as a JSON object
      Returns:
      a throwable to be used in response
    • noEntityOkResponse

      protected <T extends ApiResponse> Single<T> noEntityOkResponse(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientResponse 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 path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      responseBuilder - response builder
      Returns:
      future with the typed response
    • requestJsonPayload

      protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder 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 requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - WebClient request builder
      jsonObject - JSON object that should be sent as a request entity
      Returns:
      supplier of a web client response
    • requestBytesPayload

      protected Supplier<Single<WebClientResponse>> requestBytesPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, Flow.Publisher<DataChunk> publisher)
      Create a supplier for a response with publisher request. Defaults to "() -> clientRequest.submit(publisher)". Also configures content type and accept headers.
      Parameters:
      path - path requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - WebClient request builder
      publisher - publisher to be used as request entity
      Returns:
      supplier of a web client response
    • requestPayload

      protected Supplier<Single<WebClientResponse>> requestPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder)
      Create a supplier for a response. Defaults to requestBuilder::request.
      Parameters:
      path - path requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - WebClient request builder
      Returns:
      supplier of a web client response
    • updateRequestBuilder

      protected Single<WebClientRequestBuilder> updateRequestBuilder(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId)
      Update request builder with no request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • updateRequestBuilderBytesPayload

      protected Single<WebClientRequestBuilder> updateRequestBuilderBytesPayload(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId)
      Update request builder with publisher request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • updateRequestBuilder

      protected Single<WebClientRequestBuilder> updateRequestBuilder(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, JsonObject jsonObject)
      Update request builder with no request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      jsonObject - json object with the request
      Returns:
      updated builder
    • updateRequestBuilderCommon

      protected Single<WebClientRequestBuilder> updateRequestBuilderCommon(WebClientRequestBuilder requestBuilder, String path, ApiRequest<?> request, Http.RequestMethod method, String requestId)
      Update request builder used by all default implementation in RestApiBase.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • requestId

      protected String requestId(ApiRequest<?> restRequest)
      Attempts to find a good request id. This looks in the following (sequentially):
      Parameters:
      restRequest - request
      Returns:
      request ID
    • webClient

      protected WebClient webClient()
      WebClient to be used to invoke requests.
      Returns:
      web client
    • ftHandler

      protected FtHandler ftHandler()
      Fault tolerance handler to use to invoke requests.
      Returns:
      fault tolerance handler
    • jsonBuilderFactory

      protected JsonBuilderFactory jsonBuilderFactory()
      JSON builder factory.
      Returns:
      builder factory
    • jsonReaderFactory

      protected JsonReaderFactory jsonReaderFactory()
      JSON reader factory.
      Returns:
      reader factory
    • jsonWriterFactory

      protected JsonWriterFactory jsonWriterFactory()
      JSON writer factory.
      Returns:
      writer factory