amv-trafficsoft-rest is a Java client library for accessing the AMV TrafficSoft® API. Please note that this software is considered experimental until v1.0.0 is reached.
amv-trafficsoft-rest requires Java version 1.8 or greater.
repositories {
jcenter()
// ... or add bintray repo
maven {
url 'https://dl.bintray.com/amvnetworks/amv-trafficsoft-rest'
}
}
// ...
dependencies {
compile "org.amv.trafficsoft:amv-trafficsoft-rest-client:${version}"
}
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
<!-- ... or add bintray repo -->
<repository>
<id>bintray-amvnetworks-amv-trafficsoft-rest</id>
<name>bintray</name>
<url>https://dl.bintray.com/amvnetworks/amv-trafficsoft-rest</url>
</repository>
</repositories>
<!-- ... -->
<dependency>
<groupId>org.amv.trafficsoft</groupId>
<artifactId>amv-trafficsoft-rest-client</artifactId>
<version>${version}</version>
</dependency>
Construct a new client for the various services to access different parts of the AMV Trafficsoft API.
String baseUrl = "https://www.example.com";
BasicAuth basicAuth = BasicAuthImpl.builder()
.username("john_doe")
.password("mysupersecretpassword")
.build();
XfcdClient xfcdClient = TrafficsoftClients.xfcd(baseUrl, basicAuth);
// ...
String baseUrl = "https://www.example.com";
BasicAuth basicAuth = BasicAuthImpl.builder()
.username("john_doe")
.password("mysupersecretpassword")
.build();
CarSharingReservationClient reservationClient = TrafficsoftClients.carSharingReservation(baseUrl, basicAuth);
CarSharingWhitelistClient whitelistClient = TrafficsoftClients.carSharingWhitelist(baseUrl, basicAuth);
// ...
String baseUrl = "https://www.example.com";
BasicAuth basicAuth = BasicAuthImpl.builder()
.username("john_doe")
.password("mysupersecretpassword")
.build();
AsgRegisterClient asgRegisterClient = TrafficsoftClients.asgRegister(baseUrl, basicAuth);
// ...
Every method returns a HystrixCommand
which is essentially a blocking command but provides non-blocking execution if used with toObservable()
.
See Hystrix wiki for more information.
Used for asynchronous execution of a request with a callback by subscribing to the Observable.
long contractId = 42;
Action1<OemsResponseRestDto> onNext = oemsResponseRestDto -> {
log.info("Received: {}", oemsResponseRestDto);
};
Action1<Throwable> onError = error -> {
log.error("{}", error);
};
Action0 onComplete = () -> {
log.info("Completed.");
};
myAsgRegisterClient.getOems(contractId)
.toObservable()
.subscribe(onNext, onError, onCompleted);
// ...
Used for synchronous execution of requests.
long contractId = 42;
OemsResponseRestDto oemsResponseDto = myAsgRegisterClient
.getOems(contractId)
.execute();
// ...
Take a look at the test/ directory as a good way of getting started and a quick overview of the key client features and concepts.
If you are using Spring Boot you can make use of its auto-configuration feature by including
amv-trafficsoft-rest-client-spring-boot-starter
module in your application.
Add dependency:
dependencies {
compile 'org.amv.trafficsoft:amv-trafficsoft-rest-client-spring-boot-starter:${version}'
}
If the auto-configuration class detects certain properties it will automatically setup and configure
all beans. Adapt all properties to your needs in your application.yml
:
amv.trafficsoft.api:
rest:
baseUrl: 'https://www.example.com'
username: 'john_doe'
password: 'mysupersecretpassword'
contractId: 0
This will make all client beans available for your services:
@Component
public class MyService {
private final XfcdClient xfcdClient;
@Autowired
public MyService(XfcdClient xfcdClient) {
this.xfcdClient = Objects.requireNonNull(xfcdClient);
}
public void doIt() {
// ...
}
}
See amv-trafficsoft-restclient-demo repository for a simple demo application with Spring Boot.
It is possible to apply a custom configuration and configure the clients to your needs.
If you construct your own config you have to provide the target
property
or use TrafficsoftClients.config(clazz, baseUrl, basicAuth)
method. e.g.
String baseUrl = ...
BasicAuth basicAuth = ...
ClientConfig<XfcdClient> customConfig = TrafficsoftClients.config(XfcdClient.class, baseUrl, basicAuth)
.logLevel(Logger.Level.HEADERS)
.retryer(Retryer.NEVER_RETRY)
.requestInterceptor(new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header("X-MyCustomHeader", "MyCustomValue");
}
})
.requestInterceptor(new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.replaceQueryValues(ImmutableMap.<String, String>builder()
.put("myQueryParam", "myQueryValue")
.build());
}
})
// ...
.build();
XfcdClient xfcdClient = TrafficsoftClients.xfcd(customConfig);
// ...
This library uses Hystrix for latency and fault tolerance.
The clients are created with reasonable default values but the options can be adapted to your special
requirements by providing your own SetterFactory
instance.
For more information see the Hystrix Configuration Documentation.
String baseUrl = ...
BasicAuth basicAuth = ...
ClientConfig<XfcdClient> customConfig = TrafficsoftClients.config(XfcdClient.class, baseUrl, basicAuth)
.setterFactory(new SetterFactory() {
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = Feign.configKey(target.type(), method);
HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter()
.withCoreSize(2);
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter()
.withFallbackEnabled(false)
.withExecutionTimeoutEnabled(true)
.withExecutionTimeoutInMilliseconds((int) SECONDS.toMillis(45))
.withExecutionIsolationStrategy(THREAD)
.withExecutionIsolationThreadInterruptOnTimeout(true);
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andThreadPoolPropertiesDefaults(threadPoolProperties)
.andCommandPropertiesDefaults(commandProperties);
}
})
.build();
XfcdClient xfcdClient = TrafficsoftClients.xfcd(customConfig);
// ...
This library uses OkHttpClient as default client to direct http requests to OkHttp, which enables SPDY and better network control. You can easily switch to another client like ApacheHttpClient in order to use Apache HttpComponents or provide your very own implementation:
String baseUrl = ...
BasicAuth basicAuth = ...
ClientConfig<XfcdClient> customConfig = TrafficsoftClients.config(XfcdClient.class, baseUrl, basicAuth)
.client(new ApacheHttpClient())
// ...
.build();
XfcdClient xfcdClient = TrafficsoftClients.xfcd(customConfig);
// ...
./gradlew clean build
When a parameter minimal
is provided, certain tasks will be skipped to make the build faster.
e.g. findbugs
, checkstyle
, javadoc
- tasks which results are not essential for a working build.
./gradlew clean build -Pminimal
./gradlew final -Prelease.scope=patch
./gradlew clean build bintrayUpload
-Prelease.stage=final
-Prelease.scope=patch
-PreleaseToBintray
-PbintrayUser=${username}
-PbintrayApiKey=${apiKey}
The project is licensed under the Apache License. See LICENSE for details.