Package com.linecorp.armeria.client
Class Clients
java.lang.Object
com.linecorp.armeria.client.Clients
-
Method Summary
Modifier and TypeMethodDescriptionstatic ClientBuilder
builder
(Scheme scheme, EndpointGroup endpointGroup) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedScheme
.static ClientBuilder
builder
(Scheme scheme, EndpointGroup endpointGroup, String path) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedScheme
andpath
.static ClientBuilder
builder
(SessionProtocol protocol, EndpointGroup endpointGroup) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
.static ClientBuilder
builder
(SessionProtocol protocol, EndpointGroup endpointGroup, String path) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
andpath
.static ClientBuilder
Returns a newClientBuilder
that builds the client that connects to the specifieduri
.static ClientBuilder
builder
(String scheme, EndpointGroup endpointGroup) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedscheme
.static ClientBuilder
builder
(String scheme, EndpointGroup endpointGroup, String path) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedscheme
andpath
.static ClientBuilder
Returns a newClientBuilder
that builds the client that connects to the specifiedURI
.static boolean
isUndefinedUri
(URI uri) Returnstrue
if the specifieduri
is an undefinedURI
, which signifies that aClient
, was created without aURI
orEndpointGroup
.static <T> T
newClient
(Scheme scheme, EndpointGroup endpointGroup, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedScheme
using the defaultClientFactory
.static <T> T
newClient
(Scheme scheme, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedScheme
andpath
using the defaultClientFactory
.static <T> T
newClient
(SessionProtocol protocol, EndpointGroup endpointGroup, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
using the defaultClientFactory
.static <T> T
newClient
(SessionProtocol protocol, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
andpath
using the defaultClientFactory
.static <T> T
newClient
(String scheme, EndpointGroup endpointGroup, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedscheme
using the defaultClientFactory
.static <T> T
newClient
(String scheme, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedscheme
andpath
using the defaultClientFactory
.static <T> T
Creates a new client that connects to the specifieduri
using the defaultClientFactory
.static <T> T
Creates a new client that connects to the specifiedURI
using the defaultClientFactory
.static ClientRequestContextCaptor
Prepare to capture theClientRequestContext
of the next request sent from the current thread.static <T> T
newDerivedClient
(T client, ClientOptionValue<?>... additionalOptions) Creates a new derived client that connects to the sameURI
with the specifiedclient
and the specifiedadditionalOptions
.static <T> T
newDerivedClient
(T client, Iterable<ClientOptionValue<?>> additionalOptions) Creates a new derived client that connects to the sameURI
with the specifiedclient
and the specifiedadditionalOptions
.static <T> T
newDerivedClient
(T client, Function<? super ClientOptions, ClientOptions> configurator) Creates a new derived client that connects to the sameURI
with the specifiedclient
but with differentClientOption
s.static <T> T
Unwraps the specified client into the object of the specifiedtype
.static SafeCloseable
withContextCustomizer
(Consumer<? super ClientRequestContext> contextCustomizer) Sets the specifiedClientRequestContext
customizer function in a thread-local variable so that the customized context is used when the client invokes a request from the current thread.static SafeCloseable
withHeader
(CharSequence name, Object value) Sets the specified HTTP header in a thread-local variable so that the header is sent by the client call made from the current thread.static SafeCloseable
withHeader
(CharSequence name, String value) Sets the specified HTTP header in a thread-local variable so that the header is sent by the client call made from the current thread.static SafeCloseable
withHeaders
(Consumer<HttpHeadersBuilder> headerMutator) Sets the specifiedConsumer
, which mutates HTTP headers, in a thread-local variable so that the mutated headers are sent by the client call made from the current thread.
-
Method Details
-
newClient
Creates a new client that connects to the specifieduri
using the defaultClientFactory
.- Parameters:
uri
- the URI of the server endpointclientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifieduri
is invalid, or the specifiedclientType
is unsupported for theuri
's scheme
-
newClient
Creates a new client that connects to the specifiedURI
using the defaultClientFactory
.- Parameters:
uri
- theURI
of the server endpointclientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedURI
is invalid, or the specifiedclientType
is unsupported for theURI
's scheme
-
newClient
Creates a new client that connects to the specifiedEndpointGroup
with the specifiedscheme
using the defaultClientFactory
.- Parameters:
scheme
- theScheme
represented as aString
endpointGroup
- the serverEndpointGroup
clientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedscheme
is invalid or the specifiedclientType
is unsupported for the specifiedscheme
.
-
newClient
public static <T> T newClient(String scheme, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedscheme
andpath
using the defaultClientFactory
.- Parameters:
scheme
- theScheme
represented as aString
endpointGroup
- the serverEndpointGroup
path
- the path to the endpointclientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedscheme
is invalid or the specifiedclientType
is unsupported for the specifiedscheme
.
-
newClient
Creates a new client that connects to the specifiedEndpointGroup
with the specifiedScheme
using the defaultClientFactory
.- Parameters:
scheme
- theScheme
endpointGroup
- the serverEndpointGroup
clientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedclientType
is unsupported for the specifiedScheme
.
-
newClient
public static <T> T newClient(Scheme scheme, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedScheme
andpath
using the defaultClientFactory
.- Parameters:
scheme
- theScheme
endpointGroup
- the serverEndpointGroup
path
- the path to the endpointclientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedclientType
is unsupported for the specifiedScheme
.
-
newClient
public static <T> T newClient(SessionProtocol protocol, EndpointGroup endpointGroup, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
using the defaultClientFactory
.- Parameters:
protocol
- theSessionProtocol
endpointGroup
- the serverEndpointGroup
clientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedclientType
is unsupported for the specifiedSessionProtocol
orSerializationFormat
is required.
-
newClient
public static <T> T newClient(SessionProtocol protocol, EndpointGroup endpointGroup, String path, Class<T> clientType) Creates a new client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
andpath
using the defaultClientFactory
.- Parameters:
protocol
- theSessionProtocol
endpointGroup
- the serverEndpointGroup
path
- the path to the endpointclientType
- the type of the new client- Throws:
IllegalArgumentException
- if the specifiedclientType
is unsupported for the specifiedSessionProtocol
orSerializationFormat
is required.
-
builder
Returns a newClientBuilder
that builds the client that connects to the specifieduri
.- Throws:
IllegalArgumentException
- if the specifieduri
is invalid, or the specifiedclientType
is unsupported for theuri
's scheme
-
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedURI
.- Throws:
IllegalArgumentException
- if the specifiedURI
is invalid, or the specifiedclientType
is unsupported for theURI
's scheme
-
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedscheme
.- Throws:
IllegalArgumentException
- if thescheme
is invalid.
-
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedscheme
andpath
.- Throws:
IllegalArgumentException
- if thescheme
is invalid.
-
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
. -
builder
public static ClientBuilder builder(SessionProtocol protocol, EndpointGroup endpointGroup, String path) Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedSessionProtocol
andpath
. -
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedScheme
. -
builder
Returns a newClientBuilder
that builds the client that connects to the specifiedEndpointGroup
with the specifiedScheme
andpath
. -
newDerivedClient
Creates a new derived client that connects to the sameURI
with the specifiedclient
and the specifiedadditionalOptions
. -
newDerivedClient
Creates a new derived client that connects to the sameURI
with the specifiedclient
and the specifiedadditionalOptions
. -
newDerivedClient
public static <T> T newDerivedClient(T client, Function<? super ClientOptions, ClientOptions> configurator) Creates a new derived client that connects to the sameURI
with the specifiedclient
but with differentClientOption
s. For example:WebClient derivedWebClient = Clients.newDerivedClient(webClient, options -> { ClientOptionsBuilder builder = options.toBuilder(); builder.decorator(...); // Add a decorator. builder.addHeader(...); // Add an HTTP header. return builder.build(); });
- Parameters:
configurator
- aFunction
whose input is the originalClientOptions
of the client being derived from and whose output is theClientOptions
of the new derived client- See Also:
-
unwrap
Unwraps the specified client into the object of the specifiedtype
. Use this method instead of an explicit downcast. For example:WebClient client = WebClient.builder(...) .decorator(LoggingClient.newDecorator()) .build(); LoggingClient unwrapped = Clients.unwrap(client, LoggingClient.class); // If the client implements Unwrappable, you can just use the 'as()' method. LoggingClient unwrapped2 = client.as(LoggingClient.class);
- Parameters:
type
- the type of the object to return- Returns:
- the object of the specified
type
if found, ornull
if not found. - See Also:
-
withHeader
Sets the specified HTTP header in a thread-local variable so that the header is sent by the client call made from the current thread. Use thetry-with-resources
block with the returnedSafeCloseable
to unset the thread-local variable automatically:
You can also nest the header manipulation:import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION; try (SafeCloseable ignored = withHeader(AUTHORIZATION, myCredential)) { client.executeSomething(..); }
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION; import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT; try (SafeCloseable ignored = withHeader(USER_AGENT, myAgent)) { for (String secret : secrets) { try (SafeCloseable ignored2 = withHeader(AUTHORIZATION, secret)) { // Both USER_AGENT and AUTHORIZATION will be set. client.executeSomething(..); } } }
- See Also:
-
withHeader
Sets the specified HTTP header in a thread-local variable so that the header is sent by the client call made from the current thread. Use thetry-with-resources
block with the returnedSafeCloseable
to unset the thread-local variable automatically:
You can also nest the header manipulation:import static com.linecorp.armeria.common.HttpHeaderNames.CONTENT_TYPE; import static com.linecorp.armeria.common.MediaType.JSON_UTF_8; try (SafeCloseable ignored = withHeader(CONTENT_TYPE, JSON_UTF_8)) { client.executeSomething(..); }
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION; import static com.linecorp.armeria.common.HttpHeaderNames.CONTENT_TYPE; import static com.linecorp.armeria.common.MediaType.JSON_UTF_8; try (SafeCloseable ignored = withHeader(CONTENT_TYPE, JSON_UTF_8)) { for (String secret : secrets) { try (SafeCloseable ignored2 = withHeader(AUTHORIZATION, secret)) { // Both CONTENT_TYPE and AUTHORIZATION will be set. client.executeSomething(..); } } }
- See Also:
-
withHeaders
Sets the specifiedConsumer
, which mutates HTTP headers, in a thread-local variable so that the mutated headers are sent by the client call made from the current thread. Use thetry-with-resources
block with the returnedSafeCloseable
to unset the thread-local variable automatically:
You can also nest the header mutation:import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION; import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT; try (SafeCloseable ignored = withHeaders(builder -> { builder.set(HttpHeaders.AUTHORIZATION, myCredential) .set(HttpHeaders.USER_AGENT, myAgent); })) { client.executeSomething(..); }
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION; import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT; try (SafeCloseable ignored = withHeaders(builder -> { builder.set(USER_AGENT, myAgent); })) { for (String secret : secrets) { try (SafeCloseable ignored2 = withHeaders(builder -> { builder.set(AUTHORIZATION, secret); })) { // Both USER_AGENT and AUTHORIZATION will be set. client.executeSomething(..); } } }
- See Also:
-
withContextCustomizer
@MustBeClosed public static SafeCloseable withContextCustomizer(Consumer<? super ClientRequestContext> contextCustomizer) Sets the specifiedClientRequestContext
customizer function in a thread-local variable so that the customized context is used when the client invokes a request from the current thread. The given customizer function is evaluated after the customizer function specified withClientBuilder.contextCustomizer(Consumer)
.Use the
try-with-resources
block with the returnedSafeCloseable
to unset the thread-local variable automatically:
You can also nest the request context customization:// Good: try (SafeCloseable ignored = withContextCustomizer(ctx -> { ctx.setAttr(USER_ID, userId); ctx.setAttr(USER_SECRET, secret); })) { client.executeSomething(..); } // Bad: try (SafeCloseable ignored = withContextCustomizer(ctx -> { ctx.setAttr(USER_ID, userId); ctx.setAttr(USER_SECRET, secret); })) { executor.execute(() -> { // The variables in USER_ID and USER_SECRET will not be propagated to the context. // client.executeSomething() must be invoked by the same thread // that called withContextCustomizer(). client.executeSomething(..); }); }
try (SafeCloseable ignored = withContextCustomizer(ctx -> ctx.setAttr(USER_ID, userId))) { String secret = client.getSecret(); try (SafeCloseable ignored2 = withContextCustomizer(ctx -> ctx.setAttr(USER_SECRET, secret))) { // Both USER_ID and USER_SECRET will be set. client.executeSomething(..); } }
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. -
newContextCaptor
Prepare to capture theClientRequestContext
of the next request sent from the current thread. Use thetry-with-resources
block with the returnedClientRequestContextCaptor
to retrieve the capturedClientRequestContext
and to unset the thread-local variable automatically.
Note that you can also capture more than onetry (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { WebClient.of().get("https://www.example.com/hello"); ClientRequestContext ctx = captor.get(); assert ctx.path().equals("/hello"); }
ClientRequestContext
:try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { WebClient.of().get("https://www.example.com/foo"); WebClient.of().get("https://www.example.com/bar"); List<ClientRequestContext> contexts = captor.getAll(); assert contexts.get(0).path().equals("/foo"); assert contexts.get(1).path().equals("/bar"); }
-
isUndefinedUri
Returnstrue
if the specifieduri
is an undefinedURI
, which signifies that aClient
, was created without aURI
orEndpointGroup
. For example,isUndefinedUri(WebClient.of().uri())
will returntrue
.
-