package com.microsoft.azure.arm.resources.implementation;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.AzureResponseBuilder;
import com.microsoft.azure.arm.resources.AzureConfigurable;
import com.microsoft.azure.arm.utils.ResourceManagerThrottlingInterceptor;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.serializer.AzureJacksonAdapter;
import com.microsoft.rest.LogLevel;
import com.microsoft.rest.RestClient;
import okhttp3.Authenticator;
import okhttp3.Interceptor;
import java.net.Proxy;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class AzureConfigurableCoreImpl<T extends AzureConfigurable<T>>
implements AzureConfigurable<T> {
protected RestClient.Builder restClientBuilder;
protected AzureConfigurableCoreImpl() {
this.restClientBuilder = new RestClient.Builder()
.withSerializerAdapter(new AzureJacksonAdapter())
.withResponseBuilderFactory(new AzureResponseBuilder.Factory());
}
@SuppressWarnings("unchecked")
@Override
public T withLogLevel(LogLevel level) {
this.restClientBuilder = this.restClientBuilder.withLogLevel(level);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withInterceptor(Interceptor interceptor) {
this.restClientBuilder = this.restClientBuilder.withInterceptor(interceptor);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withUserAgent(String userAgent) {
this.restClientBuilder = this.restClientBuilder.withUserAgent(userAgent);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withReadTimeout(long timeout, TimeUnit unit) {
this.restClientBuilder = restClientBuilder.withReadTimeout(timeout, unit);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withConnectionTimeout(long timeout, TimeUnit unit) {
this.restClientBuilder = restClientBuilder.withConnectionTimeout(timeout, unit);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withMaxIdleConnections(int maxIdleConnections) {
this.restClientBuilder = restClientBuilder.withMaxIdleConnections(maxIdleConnections);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withCallbackExecutor(Executor executor) {
this.restClientBuilder = restClientBuilder.withCallbackExecutor(executor);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withProxy(Proxy proxy) {
this.restClientBuilder = restClientBuilder.withProxy(proxy);
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public T withProxyAuthenticator(Authenticator proxyAuthenticator) {
this.restClientBuilder = restClientBuilder.withProxyAuthenticator(proxyAuthenticator);
return (T) this;
}
protected RestClient buildRestClient(AzureTokenCredentials credentials, AzureEnvironment.Endpoint endpoint) {
RestClient client = restClientBuilder
.withBaseUrl(credentials.environment(), endpoint)
.withCredentials(credentials)
.withInterceptor(new ResourceManagerThrottlingInterceptor())
.build();
if (client.httpClient().proxy() != null) {
credentials.withProxy(client.httpClient().proxy());
}
return client;
}
protected RestClient buildRestClient(AzureTokenCredentials credentials) {
return buildRestClient(credentials, AzureEnvironment.Endpoint.RESOURCE_MANAGER);
}
}