@InterfaceAudience.Public @InterfaceStability.Experimental public class RestBuilder extends Object implements RestBuilderMarker
A builder class to incrementally construct REST API requests and execute them synchronously
Constructor and Description |
---|
RestBuilder(AsyncRestBuilder asyncBuilder,
long defaultTimeout,
TimeUnit defaultTimeUnit) |
Modifier and Type | Method and Description |
---|---|
RestBuilder |
accept(String type)
Sets the “Accept” standard header’s value.
|
RestApiRequest |
asRequest() |
String |
body() |
RestBuilder |
body(JsonValue jsonBody)
Sets the body for the request, assuming it is JSON.
|
RestBuilder |
body(String jsonBody)
Sets the body for the request, assuming it is JSON.
|
RestBuilder |
bodyForm(Form form)
Sets the body for the request to be an url-encoded form.
|
RestBuilder |
bodyRaw(String body)
Sets the body for the request without assuming a Content-Type or Accept header.
|
RestBuilder |
contentType(String type)
Sets the “Content-Type” standard header’s value.
|
RestApiResponse |
execute()
Executes the API request in a synchronous fashion, using the default timeout.
|
RestApiResponse |
execute(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.HttpMethod |
method() |
Map<String,String> |
params() |
String |
path() |
RestBuilder |
withHeader(String key,
Object value)
Adds an HTTP header to the request.
|
RestBuilder |
withParam(String key,
String value)
Adds an URL query parameter to the request.
|
public RestBuilder(AsyncRestBuilder asyncBuilder, long defaultTimeout, TimeUnit defaultTimeUnit)
asyncBuilder
- defaultTimeout
- defaultTimeUnit
- 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.
key
- the parameter key.value
- the parameter value.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)
.
type
- the “Content-Type” to use.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.
key
- the header name (see HttpHeaders.Names
for standard names).value
- the header value (see HttpHeaders.Values
for standard values).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.
jsonBody
- the JSON body to use, as a String.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.
jsonBody
- the JSON body to use, as a JsonObject
.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.
body
- the raw body value to use, as a String.public RestBuilder accept(String type)
Sets the “Accept” standard header’s value. This is a convenience method equivalent to calling withHeader("Accept", type)
.
type
- the “Accept” type to use.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)
.
form
- the Form
builder object used to set form parameters.public com.couchbase.client.deps.io.netty.handler.codec.http.HttpMethod method()
HttpMethod
used for this request.public String path()
public Map<String,String> params()
public Map<String,Object> headers()
public String body()
public RestApiRequest asRequest()
RestApiRequest
message sent through the ClusterFacade
when executing this request.public RestApiResponse execute(long timeout, TimeUnit timeUnit)
Executes the API request in a synchronous fashion, using the given timeout.
timeout
- the custom timeout to use for the request.timeUnit
- the TimeUnit
for the timeout.RestApiResponse
.RuntimeException
- wrapping a TimeoutException
in case the request took too long.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
.
RestApiResponse
.RuntimeException
- wrapping a TimeoutException
in case the request took too long.Copyright © 2015 Couchbase, Inc.