def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.when(Status.SUCCESS){
// executed when a successful response is received
}
}
public static interface HttpConfig.Response
Defines the configurable HTTP response properties.
Modifier and Type | Method and Description |
---|---|
default void |
exception(Closure<?> closure)
Configures the execution of the provided closure to handle exceptions during request/response processing.
|
void |
exception(java.util.function.Function<java.lang.Throwable,?> function)
Configures the execution of the provided
function to handle exceptions during request/response processing. |
void |
failure(java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" a failure response is received (code >= 400).
|
default void |
failure(Closure<?> closure)
Configures the execution of the provided closure "when" a failure response is received (code >= 400).
|
void |
parser(java.lang.Iterable<java.lang.String> contentTypes,
java.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> val)
Used to specify a response parser (
FromServer instance) for the specified content types, wrapped in a BiFunction . |
java.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> |
parser(java.lang.String contentType)
Used to retrieve the parser configured for the specified content type.
|
void |
parser(java.lang.String contentType,
java.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> val)
Used to specify a response parser (
FromServer instance) for the specified content type, wrapped in a BiFunction . |
void |
success(java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function when a success response is received (code < 400).
|
default void |
success(Closure<?> closure)
Configures the execution of the provided closure "when" a successful response is received (code < 400).
|
void |
when(HttpConfig.Status status,
java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status occurs in the response.
|
default void |
when(HttpConfig.Status status,
Closure<?> closure)
Configures the execution of the provided closure "when" the given status occurs in the response.
|
java.util.function.BiFunction<FromServer,java.lang.Object,?> |
when(java.lang.Integer code)
Used to retrieve the "when" function associated with the given status code.
|
void |
when(java.lang.Integer code,
java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status code occurs in the response.
|
default void |
when(java.lang.Integer code,
Closure<?> closure)
Configures the execution of the provided closure "when" the given status code occurs in the response.
|
void |
when(java.lang.String code,
java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status code (as a
String ) occurs in the response. |
default void |
when(java.lang.String code,
Closure<?> closure)
Configures the execution of the provided closure "when" the given status code (as a String) occurs in the response.
|
default void when(HttpConfig.Status status, Closure<?> closure)
Configures the execution of the provided closure "when" the given status occurs in the response. The closure
will be called with an instance
of the response as a FromServer
instance and the response body as an Object
(if there is one). The value returned from the closure will be
used as the result value of the request; this allows the closure to modify the captured response.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.when(Status.SUCCESS){
// executed when a successful response is received
}
}
This method is the same as calling either the success(Closure)
or failure(Closure)
methods. Only one closure may be mapped to each
status.
status
- the response HttpConfig.Status
enumclosure
- the closure to be executedvoid when(HttpConfig.Status status, java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status occurs in the response. The function
will be called with an instance
of the response as a FromServer
instance and the response body as an Object
(if there is one). The value returned from the closure will be
used as the result value of the request; this allows the closure to modify the captured response.
This method is generally used for Java-based configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().when(Status.SUCCESS, new BiFunction<FromServer, Object, Object>() {
// executed when a successful response is received
});
});
This method is the same as calling either the success(BiFunction)
or failure(BiFunction)
methods. Only one function may be mapped to each
status.
status
- the response HttpConfig.Status
enumfunction
- the function to be executeddefault void when(java.lang.Integer code, Closure<?> closure)
Configures the execution of the provided closure "when" the given status code occurs in the response. The closure
will be called with an instance
of the response as a FromServer
instance. The value returned from the closure will be used as the result value of the request; this allows
the closure to modify the captured response.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.when(404){
// executed when a 'not found' response is received
}
}
code
- the response code to be caughtclosure
- the closure to be executedvoid when(java.lang.Integer code, java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status code occurs in the response. The function
will be called with an instance
of the response as a FromServer
instance and the response body as an Object
(if there is one). The value returned from the closure will be
used as the result value of the request; this allows the closure to modify the captured response.
This method is generally used for Java-based configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().when(404, new BiFunction<FromServer, Object, Object>() {
// executed when a successful response is received
});
});
This method is the same as calling either the success(BiFunction)
or failure(BiFunction)
methods. Only one function may be mapped to each
status.
code
- the response codefunction
- the function to be executeddefault void when(java.lang.String code, Closure<?> closure)
Configures the execution of the provided closure "when" the given status code (as a String) occurs in the response. The closure
will be
called with an instance of the response as a FromServer
instance. The value returned from the closure will be used as the result value
of the request; this allows the closure to modify the captured response.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.when('404'){
// executed when a 'not found' response is received
}
}
code
- the response code to be caughtclosure
- the closure to be executedvoid when(java.lang.String code, java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" the given status code (as a String
) occurs in the response. The function
will be
called with an instance of the response as a FromServer
instance and the response body as an Object
(if there is one). The value returned
from the function will be used as the result value of the request; this allows the function to modify the captured response.
This method is generally used for Java-based configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().when("404", new BiFunction<FromServer, Object, Object>() {
// executed when a successful response is received
});
});
code
- the response code as a String
function
- the function to be executedjava.util.function.BiFunction<FromServer,java.lang.Object,?> when(java.lang.Integer code)
Used to retrieve the "when" function associated with the given status code.
code
- the status codedefault void success(Closure<?> closure)
Configures the execution of the provided closure "when" a successful response is received (code < 400). The closure
will be called with
an instance of the response as a FromServer
instance. The value returned from the closure will be used as the result value of the request;
this allows the closure to modify the captured response.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.success(){
// executed when a successful response is received
}
}
This method is the same as calling either the when(Status.SUCCESS, Closure)
method.
closure
- the closure to be executedvoid success(java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function when a success response is received (code < 400). The function
will be called with
an instance of the response as a FromServer
instance and the body content as an Object
(if present). The value returned from the function
will be used as the result value of the request; this allows the function to modify the captured response.
This method is generally used for Java-specific configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().success(new BiFunction<FromServer, Object, Object>() {
// executed when a success response is received
});
});
This method is the same as calling either the when(Status.SUCCESS, BiFunction)
method.
function
- the closure to be executeddefault void failure(Closure<?> closure)
Configures the execution of the provided closure "when" a failure response is received (code >= 400). The closure
will be called with
an instance of the response as a FromServer
instance. The value returned from the closure will be used as the result value of the request;
this allows the closure to modify the captured response.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.failure {
// executed when a failure response is received
}
}
This method is the same as calling either the when(Status.FAILURE, Closure)
method.
closure
- the closure to be executedvoid failure(java.util.function.BiFunction<FromServer,java.lang.Object,?> function)
Configures the execution of the provided function "when" a failure response is received (code >= 400). The function
will be called with
an instance of the response as a FromServer
instance and the body content as an Object
(if present). The value returned from the function
will be used as the result value of the request; this allows the function to modify the captured response.
This method is generally used for Java-specific configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().failure(new BiFunction<FromServer, Object, Object>() {
// executed when a failure response is received
});
});
This method is the same as calling either the when(Status.FAILURE, BiFunction)
method.
function
- the closure to be executeddefault void exception(Closure<?> closure)
Configures the execution of the provided closure to handle exceptions during request/response processing. This is
different from a failure condition because there is no response, no status code, no headers, etc. The closure
will be called with
the best guess as to what was the original exception. Some attempts will be made to unwrap exceptions that are of type
TransportingException
or UndeclaredThrowableException
. The closure
should have a single Throwable
argument.
The value returned from the closure will be used as the result value of the request. Since there is no response body for the closure to process, this usually means that the closure should do one of three things: re-throw the exception or throw a wrapped version of the exception, return null, or return a predefined empty value.
def http = HttpBuilder.configure {
request.uri = 'http://localhost:10101'
}
http.get {
request.uri.path = '/foo'
response.exception { Throwable t ->
t.printStackTrace();
throw new RuntimeException(t);
}
}
The default exception method wraps the exception in a RuntimeException
(if it is
not already of that type) and rethrows.
closure
- the closure to be executedvoid exception(java.util.function.Function<java.lang.Throwable,?> function)
Configures the execution of the provided function
to handle exceptions during request/response processing. This is
different from a failure condition because there is no response, no status code, no headers, etc. The function
will be called with
the best guess as to what was the original exception. Some attempts will be made to unwrap exceptions that are of type
TransportingException
or UndeclaredThrowableException
.
The value returned from the function will be used as the result value of the request. Since there is no response body for the function to process, this usually means that the function should do one of three things: re-throw the exception or throw a wrapped version of the exception, return null, or return a predefined empty value.
This method is generally used for Java-specific configuration.
HttpBuilder http = HttpBuilder.configure(config -> {
config.getRequest().setUri("http://localhost:10101");
});
http.get( config -> {
config.getRequest().getUri().setPath("/foo");
config.getResponse().exception((t) -> {
t.printStackTrace();
throw new RuntimeException(t);
});
});
The built in exception method wraps the exception in a RuntimeException
(if it is
not already of that type) and rethrows.
function
- the function to be executedvoid parser(java.lang.String contentType, java.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> val)
Used to specify a response parser (FromServer
instance) for the specified content type, wrapped in a BiFunction
.
contentType
- the content type where the parser will be appliedval
- the parser wrapped in a function objectvoid parser(java.lang.Iterable<java.lang.String> contentTypes, java.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> val)
Used to specify a response parser (FromServer
instance) for the specified content types, wrapped in a BiFunction
.
contentTypes
- the contents type where the parser will be appliedval
- the parser wrapped in a function objectjava.util.function.BiFunction<ChainedHttpConfig,FromServer,java.lang.Object> parser(java.lang.String contentType)
Used to retrieve the parser configured for the specified content type.
contentType
- the content typeFromServer
instance wrapped in a function object