public final class Clients extends Object
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
newClient(ClientFactory factory,
Scheme scheme,
Endpoint endpoint,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
Endpoint with the Scheme using
the specified ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
Scheme scheme,
Endpoint endpoint,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
Endpoint with the Scheme using
the specified ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
SessionProtocol protocol,
SerializationFormat format,
Endpoint endpoint,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
Endpoint with the SessionProtocol and
the SerializationFormat using the specified ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
SessionProtocol protocol,
SerializationFormat format,
Endpoint endpoint,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
Endpoint with the SessionProtocol and
the SerializationFormat using the specified ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
String uri,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
uri using the specified
ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
String uri,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
uri using the specified
ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
URI uri,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
URI using the specified
ClientFactory. |
static <T> T |
newClient(ClientFactory factory,
URI uri,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
URI using the specified
ClientFactory. |
static <T> T |
newClient(Scheme scheme,
Endpoint endpoint,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
Endpoint with the Scheme using
the default ClientFactory. |
static <T> T |
newClient(Scheme scheme,
Endpoint endpoint,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
Endpoint with the Scheme using
the default ClientFactory. |
static <T> T |
newClient(SessionProtocol protocol,
SerializationFormat format,
Endpoint endpoint,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
Endpoint with the SessionProtocol and
the SerializationFormat using the default ClientFactory. |
static <T> T |
newClient(SessionProtocol protocol,
SerializationFormat format,
Endpoint endpoint,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
Endpoint with the SessionProtocol and
the SerializationFormat using the default ClientFactory. |
static <T> T |
newClient(String uri,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
uri using the default
ClientFactory. |
static <T> T |
newClient(String uri,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
uri using the default
ClientFactory. |
static <T> T |
newClient(URI uri,
Class<T> clientType,
ClientOptions options)
Creates a new client that connects to the specified
URI using the default
ClientFactory. |
static <T> T |
newClient(URI uri,
Class<T> clientType,
ClientOptionValue<?>... options)
Creates a new client that connects to the specified
URI using the default
ClientFactory. |
static <T> T |
newDerivedClient(T client,
ClientOptionValue<?>... additionalOptions)
Creates a new derived client that connects to the same
URI with the specified client
and the specified additionalOptions. |
static <T> T |
newDerivedClient(T client,
Function<? super ClientOptions,ClientOptions> configurator)
Creates a new derived client that connects to the same
URI with the specified client
but with different ClientOptions. |
static <T> T |
newDerivedClient(T client,
Iterable<ClientOptionValue<?>> additionalOptions)
Creates a new derived client that connects to the same
URI with the specified client
and the specified additionalOptions. |
static <T> Optional<T> |
unwrap(Object client,
Class<T> type)
Unwraps the specified client into the object of the specified
type. |
static SafeCloseable |
withContextCustomizer(Consumer<ClientRequestContext> contextCustomizer)
Sets the specified
ClientRequestContext customization 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 |
withHttpHeader(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 |
withHttpHeader(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 |
withHttpHeaders(Function<HttpHeaders,HttpHeaders> headerManipulator)
Sets the specified HTTP header manipulating function in a thread-local variable so that the manipulated
headers are sent by the client call made from the current thread.
|
public static <T> T newClient(String uri, Class<T> clientType, ClientOptionValue<?>... options)
uri using the default
ClientFactory.uri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(String uri, Class<T> clientType, ClientOptions options)
uri using the default
ClientFactory.uri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(ClientFactory factory, String uri, Class<T> clientType, ClientOptionValue<?>... options)
uri using the specified
ClientFactory.factory - an alternative ClientFactoryuri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(ClientFactory factory, String uri, Class<T> clientType, ClientOptions options)
uri using the specified
ClientFactory.factory - an alternative ClientFactoryuri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(URI uri, Class<T> clientType, ClientOptionValue<?>... options)
URI using the default
ClientFactory.uri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(URI uri, Class<T> clientType, ClientOptions options)
URI using the default
ClientFactory.uri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(ClientFactory factory, URI uri, Class<T> clientType, ClientOptionValue<?>... options)
URI using the specified
ClientFactory.factory - an alternative ClientFactoryuri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(ClientFactory factory, URI uri, Class<T> clientType, ClientOptions options)
URI using the specified
ClientFactory.factory - an alternative ClientFactoryuri - the URI of the server endpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified uri or
the specified clientType is unsupported for the schemepublic static <T> T newClient(SessionProtocol protocol, SerializationFormat format, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
Endpoint with the SessionProtocol and
the SerializationFormat using the default ClientFactory.protocol - the session protocolformat - the SerializationFormat for remote procedure callendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified SessionProtocol and
SerializationFormat, or the specified clientType is
unsupported for the schemepublic static <T> T newClient(SessionProtocol protocol, SerializationFormat format, Endpoint endpoint, Class<T> clientType, ClientOptions options)
Endpoint with the SessionProtocol and
the SerializationFormat using the default ClientFactory.protocol - the session protocolformat - the SerializationFormat for remote procedure callendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified SessionProtocol and
SerializationFormat, or the specified clientType is
unsupported for the schemepublic static <T> T newClient(ClientFactory factory, SessionProtocol protocol, SerializationFormat format, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
Endpoint with the SessionProtocol and
the SerializationFormat using the specified ClientFactory.factory - an alternative ClientFactoryprotocol - the session protocolformat - the SerializationFormat for remote procedure callendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the scheme of the specified SessionProtocol and
SerializationFormat, or the specified clientType is
unsupported for the schemepublic static <T> T newClient(ClientFactory factory, SessionProtocol protocol, SerializationFormat format, Endpoint endpoint, Class<T> clientType, ClientOptions options)
Endpoint with the SessionProtocol and
the SerializationFormat using the specified ClientFactory.factory - an alternative ClientFactoryprotocol - the session protocolformat - the SerializationFormat for remote procedure callendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the scheme of the specified SessionProtocol and
SerializationFormat, or the specified clientType is
unsupported for the schemepublic static <T> T newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
Endpoint with the Scheme using
the default ClientFactory.scheme - the Schemeendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the specified Scheme or the specified clientType is
unsupported for the schemepublic static <T> T newClient(Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptions options)
Endpoint with the Scheme using
the default ClientFactory.scheme - the Schemeendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the specified Scheme or the specified clientType is
unsupported for the schemepublic static <T> T newClient(ClientFactory factory, Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptionValue<?>... options)
Endpoint with the Scheme using
the specified ClientFactory.factory - an alternative ClientFactoryscheme - the Schemeendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionValuesIllegalArgumentException - if the specified Scheme or the specified clientType is
unsupported for the schemepublic static <T> T newClient(ClientFactory factory, Scheme scheme, Endpoint endpoint, Class<T> clientType, ClientOptions options)
Endpoint with the Scheme using
the specified ClientFactory.factory - an alternative ClientFactoryscheme - the Schemeendpoint - the server EndpointclientType - the type of the new clientoptions - the ClientOptionsIllegalArgumentException - if the specified Scheme or the specified clientType is
unsupported for the schemepublic static <T> T newDerivedClient(T client,
ClientOptionValue<?>... additionalOptions)
URI with the specified client
and the specified additionalOptions.public static <T> T newDerivedClient(T client,
Iterable<ClientOptionValue<?>> additionalOptions)
URI with the specified client
and the specified additionalOptions.public static <T> T newDerivedClient(T client,
Function<? super ClientOptions,ClientOptions> configurator)
URI with the specified client
but with different ClientOptions. For example:
WebClient derivedWebClient = Clients.newDerivedClient(webClient, options -> {
ClientOptionsBuilder builder = new ClientOptionsBuilder(options);
builder.decorator(...); // Add a decorator.
builder.addHttpHeader(...); // Add an HTTP header.
return builder.build();
});
configurator - a Function whose input is the original ClientOptions of the client
being derived from and whose output is the ClientOptions of the new derived
clientClientBuilder, for more information about how the base options and
additional options are merged when a derived client is created.,
ClientOptionsBuilderpublic static <T> Optional<T> unwrap(Object client, Class<T> type)
type.
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).get();
// If the client implements Unwrappable, you can just use the 'as()' method.
LoggingClient unwrapped2 = client.as(LoggingClient.class).get();
type - the type of the object to returntype if found. Optional.empty() if not found.Client.as(Class),
ClientFactory.unwrap(Object, Class),
Unwrappablepublic static SafeCloseable withHttpHeader(CharSequence name, String value)
try-with-resources block with the returned
SafeCloseable to unset the thread-local variable automatically:
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
try (SafeCloseable ignored = withHttpHeader(AUTHORIZATION, myCredential)) {
client.executeSomething(..);
}
You can also nest the header manipulation:
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT;
try (SafeCloseable ignored = withHttpHeader(USER_AGENT, myAgent)) {
for (String secret : secrets) {
try (SafeCloseable ignored2 = withHttpHeader(AUTHORIZATION, secret)) {
// Both USER_AGENT and AUTHORIZATION will be set.
client.executeSomething(..);
}
}
}
withHttpHeaders(Function)public static SafeCloseable withHttpHeader(CharSequence name, Object value)
try-with-resources block with the returned
SafeCloseable to unset the thread-local variable automatically:
import static com.linecorp.armeria.common.HttpHeaderNames.CONTENT_TYPE;
import static com.linecorp.armeria.common.MediaType.JSON_UTF_8;
try (SafeCloseable ignored = withHttpHeader(CONTENT_TYPE, JSON_UTF_8)) {
client.executeSomething(..);
}
You can also nest the header manipulation:
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 = withHttpHeader(CONTENT_TYPE, JSON_UTF_8)) {
for (String secret : secrets) {
try (SafeCloseable ignored2 = withHttpHeader(AUTHORIZATION, secret)) {
// Both CONTENT_TYPE and AUTHORIZATION will be set.
client.executeSomething(..);
}
}
}
withHttpHeaders(Function)public static SafeCloseable withHttpHeaders(Function<HttpHeaders,HttpHeaders> headerManipulator)
try-with-resources
block with the returned SafeCloseable to unset the thread-local variable automatically:
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT;
try (SafeCloseable ignored = withHttpHeaders(headers -> {
return headers.toBuilder()
.set(HttpHeaders.AUTHORIZATION, myCredential)
.set(HttpHeaders.USER_AGENT, myAgent)
.build();
})) {
client.executeSomething(..);
}
You can also nest the header manipulation:
import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT;
try (SafeCloseable ignored = withHttpHeaders(h -> {
return h.toBuilder()
.set(USER_AGENT, myAgent)
.build();
})) {
for (String secret : secrets) {
try (SafeCloseable ignored2 = withHttpHeaders(h -> {
return h.toBuilder()
.set(AUTHORIZATION, secret)
.build();
})) {
// Both USER_AGENT and AUTHORIZATION will be set.
client.executeSomething(..);
}
}
}
withHttpHeader(CharSequence, String)public static SafeCloseable withContextCustomizer(Consumer<ClientRequestContext> contextCustomizer)
ClientRequestContext customization function in a thread-local variable so that
the customized context is used when the client invokes a request from the current thread. Use the
try-with-resources block with the returned SafeCloseable to unset the thread-local
variable automatically:
try (SafeCloseable ignored = withContextCustomizer(ctx -> {
ctx.attr(USER_ID).set(userId);
ctx.attr(USER_SECRET).set(secret);
})) {
client.executeSomething(..);
}
You can also nest the request context customization:
try (SafeCloseable ignored = withContextCustomizer(ctx -> ctx.attr(USER_ID).set(userId))) {
String secret = client.getSecret();
try (SafeCloseable ignored2 = withContextCustomizer(ctx -> ctx.attr(USER_SECRET)
.set(secret))) {
// Both USER_ID and USER_SECRET will be set.
client.executeSomething(..);
}
}
Note that certain properties of ClientRequestContext, such as:
may be null while the customizer function runs, because the target host of the Request
is not determined yet.withHttpHeaders(Function)Copyright © 2020 LeanCloud. All rights reserved.