Package com.linecorp.armeria.client
Class RestClientPreparation
java.lang.Object
com.linecorp.armeria.client.RestClientPreparation
- All Implemented Interfaces:
RequestOptionsSetters
,RequestPreparationSetters
,HttpMessageSetters
,PathAndQueryParamSetters
@UnstableApi
public final class RestClientPreparation
extends Object
implements RequestPreparationSetters
Prepares and executes a new
HttpRequest
for RestClient
.-
Method Summary
Modifier and TypeMethodDescriptionattr
(AttributeKey<V> key, V value) Associates the specified value with the givenAttributeKey
in this request.Sets the content for this message.Sets the content for this message.content
(MediaType contentType, CharSequence content) Sets the content for this message.Sets the content for this message.Sets the content for this message.Sets thePublisher
for this message.Sets the content as UTF-8 for this message.Sets the content as UTF-8 for this message.Sets thePublisher
for this message.contentJson
(Object content) Sets the content for this message.Sets a cookie for this message.Sets multiple cookies for this message.Disables path parameters substitution.exchangeType
(ExchangeType exchangeType) <T> CompletableFuture<ResponseEntity<T>>
execute
(TypeReference<? extends T> typeRef) Sends the HTTP request and converts the JSON response body as theT
object using the defaultObjectMapper
.<T> CompletableFuture<ResponseEntity<T>>
execute
(TypeReference<? extends T> typeRef, ObjectMapper mapper) Sends the HTTP request and converts the JSON response body as theT
object using the specifiedObjectMapper
.<T> T
execute
(ResponseAs<HttpResponse, T> responseAs) Sends the HTTP request and converts theHttpResponse
using theResponseAs
.<T> CompletableFuture<ResponseEntity<T>>
Sends the HTTP request and converts the JSON response body as theT
object using the defaultObjectMapper
.<T> CompletableFuture<ResponseEntity<T>>
execute
(Class<? extends T> clazz, ObjectMapper mapper) Sends the HTTP request and converts the JSON response body as theT
object using the specifiedObjectMapper
.header
(CharSequence name, Object value) Adds a header for this message.headers
(Iterable<? extends Map.Entry<? extends CharSequence, String>> headers) Adds multiple headers for this message.maxResponseLength
(long maxResponseLength) Sets the maximum allowed length of a server response in bytes.Sets a path param for this message.pathParams
(Map<String, ?> pathParams) Sets multiple path params for this message.queryParam
(String name, Object value) Adds a query param for this message.queryParams
(Iterable<? extends Map.Entry<? extends String, String>> queryParams) Adds multiple query params for this message.requestAutoAbortDelay
(Duration delay) Sets the amount of time to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete.requestAutoAbortDelayMillis
(long delayMillis) Sets the amount of time in millis to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete.requestOptions
(RequestOptions requestOptions) Sets the specifiedRequestOptions
that could overwrite the previously configured values such asRequestOptionsSetters.responseTimeout(Duration)
,RequestOptionsSetters.writeTimeout(Duration)
,RequestOptionsSetters.maxResponseLength(long)
andRequestOptionsSetters.attr(AttributeKey, Object)
.responseTimeout
(Duration responseTimeout) responseTimeoutMillis
(long responseTimeoutMillis) trailer
(CharSequence name, Object value) Adds an HTTP trailer for this message.trailers
(Iterable<? extends Map.Entry<? extends CharSequence, String>> trailers) Adds HTTP trailers for this message.writeTimeout
(Duration writeTimeout) Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.writeTimeoutMillis
(long writeTimeoutMillis) Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.
-
Method Details
-
execute
Sends the HTTP request and converts the JSON response body as theT
object using the defaultObjectMapper
.- See Also:
-
execute
public <T> CompletableFuture<ResponseEntity<T>> execute(Class<? extends T> clazz, ObjectMapper mapper) Sends the HTTP request and converts the JSON response body as theT
object using the specifiedObjectMapper
. -
execute
Sends the HTTP request and converts the JSON response body as theT
object using the defaultObjectMapper
.- See Also:
-
execute
public <T> CompletableFuture<ResponseEntity<T>> execute(TypeReference<? extends T> typeRef, ObjectMapper mapper) Sends the HTTP request and converts the JSON response body as theT
object using the specifiedObjectMapper
. -
execute
Sends the HTTP request and converts theHttpResponse
using theResponseAs
. -
pathParam
Description copied from interface:PathAndQueryParamSetters
Sets a path param for this message.- Specified by:
pathParam
in interfacePathAndQueryParamSetters
-
pathParams
Description copied from interface:PathAndQueryParamSetters
Sets multiple path params for this message.- Specified by:
pathParams
in interfacePathAndQueryParamSetters
-
disablePathParams
Description copied from interface:PathAndQueryParamSetters
Disables path parameters substitution. If path parameter is not disabled and a parameter is specified using{}
or:
, value is not found, anIllegalStateException
is thrown.- Specified by:
disablePathParams
in interfacePathAndQueryParamSetters
-
queryParam
Description copied from interface:PathAndQueryParamSetters
Adds a query param for this message.- Specified by:
queryParam
in interfacePathAndQueryParamSetters
-
queryParams
public RestClientPreparation queryParams(Iterable<? extends Map.Entry<? extends String, String>> queryParams) Description copied from interface:PathAndQueryParamSetters
Adds multiple query params for this message.- Specified by:
queryParams
in interfacePathAndQueryParamSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content as UTF-8 for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content as UTF-8 for this message. Thecontent
is formatted byString.format(Locale, String, Object...)
with English locale.- Specified by:
content
in interfaceHttpMessageSetters
-
content
@FormatMethod public RestClientPreparation content(MediaType contentType, @FormatString String format, Object... content) Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
is formatted byString.format(Locale, String, Object...)
with English locale.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
will be wrapped usingHttpData.wrap(byte[])
, so any changes made tocontent
will be reflected in the request.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
content
Description copied from interface:HttpMessageSetters
Sets thePublisher
for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
content
public RestClientPreparation content(MediaType contentType, org.reactivestreams.Publisher<? extends HttpData> content) Description copied from interface:HttpMessageSetters
Sets thePublisher
for this message.- Specified by:
content
in interfaceHttpMessageSetters
-
contentJson
Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
is converted into JSON format using the defaultObjectMapper
.- Specified by:
contentJson
in interfaceHttpMessageSetters
-
header
Description copied from interface:HttpMessageSetters
Adds a header for this message.- Specified by:
header
in interfaceHttpMessageSetters
-
headers
public RestClientPreparation headers(Iterable<? extends Map.Entry<? extends CharSequence, String>> headers) Description copied from interface:HttpMessageSetters
Adds multiple headers for this message.- Specified by:
headers
in interfaceHttpMessageSetters
- See Also:
-
trailer
Description copied from interface:HttpMessageSetters
Adds an HTTP trailer for this message.- Specified by:
trailer
in interfaceHttpMessageSetters
-
trailers
public RestClientPreparation trailers(Iterable<? extends Map.Entry<? extends CharSequence, String>> trailers) Description copied from interface:HttpMessageSetters
Adds HTTP trailers for this message.- Specified by:
trailers
in interfaceHttpMessageSetters
-
cookie
Description copied from interface:HttpMessageSetters
Sets a cookie for this message.- Specified by:
cookie
in interfaceHttpMessageSetters
- See Also:
-
cookies
Description copied from interface:HttpMessageSetters
Sets multiple cookies for this message.- Specified by:
cookies
in interfaceHttpMessageSetters
- See Also:
-
responseTimeout
Description copied from interface:RequestOptionsSetters
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedDuration
since theResponse
started orRequest
was fully sent.Duration.ZERO
disables the limit.- Specified by:
responseTimeout
in interfaceRequestOptionsSetters
-
responseTimeoutMillis
Description copied from interface:RequestOptionsSetters
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent.0
disables the limit.- Specified by:
responseTimeoutMillis
in interfaceRequestOptionsSetters
-
writeTimeout
Description copied from interface:RequestOptionsSetters
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.Duration.ZERO
disables the limit.- Specified by:
writeTimeout
in interfaceRequestOptionsSetters
-
writeTimeoutMillis
Description copied from interface:RequestOptionsSetters
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.0
disables the limit.- Specified by:
writeTimeoutMillis
in interfaceRequestOptionsSetters
-
maxResponseLength
Description copied from interface:RequestOptionsSetters
Sets the maximum allowed length of a server response in bytes.0
disables the limit.- Specified by:
maxResponseLength
in interfaceRequestOptionsSetters
-
requestAutoAbortDelay
Description copied from interface:RequestOptionsSetters
Sets the amount of time to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete. This may be useful when you want to send additional data even after the response is complete. SpecifyDuration.ZERO
to abort theHttpRequest
immediately. Any negative value will not abort the request automatically. There is no delay by default.- Specified by:
requestAutoAbortDelay
in interfaceRequestOptionsSetters
-
requestAutoAbortDelayMillis
Description copied from interface:RequestOptionsSetters
Sets the amount of time in millis to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete. This may be useful when you want to send additional data even after the response is complete. Specify0
to abort theHttpRequest
immediately. Any negative value will not abort the request automatically. There is no delay by default.- Specified by:
requestAutoAbortDelayMillis
in interfaceRequestOptionsSetters
-
attr
Description copied from interface:RequestOptionsSetters
Associates the specified value with the givenAttributeKey
in this request. If this context previously contained a mapping for theAttributeKey
, the old value is replaced by the specified value.- Specified by:
attr
in interfaceRequestOptionsSetters
-
exchangeType
Description copied from interface:RequestOptionsSetters
Sets theExchangeType
that determines whether to stream anHttpRequest
orHttpResponse
. Note that anHttpRequest
will be aggregated before being written ifExchangeType.UNARY
orExchangeType.RESPONSE_STREAMING
is set. If unspecified, theClient
s try to infer a properExchangeType
depending on the content type of a request and a response. Here are examples:WebClient client = WebClient.of("https://armeria.dev"); try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.prepare() .post("/api/v1/items") .contentJson(new Item(...)) // A non-streaming request type. .asString() // A non-streaming response type. .execute(); assert captor.get().exchangeType() == ExchangeType.UNARY; } try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.get("/api/v1/items") // A non-streaming request type. .aggregate(); // A return type is not specified; Assuming that response streaming // is enabled. assert captor.get().exchangeType() == ExchangeType.RESPONSE_STREAMING; } try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.prepare() .post("/api/v1/items") .content(MediaType.JSON_LINES, StreamMessage.of(...)) // A streaming request type. .asFile(Path.get("/path/to/destination")) // A streaming response type. .execute(); assert captor.get().exchangeType() == ExchangeType.BIDI_STREAMING; }
Since a request and a response of
BlockingWebClient
are fully aggregated,ExchangeType.UNARY
is only supported.try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { AggregatedHttpResponse response = client.blocking().get("/api/v1/items"); assert captor.get().exchangeType() == ExchangeType.UNARY; }
gRPC clients
An
ExchangeType
is automatically inferred from theio.grpc.MethodDescriptor.MethodType
.try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { Response response = grpcClient.unaryCall(...); assert captor.get().exchangeType() == ExchangeType.UNARY; }
Thrift clients
Thrift protocols do not support streaming.
ExchangeType.UNARY
is only supported.- Specified by:
exchangeType
in interfaceRequestOptionsSetters
-
requestOptions
Description copied from interface:RequestPreparationSetters
Sets the specifiedRequestOptions
that could overwrite the previously configured values such asRequestOptionsSetters.responseTimeout(Duration)
,RequestOptionsSetters.writeTimeout(Duration)
,RequestOptionsSetters.maxResponseLength(long)
andRequestOptionsSetters.attr(AttributeKey, Object)
.- Specified by:
requestOptions
in interfaceRequestPreparationSetters
-