com.clinia.config.ClientOptions Maven / Gradle / Ivy
package com.clinia.config;
import com.clinia.internal.HttpRequester;
import com.clinia.utils.ExecutorUtils;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
public final class ClientOptions implements ClientConfig {
public static Builder builder() {
return new Builder();
}
private final List cliniaAgentSegments;
private final List hosts;
private final LogLevel logLevel;
private final Duration connectTimeout;
private final Duration writeTimeout;
private final Duration readTimeout;
private final Map defaultHeaders;
private final CompressionType compressionType;
private final Requester customRequester;
private final Logger logger;
private final Consumer requesterConfig;
private final Consumer mapperConfig;
private final ExecutorService executor;
public ClientOptions() {
this(new Builder());
}
ClientOptions(Builder builder) {
this.cliniaAgentSegments = builder.cliniaAgentSegments;
this.hosts = builder.hosts;
this.customRequester = builder.customRequester;
this.logLevel = builder.logLevel;
this.connectTimeout = builder.connectTimeout;
this.writeTimeout = builder.writeTimeout;
this.readTimeout = builder.readTimeout;
this.defaultHeaders = builder.defaultHeaders;
this.compressionType = builder.compressionType;
this.logger = builder.logger;
this.requesterConfig = builder.requesterConfig;
this.mapperConfig = builder.mapperConfig;
this.executor = builder.executor != null ? builder.executor : ExecutorUtils.newThreadPool();
}
@Nonnull
public List getCliniaAgentSegments() {
return cliniaAgentSegments;
}
public List getHosts() {
return hosts;
}
@Nonnull
public LogLevel getLogLevel() {
return logLevel;
}
@Nonnull
public Duration getConnectTimeout() {
return connectTimeout;
}
@Nonnull
public Duration getWriteTimeout() {
return writeTimeout;
}
@Nonnull
public Duration getReadTimeout() {
return readTimeout;
}
@Nonnull
public Map getDefaultHeaders() {
return defaultHeaders;
}
@Nonnull
public CompressionType getCompressionType() {
return compressionType;
}
public Requester getCustomRequester() {
return customRequester;
}
@Nonnull
public Logger getLogger() {
return logger;
}
public Consumer getRequesterConfig() {
return requesterConfig;
}
public Consumer getMapperConfig() {
return mapperConfig;
}
public ExecutorService getExecutor() {
return executor;
}
public static class Builder {
public ExecutorService executor;
private Requester customRequester;
private List hosts;
private Logger logger;
private Consumer requesterConfig;
private Consumer mapperConfig;
private final List cliniaAgentSegments = new ArrayList<>();
private final Map defaultHeaders = new HashMap<>();
private LogLevel logLevel = LogLevel.NONE;
private Duration connectTimeout = Duration.ofSeconds(2);
private Duration writeTimeout = Duration.ofSeconds(30);
private Duration readTimeout = Duration.ofSeconds(5);
private CompressionType compressionType = CompressionType.NONE;
public Builder setRequester(Requester requester) {
this.customRequester = requester;
return this;
}
public Builder addCliniaAgentSegment(CliniaAgent.Segment segment) {
this.cliniaAgentSegments.add(segment);
return this;
}
public Builder addCliniaAgentSegment(String value, String version) {
CliniaAgent.Segment segment = new CliniaAgent.Segment(value, version);
return addCliniaAgentSegment(segment);
}
public Builder addCliniaAgentSegment(String value) {
CliniaAgent.Segment segment = new CliniaAgent.Segment(value);
return addCliniaAgentSegment(segment);
}
public Builder addCliniaAgentSegments(List segments) {
this.cliniaAgentSegments.addAll(segments);
return this;
}
public Builder addDefaultHeader(String header, String value) {
this.defaultHeaders.put(header, value);
return this;
}
public Builder setHosts(List hosts) {
this.hosts = hosts;
return this;
}
public Builder setLogLevel(LogLevel logLevel) {
this.logLevel = logLevel;
return this;
}
public Builder setConnectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public Builder setWriteTimeout(Duration writeTimeout) {
this.writeTimeout = writeTimeout;
return this;
}
public Builder setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public Builder setCompressionType(CompressionType compressionType) {
this.compressionType = compressionType;
return this;
}
public Builder setCustomRequester(Requester customRequester) {
this.customRequester = customRequester;
return this;
}
public Builder setLogger(Logger logger) {
this.logger = logger;
return this;
}
public Builder setRequesterConfig(Consumer requesterConfig) {
this.requesterConfig = requesterConfig;
return this;
}
public Builder setMapperConfig(Consumer mapperConfig) {
this.mapperConfig = mapperConfig;
return this;
}
public Builder setExecutor(ExecutorService executor) {
this.executor = executor;
return this;
}
public ClientOptions build() {
return new ClientOptions(this);
}
}
}