package com.rebuy.consul;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;

import java.util.ArrayList;
import java.util.List;

public class ConsulServiceBuilder
{
    private String checkInterval = "10s";

    private String name;

    private int port = 80;

    private String agent = "localhost";

    private String endpoint = "health";

    private List<String> tags = new ArrayList<>();

    public ConsulServiceBuilder checkInterval(String checkInterval)
    {
        this.checkInterval = checkInterval;
        return this;
    }

    public String checkInterval()
    {
        return checkInterval;
    }

    public String id()
    {
        return String.format("%s:%d", name(), port);
    }

    public String name()
    {
        return name;
    }

    public ConsulServiceBuilder name(String name)
    {
        this.name = name;
        return this;
    }

    public int port()
    {
        return port;
    }

    public ConsulServiceBuilder port(int port)
    {
        this.port = port;
        return this;
    }

    public String agent()
    {
        return agent;
    }

    public ConsulServiceBuilder agent(String agent)
    {
        this.agent = agent;
        return this;
    }

    public String endpoint()
    {
        return endpoint;
    }

    public ConsulServiceBuilder endpoint(String endpoint)
    {
        this.endpoint = endpoint;
        return this;
    }

    public ConsulService build()
    {
        return new ConsulService(buildClient(), buildService());
    }

    public List<String> tags()
    {
        return tags;
    }

    public ConsulServiceBuilder tag(String tag)
    {
        tags.add(tag);
        return this;
    }

    NewService.Check buildCheck()
    {
        NewService.Check check = new NewService.Check();
        check.setInterval(checkInterval());
        check.setHttp(String.format(
            "http://localhost:%d/%s",
            port,
            endpoint
        ));
        return check;
    }

    ConsulClient buildClient()
    {
        return new ConsulClient(agent());
    }

    NewService buildService()
    {
        NewService service = new NewService();
        service.setId(id());
        service.setName(name());
        service.setPort(port());
        service.setCheck(buildCheck());
        service.setTags(tags);
        return service;
    }
}

@@ -240,24 +240,61 @@
 l = %2210s%22;%0A%0A
+    private String timeout = %2210s%22;%0A%0A
     private 
@@ -684,32 +684,219 @@
 nterval;%0A    %7D%0A%0A
+    public ConsulServiceBuilder timeout(String timeout)%0A    %7B%0A        this.timeout = timeout;%0A        return this;%0A    %7D%0A%0A    public String timeout()%0A    %7B%0A        return timeout;%0A    %7D%0A%0A
     public Strin
@@ -2042,24 +2042,61 @@
 ce.Check();%0A
+        check.setTimeout(timeout());%0A
         chec

Allow timeout settings