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 Summary
Constructors Constructor Description RestBuilder(AsyncRestBuilder asyncBuilder, long defaultTimeout, TimeUnit defaultTimeUnit)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RestBuilderaccept(String type)Sets the "Accept" standard header's value.RestApiRequestasRequest()Stringbody()RestBuilderbody(JsonValue jsonBody)Sets the body for the request, assuming it is JSON.RestBuilderbody(String jsonBody)Sets the body for the request, assuming it is JSON.RestBuilderbodyForm(Form form)Sets the body for the request to be an url-encoded form.RestBuilderbodyRaw(String body)Sets the body for the request without assuming a Content-Type or Accept header.RestBuildercontentType(String type)Sets the "Content-Type" standard header's value.RestApiResponseexecute()Executes the API request in a synchronous fashion, using the default timeout.RestApiResponseexecute(long timeout, TimeUnit timeUnit)Executes the API request in a synchronous fashion, using the given timeout.Map<String,Object>headers()com.couchbase.client.deps.io.netty.handler.codec.http.HttpMethodmethod()Map<String,String>params()Stringpath()RestBuilderwithHeader(String key, Object value)Adds an HTTP header to the request.RestBuilderwithParam(String key, String value)Adds an URL query parameter to the request.
-
-
-
Constructor Detail
-
RestBuilder
public RestBuilder(AsyncRestBuilder asyncBuilder, long defaultTimeout, TimeUnit defaultTimeUnit)
- Parameters:
asyncBuilder-defaultTimeout-defaultTimeUnit-
-
-
Method Detail
-
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 callingwithHeader("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 viabodyRaw(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 viabodyRaw(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 aJsonObject.
-
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 callingwithHeader("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 viabodyRaw(String).- Parameters:
form- theFormbuilder object used to set form parameters.
-
method
public com.couchbase.client.deps.io.netty.handler.codec.http.HttpMethod method()
- Returns:
- the
HttpMethodused 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
RestApiRequestmessage sent through theClusterFacadewhen 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- theTimeUnitfor the timeout.- Returns:
- the result of the API call, as a
RestApiResponse. - Throws:
RuntimeException- wrapping aTimeoutExceptionin 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 theview timeout.- Returns:
- the result of the API call, as a
RestApiResponse. - Throws:
RuntimeException- wrapping aTimeoutExceptionin case the request took too long.
-
-