package io.digdag.plugin.slack;

import io.digdag.client.config.Config;
import io.digdag.spi.Operator;
import io.digdag.spi.OperatorContext;
import io.digdag.spi.OperatorFactory;
import io.digdag.spi.TaskResult;
import io.digdag.spi.TemplateEngine;
import io.digdag.util.BaseOperator;
import okhttp3.*;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class SlackOperatorFactory
        implements OperatorFactory
{
    private final TemplateEngine templateEngine;

    private static final OkHttpClient singletonInstance = new OkHttpClient();

    public SlackOperatorFactory(TemplateEngine templateEngine)
    {
        this.templateEngine = templateEngine;
    }

    public String getType()
    {
        return "slack";
    }

    @Override
    public Operator newOperator(OperatorContext context)
    {
        return new SlackOperator(context);
    }

    private class SlackOperator
            extends BaseOperator
    {
        public SlackOperator(OperatorContext context)
        {
            super(context);
        }

        @Override
        public TaskResult runTask()
        {
            Config params = request.getConfig().mergeDefault(
                    request.getConfig().getNestedOrGetEmpty("slack"));

            String message = workspace.templateCommand(templateEngine, params, "message", UTF_8);
            String url = params.get("webhook_url", String.class);
            String payload = SlackPayload.convertToJson(message);

            this.postToSlack(url, payload);

            return TaskResult.empty(request);
        }

        private void postToSlack(String url, String payload)
        {
            RequestBody body = new FormBody.Builder()
                .add("payload", payload)
                .build();

            Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

            Call call = singletonInstance.newCall(request);

            try (Response response = call.execute()) {
                if(!response.isSuccessful()) {
                    throw new IOException("posting to slack failed");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                singletonInstance.connectionPool().evictAll();
            }
        }
    }
}

@@ -1742,16 +1742,20 @@
         
+    
 .add(%22pa
@@ -1763,32 +1763,36 @@
 load%22, payload)%0A
+    
                 
@@ -1870,16 +1870,20 @@
         
+    
 .url(url
@@ -1884,16 +1884,20 @@
 rl(url)%0A
+    
         
@@ -1916,16 +1916,20 @@
 t(body)%0A
+    
         
@@ -2081,16 +2081,17 @@
       if
+ 
 (!respon
@@ -2212,16 +2212,28 @@
        %7D
+%0A           
  catch (
@@ -2299,16 +2299,28 @@
        %7D
+%0A           
  finally

Reformat code