All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.client.TeamscaleServiceGenerator Maven / Gradle / Ivy

Go to download

A tiny service client that only supports Teamscale's the external upload interface and impacted-tests service.

There is a newer version: 34.2.0
Show newest version
package com.teamscale.client;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

/** Helper class for generating a teamscale compatible service. */
public class TeamscaleServiceGenerator {

	/** Custom user agent of the requests, used to monitor API traffic. */
	public static final String USER_AGENT = "Teamscale JaCoCo Agent";

	/**
	 * Generates a {@link Retrofit} instance for the given service, which uses basic auth to authenticate against the
	 * server and which sets the accept header to json.
	 */
	public static  S createService(Class serviceClass, HttpUrl baseUrl, String username, String accessToken,
									  Duration readTimeout, Duration writeTimeout, Interceptor... interceptors) {
		return createServiceWithRequestLogging(serviceClass, baseUrl, username, accessToken, null, readTimeout,
				writeTimeout,
				interceptors);
	}

	/**
	 * Generates a {@link Retrofit} instance for the given service, which uses basic auth to authenticate against the
	 * server and which sets the accept-header to json. Logs requests and responses to the given logfile.
	 */
	public static  S createServiceWithRequestLogging(Class serviceClass, HttpUrl baseUrl, String username,
														String accessToken, File logfile, Duration readTimeout,
														Duration writeTimeout, Interceptor... interceptors) {
		Retrofit retrofit = HttpUtils.createRetrofit(
				retrofitBuilder -> retrofitBuilder.baseUrl(baseUrl)
						.addConverterFactory(JacksonConverterFactory.create(JsonUtils.OBJECT_MAPPER)),
				okHttpBuilder -> {
					addInterceptors(okHttpBuilder, interceptors)
							.addInterceptor(HttpUtils.getBasicAuthInterceptor(username, accessToken))
							.addInterceptor(new AcceptJsonInterceptor())
							.addNetworkInterceptor(new CustomUserAgentInterceptor());
					if (logfile != null) {
						okHttpBuilder.addInterceptor(new FileLoggingInterceptor(logfile));
					}
				},
				readTimeout, writeTimeout
		);
		return retrofit.create(serviceClass);
	}

	private static OkHttpClient.Builder addInterceptors(OkHttpClient.Builder builder, Interceptor... interceptors) {
		for (Interceptor interceptor : interceptors) {
			builder.addInterceptor(interceptor);
		}
		return builder;
	}


	/**
	 * Sets an Accept: application/json header on all requests.
	 */
	private static class AcceptJsonInterceptor implements Interceptor {

		@NotNull
		@Override
		public Response intercept(Chain chain) throws IOException {
			Request newRequest = chain.request().newBuilder().header("Accept", "application/json").build();
			return chain.proceed(newRequest);
		}
	}

	/**
	 * Sets the custom user agent {@link #USER_AGENT} header on all requests.
	 */
	public static class CustomUserAgentInterceptor implements Interceptor {
		@NotNull
		@Override
		public Response intercept(Chain chain) throws IOException {
			Request newRequest = chain.request().newBuilder().header("User-Agent", USER_AGENT).build();
			return chain.proceed(newRequest);
		}
	}

}