Generated Files

Usage

To access the rest service a interface is generated with methods for each HTTP method you have modeled.

The creation and use of the client is as easy as:

RestClientConfiguration restClientConfiguration = new RestClientConfiguration(
        url,
        timeoutConnect,
        timeoutRead,
        timeoutWrite);
RestClientI restClient = new RestClient(restClientConfiguration);

/* blocking get */
SomeBean bean = resclient.getSomething(param);

/* get with callback */
restClient.getSomethingWithCallback(param, new RestClientI.GetSomethingCallback() {
    @Override
    public void onSuccess(SomeBean result) {    
        System.out.println("Result: " + result.toString());
    }

    @Override
    public void onFailure(Throwable throwable) {
        System.out.println("ERROR:");
        throwable.printStackTrace();
    }
});

Configuration

The Retrofit client is build on top of okhttp3. Therefore it is possible to modify the builder of okhttp3 and add a interceptor or change the complete network configuration.

In the RestClientConfiguration there is a method called customizeOkHttpClientBuilder where it is possible to change the okhhtp3 builder in the development areas. For this example the RestClientConfiguration is extended.

To add basic authentication and to add a network interceptor:

RestClientConfiguration restClientConfiguration = new RestClientConfiguration(
        url,
        timeoutConnect,
        timeoutRead,
        timeoutWrite) {

    @Override
    public void customizeOkHttpClientBuilder(Builder builder) {
        /* add basic authentication */
        builder.authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, okhttp3.Response response)
                    throws IOException {
                String credential = Credentials.basic("name", "password");
                return response
                        .request()
                        .newBuilder()
                        .header("Authorization", credential)
                        .build();
            }
        });
        /* add a network interceptor (e.g. for OAuth) */
        builder.addNetworkInterceptor(new SomeNetworkInterceptor());
    }
};
RestClientI restClient = new RestClient(restClientConfiguration);

There already exist three interceptor wich will be registered by default.

  • RestClientApplicationInterceptor
  • RestClientNetworkInterceptor
  • RestClientLoggingInterceptor

Each of them have a RestClient<*>InterceptorSettings class where they can be configured.

Logging

The generated loggin interceptor is capable of logging the headers of the network. To set the log level modify the constructor of the RestClientLoggingInterceptorSettings and set the level to your needs.

Okhttp3 also has a loggin interceptor which does almost the same.

To remove the generated logging interceptor and add the one of okhttp3 it is necessary to customize the builder like we did before.

RestClientConfiguration restClientConfiguration = new RestClientConfiguration(
        url,
        timeoutConnect,
        timeoutRead,
        timeoutWrite) {

    @Override
    public void customizeOkHttpClientBuilder(Builder builder) {
        /* remove the generated logging interceptor */
        builder.networkInterceptors()
                .removeIf(RestClientLoggingInterceptor.class::isInstance);
        /* create the okhttp interceptor in add it */
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(Level.BASIC);
        builder.addInterceptor(logging);
    }
};
RestClientI restClient = new RestClient(restClientConfiguration);