package io.digdag.standards.operator.td;

import com.treasuredata.client.TDClient;
import io.digdag.client.config.Config;
import io.digdag.client.config.ConfigException;

class TDClientFactory
{
    static TDClient clientFromConfig(Config params)
    {

        String apikey = params.get("apikey", String.class).trim();
        if (apikey.isEmpty()) {
            throw new ConfigException("Parameter 'apikey' is empty");
        }

        TDClient client = TDClient.newBuilder(false)
                .setEndpoint(params.get("endpoint", String.class, "api.treasuredata.com"))
                .setUseSSL(params.get("use_ssl", boolean.class, true))
                .setApiKey(apikey)
                .setRetryLimit(0)  // disable td-client's retry mechanism
                .build();

        return client;
    }
}

@@ -50,36 +50,168 @@
 com.
-treasuredata.client.TDClient
+google.common.base.Optional;%0Aimport com.treasuredata.client.ProxyConfig;%0Aimport com.treasuredata.client.TDClient;%0Aimport com.treasuredata.client.TDClientBuilder
 ;%0Aim
@@ -575,23 +575,31 @@
 TDClient
- client
+Builder builder
  = TDCli
@@ -619,16 +619,294 @@
 r(false)
+;%0A%0A        Config proxyConfig = params.getNestedOrGetEmpty(%22proxy%22);%0A        boolean proxyEnabled = proxyConfig.get(%22enabled%22, Boolean.class, false);%0A        if (proxyEnabled) %7B%0A            builder.setProxy(proxyConfig(proxyConfig));%0A        %7D%0A%0A        TDClient client = builder
 %0A       
@@ -1224,11 +1224,868 @@
 t;%0A    %7D
+%0A%0A    private static ProxyConfig proxyConfig(Config config)%0A    %7B%0A        ProxyConfig.ProxyConfigBuilder builder = new ProxyConfig.ProxyConfigBuilder();%0A%0A        Optional%3CString%3E host = config.getOptional(%22host%22, String.class);%0A        if (host.isPresent()) %7B%0A            builder.setHost(host.get());%0A        %7D%0A%0A        Optional%3CInteger%3E port = config.getOptional(%22port%22, Integer.class);%0A        if (port.isPresent()) %7B%0A            builder.setPort(port.get());%0A        %7D%0A%0A        Optional%3CString%3E user = config.getOptional(%22user%22, String.class);%0A        if (user.isPresent()) %7B%0A            builder.setUser(user.get());%0A        %7D%0A%0A        Optional%3CString%3E password = config.getOptional(%22password%22, String.class);%0A        if (password.isPresent()) %7B%0A            builder.setPassword(password.get());%0A        %7D%0A%0A        return builder.createProxyConfig();%0A    %7D
 %0A%7D%0A

add proxy support to td operators