Class ArmeriaRetrofitBuilder
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddCallAdapterFactory
(CallAdapter.Factory factory) Adds the specified call adapter factory for supporting service method return types other thanCall
.addConverterFactory
(Converter.Factory factory) Adds the specified converter factory for serialization and deserialization of objects.addHeader
(CharSequence name, Object value) Adds the default HTTP header for anHttpRequest
that will be sent by thisClient
.addHeaders
(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Adds the default HTTP headers for anHttpRequest
that will be sent by thisClient
.Sets theAuthToken
header usingHttpHeaderNames.AUTHORIZATION
.auth
(BasicToken token) Sets the HTTP basic access authentication header usingHttpHeaderNames.AUTHORIZATION
.auth
(OAuth1aToken token) Sets the OAuth Core 1.0 Revision A header usingHttpHeaderNames.AUTHORIZATION
.auth
(OAuth2Token token) Sets the OAuth 2.0 header usingHttpHeaderNames.AUTHORIZATION
.build()
Returns a newly-createdRetrofit
based on the properties of this builder.callbackExecutor
(Executor executor) Clears all HTTP-level and RPC-level decorators set so far.contextCustomizer
(Consumer<? super ClientRequestContext> contextCustomizer) Adds the specifiedClientRequestContext
customizer function so that it customizes the context in the thread that initiated the client call.decorator
(DecoratingHttpClientFunction decorator) Adds the specified HTTP-leveldecorator
.decorator
(Function<? super HttpClient, ? extends HttpClient> decorator) Adds the specified HTTP-leveldecorator
.endpointRemapper
(Function<? super Endpoint, ? extends EndpointGroup> endpointRemapper) factory
(ClientFactory factory) Sets theClientFactory
used for creating a client.Enables automatic redirection.followRedirects
(RedirectConfig redirectConfig) Sets theRedirectConfig
to enable automatic redirection.maxResponseLength
(long maxResponseLength) Sets the maximum allowed length of a server response in bytes.nonBaseClientFactory
(BiFunction<? super SessionProtocol, ? super Endpoint, ? extends WebClient> nonBaseClientFactory) Specifies theBiFunction
that creates a new non-baseWebClient
, which is used for sending requests to other authorities than that of base URL.option
(ClientOption<T> option, T value) Adds the specifiedClientOption
and itsvalue
.option
(ClientOptionValue<T> optionValue) Adds the specifiedClientOptionValue
.options
(ClientOptions options) Adds the specifiedClientOptions
.options
(ClientOptionValue<?>... options) Adds the specifiedClientOptionValue
s.options
(Iterable<ClientOptionValue<?>> options) Adds the specifiedClientOptionValue
s.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.requestIdGenerator
(Supplier<RequestId> requestIdGenerator) responseTimeout
(Duration responseTimeout) Sets the timeout of a response.responseTimeoutMillis
(long responseTimeoutMillis) Sets the timeout of a response in milliseconds.rpcDecorator
(DecoratingRpcClientFunction decorator) Raises anUnsupportedOperationException
because this builder doesn't support RPC-level but only HTTP-level decorators.rpcDecorator
(Function<? super RpcClient, ? extends RpcClient> decorator) Raises anUnsupportedOperationException
because this builder doesn't support RPC-level but only HTTP-level decorators.setHeader
(CharSequence name, Object value) Sets the default HTTP header for anHttpRequest
that will be sent by thisClient
.setHeaders
(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Sets the default HTTP headers for anHttpRequest
that will be sent by thisClient
.streaming
(boolean streaming) Sets the streaming flag to make Armeria client fully supportStreaming
.successFunction
(SuccessFunction successFunction) Sets aSuccessFunction
that determines whether a request was handled successfully or not.validateEagerly
(boolean validateEagerly) When callingRetrofit.create(java.lang.Class<T>)
on the resultingRetrofit
instance, eagerly validate the configuration of all methods in the supplied interface.writeTimeout
(Duration writeTimeout) Sets the timeout of a socket write attempt.writeTimeoutMillis
(long writeTimeoutMillis) Sets the timeout of a socket write attempt in milliseconds.Methods inherited from class com.linecorp.armeria.client.AbstractClientOptionsBuilder
buildOptions, buildOptions, contextHook
-
Method Details
-
nonBaseClientFactory
public ArmeriaRetrofitBuilder nonBaseClientFactory(BiFunction<? super SessionProtocol, ? super Endpoint, ? extends WebClient> nonBaseClientFactory) Specifies theBiFunction
that creates a new non-baseWebClient
, which is used for sending requests to other authorities than that of base URL. If not specified, the non-baseWebClient
will have the same options with the baseWebClient
, which was specified withArmeriaRetrofit.of(WebClient)
orArmeriaRetrofit.builder(WebClient)
.To avoid the overhead of repetitive instantiation of
WebClient
s, theWebClient
s returned by the specifiedBiFunction
will be cached for each combination of:- Whether the connection is secured (HTTPS or HTTPS)
- Host name
- Port number
You can use this method to create a customized non-base
WebClient
, for example to send an additional header, override the timeout or enforce HTTP/1:ArmeriaRetrofit.builder("http://example.com/") .nonBaseClientFactory((protocol, endpoint) -> { // Enforce HTTP/1. final SessionProtocol actualProtocol = protocol.isTls() ? SessionProtocol.H1 : SessionProtocol.H1C; return WebClient.builder(actualProtocol, endpoint) // Derive most settings from 'defaultWebClient'. .factory(defaultWebClient.factory()) .options(defaultWebClient.options()) // Set a custom header. .setHeader(HttpHeaderNames.AUTHORIZATION, "bearer my-access-token") // Override the timeout. .responseTimeout(Duration.ofSeconds(30)) .build(); }) .build();
Note that the specified
BiFunction
is not used for sending requests to the base URL's authority. The defaultWebClient
specified withArmeriaRetrofit.of(WebClient)
orArmeriaRetrofit.builder(WebClient)
will be used instead for such requests:// No need to use 'nonBaseClientFactory()' method. ArmeriaRetrofit.of(WebClient.builder("http://example.com/") .setHeader(HttpHeaderNames.AUTHORIZATION, "bearer my-access-token") .build());
-
addConverterFactory
Adds the specified converter factory for serialization and deserialization of objects.- See Also:
-
addCallAdapterFactory
Adds the specified call adapter factory for supporting service method return types other thanCall
.- See Also:
-
callbackExecutor
Sets theExecutor
on whichCallback
methods are invoked when returningCall
from your service method.Note:
executor
is not used forcustom method return types
.- See Also:
-
streaming
Sets the streaming flag to make Armeria client fully supportStreaming
. If this flag isfalse
, Armeria client will buffer all data and call a callback after receiving the data completely, even if a service method was annotated withStreaming
. By enabling this flag, Armeria client will use theExecutor
specified withcallbackExecutor(Executor)
to read the data in a blocking way.Note: It is not recommended to have the service methods both with and without the
Streaming
annotation in the same interface. Consider separating them into different interfaces and using different builder to build the service.- See Also:
-
validateEagerly
When callingRetrofit.create(java.lang.Class<T>)
on the resultingRetrofit
instance, eagerly validate the configuration of all methods in the supplied interface.- See Also:
-
build
Returns a newly-createdRetrofit
based on the properties of this builder. -
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptions
.- Overrides:
options
in classAbstractClientOptionsBuilder
-
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
s.- Overrides:
options
in classAbstractClientOptionsBuilder
-
options
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
s.- Overrides:
options
in classAbstractClientOptionsBuilder
-
option
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOption
and itsvalue
.- Overrides:
option
in classAbstractClientOptionsBuilder
-
option
Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientOptionValue
.- Overrides:
option
in classAbstractClientOptionsBuilder
-
factory
Description copied from class:AbstractClientOptionsBuilder
Sets theClientFactory
used for creating a client. The default isClientFactory.ofDefault()
.- Overrides:
factory
in classAbstractClientOptionsBuilder
-
writeTimeout
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a socket write attempt.- Overrides:
writeTimeout
in classAbstractClientOptionsBuilder
- Parameters:
writeTimeout
- the timeout.0
disables the timeout.
-
writeTimeoutMillis
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a socket write attempt in milliseconds.- Overrides:
writeTimeoutMillis
in classAbstractClientOptionsBuilder
- Parameters:
writeTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
responseTimeout
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a response.- Overrides:
responseTimeout
in classAbstractClientOptionsBuilder
- Parameters:
responseTimeout
- the timeout.0
disables the timeout.
-
responseTimeoutMillis
Description copied from class:AbstractClientOptionsBuilder
Sets the timeout of a response in milliseconds.- Overrides:
responseTimeoutMillis
in classAbstractClientOptionsBuilder
- Parameters:
responseTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
maxResponseLength
Description copied from class:AbstractClientOptionsBuilder
Sets the maximum allowed length of a server response in bytes.- Overrides:
maxResponseLength
in classAbstractClientOptionsBuilder
- Parameters:
maxResponseLength
- the maximum length in bytes.0
disables the limit.
-
requestAutoAbortDelay
Description copied from class:AbstractClientOptionsBuilder
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.- Overrides:
requestAutoAbortDelay
in classAbstractClientOptionsBuilder
-
requestAutoAbortDelayMillis
Description copied from class:AbstractClientOptionsBuilder
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.- Overrides:
requestAutoAbortDelayMillis
in classAbstractClientOptionsBuilder
-
requestIdGenerator
Description copied from class:AbstractClientOptionsBuilder
- Overrides:
requestIdGenerator
in classAbstractClientOptionsBuilder
-
successFunction
Description copied from class:AbstractClientOptionsBuilder
Sets aSuccessFunction
that determines whether a request was handled successfully or not. If unspecified,SuccessFunction.ofDefault()
is used.- Overrides:
successFunction
in classAbstractClientOptionsBuilder
-
endpointRemapper
public ArmeriaRetrofitBuilder endpointRemapper(Function<? super Endpoint, ? extends EndpointGroup> endpointRemapper) Description copied from class:AbstractClientOptionsBuilder
Sets aFunction
that remaps anEndpoint
into anEndpointGroup
. ThisClientOption
is useful when you need to override a single target host into a group of hosts to enable client-side load-balancing, e.g.MyService.Iface client = Clients.newClient("tbinary+http://example.com/api", MyService.Iface.class); EndpointGroup myGroup = EndpointGroup.of(Endpoint.of("node-1.example.com")), Endpoint.of("node-2.example.com"))); MyService.Iface derivedClient = Clients.newDerivedClient(client, options -> { return options.toBuilder() .endpointRemapper(endpoint -> { if (endpoint.host().equals("example.com")) { return myGroup; } else { return endpoint; } }) .build(); }); // This request goes to 'node-1.example.com' or 'node-2.example.com'. derivedClient.call();
Note that the remapping does not occur recursively but only once.
- Overrides:
endpointRemapper
in classAbstractClientOptionsBuilder
- See Also:
-
decorator
public ArmeriaRetrofitBuilder decorator(Function<? super HttpClient, ? extends HttpClient> decorator) Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP-leveldecorator
.- Overrides:
decorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theFunction
that transforms anHttpClient
to another
-
decorator
Description copied from class:AbstractClientOptionsBuilder
Adds the specified HTTP-leveldecorator
.- Overrides:
decorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theDecoratingHttpClientFunction
that intercepts an invocation
-
clearDecorators
Description copied from class:AbstractClientOptionsBuilder
Clears all HTTP-level and RPC-level decorators set so far.- Overrides:
clearDecorators
in classAbstractClientOptionsBuilder
-
rpcDecorator
public ArmeriaRetrofitBuilder rpcDecorator(Function<? super RpcClient, ? extends RpcClient> decorator) Raises anUnsupportedOperationException
because this builder doesn't support RPC-level but only HTTP-level decorators.- Overrides:
rpcDecorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theFunction
that transforms anRpcClient
to another
-
rpcDecorator
Raises anUnsupportedOperationException
because this builder doesn't support RPC-level but only HTTP-level decorators.- Overrides:
rpcDecorator
in classAbstractClientOptionsBuilder
- Parameters:
decorator
- theDecoratingRpcClientFunction
that intercepts an invocation
-
addHeader
Description copied from class:AbstractClientOptionsBuilder
Adds the default HTTP header for anHttpRequest
that will be sent by thisClient
.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNames
are defined in theHttpRequest.headers()
orClientRequestContext.additionalRequestHeaders()
.- Overrides:
addHeader
in classAbstractClientOptionsBuilder
-
addHeaders
public ArmeriaRetrofitBuilder addHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Description copied from class:AbstractClientOptionsBuilder
Adds the default HTTP headers for anHttpRequest
that will be sent by thisClient
.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNames
are defined in theHttpRequest.headers()
orClientRequestContext.additionalRequestHeaders()
.- Overrides:
addHeaders
in classAbstractClientOptionsBuilder
-
setHeader
Description copied from class:AbstractClientOptionsBuilder
Sets the default HTTP header for anHttpRequest
that will be sent by thisClient
.Note that the default HTTP header could be overridden if the same
HttpHeaderNames
are defined inHttpRequest.headers()
orClientRequestContext.additionalRequestHeaders()
.- Overrides:
setHeader
in classAbstractClientOptionsBuilder
-
setHeaders
public ArmeriaRetrofitBuilder setHeaders(Iterable<? extends Map.Entry<? extends CharSequence, ?>> headers) Description copied from class:AbstractClientOptionsBuilder
Sets the default HTTP headers for anHttpRequest
that will be sent by thisClient
.Note that the values of the default HTTP headers could be overridden if the same
HttpHeaderNames
are defined inHttpRequest.headers()
orClientRequestContext.additionalRequestHeaders()
.- Overrides:
setHeaders
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the HTTP basic access authentication header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the OAuth Core 1.0 Revision A header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets the OAuth 2.0 header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
auth
Description copied from class:AbstractClientOptionsBuilder
Sets theAuthToken
header usingHttpHeaderNames.AUTHORIZATION
.- Overrides:
auth
in classAbstractClientOptionsBuilder
-
followRedirects
Description copied from class:AbstractClientOptionsBuilder
Enables automatic redirection.- Overrides:
followRedirects
in classAbstractClientOptionsBuilder
-
followRedirects
Description copied from class:AbstractClientOptionsBuilder
Sets theRedirectConfig
to enable automatic redirection.- Overrides:
followRedirects
in classAbstractClientOptionsBuilder
-
contextCustomizer
public ArmeriaRetrofitBuilder contextCustomizer(Consumer<? super ClientRequestContext> contextCustomizer) Description copied from class:AbstractClientOptionsBuilder
Adds the specifiedClientRequestContext
customizer function so that it customizes the context in the thread that initiated the client call. The given customizer function is evaluated before the customizer function specified byClients.withContextCustomizer(Consumer)
.static final ThreadLocal<String> USER_ID = new ThreadLocal<>(); static final AttributeKey<String> USER_ID_ATTR = AttributeKey.valueOf("USER_ID"); ... MyClientStub client = Clients.builder(...) .contextCustomizer(ctx -> { // This customizer will be invoked from the thread that initiates a client call. ctx.setAttr(USER_ID_ATTR, USER_ID.get()); }) .build(MyClientStub.class); // Good: // The context data is set by the thread that initiates the client call. USER_ID.set("user1"); try { client.executeSomething1(..); ... client.executeSomethingN(..); } finally { // Should clean up the thread local storage. USER_ID.remove(); } // Bad: USER_ID.set("user1"); executor.execute(() -> { // The variable in USER_ID won't be propagated to the context. // The variable is not valid at the moment client.executeSomething1() is called. client.executeSomething1(..); });
Note that certain properties of
may beClientRequestContext
, such as:null
while the customizer function runs, because the target host of theRequest
is not determined yet.- Overrides:
contextCustomizer
in classAbstractClientOptionsBuilder
- See Also:
-