Class RestBuilder

java.lang.Object
com.couchbase.client.java.cluster.api.RestBuilder
All Implemented Interfaces:
RestBuilderMarker

@Public
@Experimental
public class RestBuilder
extends Object
implements RestBuilderMarker
A builder class to incrementally construct REST API requests and execute them synchronously
Since:
2.3.2
Author:
Simon Baslé
  • Constructor Details

    • RestBuilder

      public RestBuilder​(AsyncRestBuilder asyncBuilder, long defaultTimeout, TimeUnit defaultTimeUnit)
      Parameters:
      asyncBuilder -
      defaultTimeout -
      defaultTimeUnit -
  • Method Details

    • withParam

      public RestBuilder withParam​(String key, String value)
      Adds an URL query parameter to the request. Using a key twice will result in the last call being taken into account.
      Parameters:
      key - the parameter key.
      value - the parameter value.
    • contentType

      public RestBuilder contentType​(String type)
      Sets the "Content-Type" standard header's value. This is a convenience method equivalent to calling withHeader("Content-Type", type).
      Parameters:
      type - the "Content-Type" to use.
    • withHeader

      public RestBuilder withHeader​(String key, Object value)
      Adds an HTTP header to the request. Using a key twice will result in the last value being used for a given header.
      Parameters:
      key - the header name (see HttpHeaders.Names for standard names).
      value - the header value (see HttpHeaders.Values for standard values).
    • body

      public RestBuilder body​(String jsonBody)
      Sets the body for the request, assuming it is JSON. This is equivalent to setting the "Content-Type" to "application/json" and then setting the body via bodyRaw(String). Note that you should avoid calling this for HTTP methods where it makes no sense (eg. GET, DELETE), as it won't be ignored for these types of requests.
      Parameters:
      jsonBody - the JSON body to use, as a String.
    • body

      public RestBuilder body​(JsonValue jsonBody)
      Sets the body for the request, assuming it is JSON. This is equivalent to setting the "Content-Type" to "application/json" and then setting the body via bodyRaw(String). Note that you should avoid calling this for HTTP methods where it makes no sense (eg. GET, DELETE), as it won't be ignored for these types of requests.
      Parameters:
      jsonBody - the JSON body to use, as a JsonObject.
    • bodyRaw

      public RestBuilder bodyRaw​(String body)
      Sets the body for the request without assuming a Content-Type or Accept header. Note that you should avoid calling this for HTTP methods where it makes no sense (eg. GET, DELETE), as it won't be ignored for these types of requests.
      Parameters:
      body - the raw body value to use, as a String.
    • accept

      public RestBuilder accept​(String type)
      Sets the "Accept" standard header's value. This is a convenience method equivalent to calling withHeader("Accept", type).
      Parameters:
      type - the "Accept" type to use.
    • bodyForm

      public RestBuilder bodyForm​(Form form)
      Sets the body for the request to be an url-encoded form. This is equivalent to setting the "Content-Type" to "application/x-www-form-urlencoded" and then setting the body via bodyRaw(String).
      Parameters:
      form - the Form builder object used to set form parameters.
    • method

      public com.couchbase.client.deps.io.netty.handler.codec.http.HttpMethod method()
      Returns:
      the HttpMethod used for this request.
    • path

      public String path()
      Returns:
      the full HTTP path (minus query parameters) used for this request.
    • params

      public Map<String,​String> params()
      Returns:
      a copy of the query parameters used for this request.
    • headers

      public Map<String,​Object> headers()
      Returns:
      a copy of the HTTP headers used for this request.
    • body

      public String body()
      Returns:
      the body used for this request.
    • asRequest

      public RestApiRequest asRequest()
      Returns:
      the RestApiRequest message sent through the ClusterFacade when executing this request.
    • execute

      public RestApiResponse execute​(long timeout, TimeUnit timeUnit)
      Executes the API request in a synchronous fashion, using the given timeout.
      Parameters:
      timeout - the custom timeout to use for the request.
      timeUnit - the TimeUnit for the timeout.
      Returns:
      the result of the API call, as a RestApiResponse.
      Throws:
      RuntimeException - wrapping a TimeoutException in case the request took too long.
    • execute

      public RestApiResponse execute()
      Executes the API request in a synchronous fashion, using the default timeout. The default timeout is currently the same as the view timeout.
      Returns:
      the result of the API call, as a RestApiResponse.
      Throws:
      RuntimeException - wrapping a TimeoutException in case the request took too long.