-
Notifications
You must be signed in to change notification settings - Fork 4
Examples
Below are some example usages of Robusto framework.
This is the simplest example possible of using ApiCommand. It builds a new ApiCommand from scratch that returns a String, supplying only the 2 required arguments: a ConstantUriProvider and a RemoteServiceCallback supplied via lambda. Note that all Hystrix and Spring Retry settings will be defaulted. What happens inside the RemoteServiceCallback is the core of your API call: it could be HTTP, SOAP, etc. It will simply get the url and can do with that whatever it wants.
ApiCommand<String> command = ApiCommand.<String>builder()
.withCommandGroup("GoogleV1Service")
.withUriProvider(new ConstantUriProvider<>("https://api.google.com/v1/someservice"))
.withRemoteServiceCallback(url -> { ... })
.build();
This is an extension of the previous example. It builds a new ApiCommand from scratch that returns a String, supplying the 2 required arguments: a ConstantUriProvider and a RemoteServiceCallback supplied via lambda. Note that it also provides some Hystrix and Spring Retry settings to override default settings.
ApiCommand<String> command = ApiCommand.<String>builder()
.withCommandGroup("GoogleV1Service")
.withUriProvider(new ConstantUriProvider<>("http://hostname/v1/api"))
.withRemoteServiceCallback(url -> { ... })
.withNumberOfRetries(10)
.withBackoffPolicy(new FixedBackOffPolicy())
.withHystrixCommandProperties(
HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(30000)
.withCircuitBreakerRequestVolumeThreshold(5)
.withCircuitBreakerErrorThresholdPercentage(10))
.withHystrixThreadProperties(
HystrixThreadPoolProperties.Setter()
.withCoreSize(20))
.build();
This example shows how to extend the SpringRestClient to help aid in building _ApiCommand_s. It uses the helper method restCommand(), which does some boilerplate setup for you such as command naming and reading configuration from the environment to possibly override Hystrix or Spring Retry settings.
Note that SpringRestClient sets up some _RestTemplate_s to use for HTTP calls. These are accessed using either getRestTemplate(), which returns a RestTemplate with default settings, or using getRestTemplate(String), which returns either the default RestTemplate or one that is customized for the given command name (if applicable, as determined by SpringClientConfiguration.
public class MyClient extends SpringRestClient
{
public String postGoogleV1Service(SomeDto body)
{
ApiCommand<String> command = restCommand(
new ConstantUriProvider<String>("https://api.google.com/v1/someservice"),
new SpringInstanceCallback<String>()
{
@Override
public String runWithUrl(String url)
{
return getRestTemplate(getCommandName()).postForObject(
UriComponentsBuilder
.fromUriString(url)
.pathSegment("new").pathSegment("resource")
.build()
.toUriString(),
body,
String.class
);
}
}
).build();
return command.execute();
}
}
This is the same example as above, but is simplifies the setting of command name and looking up of any customized RestTemplate by using functional interfaces. It uses SpringRestClient.restTemplateCommand(), which greatly simplifies building _ApiCommand_s.
public class MyClient extends SpringRestClient
{
public String postGoogleV1Service(SomeDto body)
{
ApiCommand<String> command = restTemplateCommand(
new ConstantUriProvider<String>("https://api.google.com/v1/someservice"),
(cmdCtx, restTemplate, url) ->
{
return restTemplate.postForObject(
UriComponentsBuilder
.fromUriString(url)
.pathSegment("new").pathSegment("resource")
.build()
.toUriString(),
body,
String.class
}
).build();
return command.execute();
}
}
HomeAdvisor is the leading marketplace for home owners and service professionals to connect.