/amv-trafficsoft-rest

Java client library for accessing the AMV TrafficSoft® API

Primary LanguageJavaApache License 2.0Apache-2.0

Build Status Download License

amv-trafficsoft-rest

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.

install

gradle

repo

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}"
}

maven

repo

<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>

usage

constructing clients

Construct a new client for the various services to access different parts of the AMV Trafficsoft API.

xfcd

String baseUrl = "https://www.example.com";
BasicAuth basicAuth = BasicAuthImpl.builder()
    .username("john_doe")
    .password("mysupersecretpassword")
    .build();
    
XfcdClient xfcdClient = TrafficsoftClients.xfcd(baseUrl, basicAuth);
// ...

car-sharing

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);
// ...

asg-register

String baseUrl = "https://www.example.com";
BasicAuth basicAuth = BasicAuthImpl.builder()
    .username("john_doe")
    .password("mysupersecretpassword")
    .build();
    
AsgRegisterClient asgRegisterClient = TrafficsoftClients.asgRegister(baseUrl, basicAuth);
// ...

execution

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.

non-blocking

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);
// ...

blocking

Used for synchronous execution of requests.

long contractId = 42;

OemsResponseRestDto oemsResponseDto = myAsgRegisterClient
    .getOems(contractId)
    .execute();
// ...

examples

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.

spring boot integration

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() {
        // ...
    }
}

demo application

See amv-trafficsoft-restclient-demo repository for a simple demo application with Spring Boot.

custom configuration

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);
// ...

adapt circuit breaker config

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);
// ...

adapt http client

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);
// ...

build

./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

create a release

./gradlew final -Prelease.scope=patch

release to bintray

./gradlew clean build bintrayUpload
  -Prelease.stage=final
  -Prelease.scope=patch
  -PreleaseToBintray 
  -PbintrayUser=${username} 
  -PbintrayApiKey=${apiKey} 

license

The project is licensed under the Apache License. See LICENSE for details.