package com.batiaev.vk.common;

import com.batiaev.vk.common.consts.VKApiJsonConst;
import com.batiaev.vk.common.json2class.VkJson2Class;
import com.batiaev.vk.common.entity.VkError;
import com.batiaev.vk.common.entity.VkObjectType;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by anton on 17/04/15.
 * ---
 * Copyright © 2015. Anton Batiaev. All Rights Reserved.
 * www.batyaev.com
 */

public class VKRequest {

    private static final Logger LOG = LogManager.getLogger(VKRequest.class);
    //    https://api.vk.com/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN
    public final static String BASE_URL = "https://api.vk.com/method/";
    public final static String ACCESS_TOKEN_URL = "https://oauth.vk.com/access_token?";
    public final static String AUTH_URL = "https://oauth.vk.com/authorize?";

    public static VKRequest create() {
        return new VKRequest();
    }

    public String getRequest(String url) {

        String responseBody = readUrl(url);
        JSONObject obj = new JSONObject(responseBody);
        if (obj.has(VKApiJsonConst.ERROR)) {
            VkError error = (VkError) VkJson2Class.toClass(obj.getJSONObject(VKApiJsonConst.ERROR), VkObjectType.ERROR);
            LOG.error("Error respond: " + error.toString());
            return null;
        }
        return responseBody;
    }

    @Deprecated
    public String getRequest(String methodName, String params) {
        String url = BASE_URL + methodName + "?" + params;
        return getRequest(url);
    }

    private String readUrl(String url) {
//        https://hc.apache.org/httpcomponents-client-4.4.x/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java /
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String responseBody = "";
        try {
            HttpGet httpget = new HttpGet(url);

            LOG.debug("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = response -> {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            };
            responseBody = httpclient.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            LOG.error("Cannot get information from VK.com or problems in protocol\n");
            e.printStackTrace();
        } catch (IOException e) {
            LOG.error("Cannot get information from VK.com\n");
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseBody;
    }
}

@@ -265,63 +265,8 @@
 ty;%0A
-import org.apache.http.client.ClientProtocolException;%0A
 impo
@@ -1342,22 +1342,16 @@
 onse
-Body = readUrl
+ = doGet
 (url
@@ -1401,20 +1401,16 @@
 response
-Body
 );%0A     
@@ -1681,36 +1681,32 @@
  return response
-Body
 ;%0A    %7D%0A%0A    @De
@@ -1900,15 +1900,13 @@
 ing 
-readUrl
+doGet
 (Str
@@ -1920,153 +1920,8 @@
 ) %7B%0A
-//        https://hc.apache.org/httpcomponents-client-4.4.x/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java /%0A
     
@@ -1982,24 +1982,25 @@
 eDefault();%0A
+%0A
         Stri
@@ -2299,16 +2299,20 @@
 t status
+Code
  = respo
@@ -2344,24 +2344,62 @@
 atusCode();%0A
+                String result = null;%0A
             
@@ -2412,16 +2412,20 @@
  (status
+Code
  %3E= 200 
@@ -2433,16 +2433,20 @@
 & status
+Code
  %3C 300) 
@@ -2529,23 +2529,20 @@
         
-return 
+if (
 entity !
@@ -2551,10 +2551,18 @@
 null
- ?
+) result =
  Ent
@@ -2590,15 +2590,8 @@
 ity)
- : null
 ;%0A  
@@ -2609,15 +2609,8 @@
    %7D
- else %7B
 %0A   
@@ -2626,85 +2626,85 @@
     
-    throw new ClientProtocolException(%22Unexpected response status: %22 + status
+else LOG.error(%22Cannot get information from VK.com or problems in protocol%5Cn%22
 );%0A 
@@ -2710,33 +2710,46 @@
                 
-%7D
+return result;
 %0A            %7D;%0A
@@ -2825,174 +2825,8 @@
 r);%0A
-        %7D catch (ClientProtocolException e) %7B%0A            LOG.error(%22Cannot get information from VK.com or problems in protocol%5Cn%22);%0A            e.printStackTrace();%0A
     

fix et request