package com.github.vbauer.yta.service.basic;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;

import javax.net.ssl.SSLContext;
import java.util.Map;

/**
 * @author Vladislav Bauer
 */

@ThreadSafe
public class RestClientImpl implements RestClient {

    private static final String SERVICE_URL = "https://translate.yandex.net/api/v1.5/tr.json";
    private static final String ATTR_KEY = "key";
    private static final int DEFAULT_TIMEOUT = 30000;
    private static volatile boolean initialized = false;

    private final String key;


    public RestClientImpl(final String key) {
        this.key = key;
    }


    @Override
    public String callMethod(final String method, final Map<String, Object> parameters) {
        try {
            initHttpClientIfNecessary();

            final String url = formatMethodUrl(method);
            final Map<String, Object> params = composeRequestParameters(parameters);
            final HttpResponse<String> response = Unirest.post(url).fields(params).asString();

            ApiStatus.check(response.getStatus());

            return response.getBody();
        } catch (final Throwable ex) {
            throw Throwables.propagate(ex);
        }
    }


    private String formatMethodUrl(final String method) {
        return SERVICE_URL + method;
    }

    private Map<String, Object> composeRequestParameters(final Map<String, Object> parameters) {
        final Map<String, Object> params = Maps.newHashMap();
        if (parameters != null) {
            params.putAll(parameters);
        }
        params.put(ATTR_KEY, key);
        return params;
    }


    private static void initHttpClientIfNecessary() throws Exception {
        if (!initialized) {
            synchronized (Unirest.class) {
                if (!initialized) {
                    final HttpClient httpClient = createClient();
                    Unirest.setHttpClient(httpClient);
                    initialized = true;
                }
            }
        }
    }

    private static HttpClient createClient() throws Exception {
        final SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(null, new TrustSelfSignedStrategy())
            .build();
        final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);

        final RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(DEFAULT_TIMEOUT)
            .setConnectTimeout(DEFAULT_TIMEOUT)
            .setRedirectsEnabled(true)
            .build();

        return HttpClients.custom()
            .setSSLSocketFactory(socketFactory)
            .setDefaultRequestConfig(requestConfig)
            .build();
    }

}

@@ -562,41 +562,8 @@
 s;%0A%0A
-import javax.net.ssl.SSLContext;%0A
 impo
@@ -580,16 +580,16 @@
 il.Map;%0A
+
 %0A/**%0A * 
@@ -1399,16 +1399,32 @@
 sponse =
+%0A               
  Unirest
@@ -1437,23 +1437,65 @@
 url)
-.fields(params)
+%0A                    .fields(params)%0A                    
 .asS
@@ -2582,25 +2582,88 @@
 LCon
-text sslContext =
+nectionSocketFactory socketFactory = new SSLConnectionSocketFactory(%0A           
  SSL
@@ -2672,32 +2672,36 @@
 ntexts.custom()%0A
+    
             .loa
@@ -2760,32 +2760,36 @@
             
+    
 .build()
 ;%0A        fi
@@ -2772,25 +2772,24 @@
     .build()
-;
 %0A        fin
@@ -2789,98 +2789,8 @@
     
-final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext
 );%0A%0A
@@ -2943,32 +2943,90 @@
 EFAULT_TIMEOUT)%0A
+            .setConnectionRequestTimeout(DEFAULT_TIMEOUT)%0A
             .set
@@ -3067,24 +3067,24 @@
  .build();%0A%0A
-
         retu
@@ -3099,32 +3099,121 @@
 lients.custom()%0A
+            .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)%0A
             .set

Add extra configs for HTTP client