package com.github.scribejava.httpclient.okhttp;

import com.github.scribejava.core.model.AbstractRequest;
import com.github.scribejava.core.model.HttpClient;
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
import com.github.scribejava.core.model.OAuthConstants;
import com.github.scribejava.core.model.OAuthRequestAsync;
import com.github.scribejava.core.model.Verb;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Future;

import static com.github.scribejava.core.model.AbstractRequest.DEFAULT_CONTENT_TYPE;

public class OkHttpHttpClient implements HttpClient {

    private final OkHttpClient client;

    public OkHttpHttpClient(OkHttpHttpClientConfig config) {
        client = config.getClient();
    }

    public OkHttpHttpClient(OkHttpClient client) {
        this.client = client;
    }

    @Override
    public void close() throws IOException {
        //client.close();
    }

    @Override
    public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
                                      String bodyContents, OAuthAsyncRequestCallback<T> callback,
                                      OAuthRequestAsync.ResponseConverter<T> converter) {
        final Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(completeUrl);

        switch (httpVerb) {
            case GET:
                requestBuilder.get();
                break;
            case POST:
                final String contentType = headers.containsKey(AbstractRequest.CONTENT_TYPE) ?
                                             headers.get(AbstractRequest.CONTENT_TYPE) : DEFAULT_CONTENT_TYPE;

                requestBuilder.post(RequestBody.create(MediaType.parse(contentType), bodyContents));
                break;
            default:
                throw new IllegalArgumentException("message build error: unknown verb type");
        }

        for (Map.Entry<String, String> header : headers.entrySet()) {
            requestBuilder.addHeader(header.getKey(), header.getValue());
        }
        if (userAgent != null) {
            requestBuilder.header(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
        }

        final Call call = client.newCall(requestBuilder.build());
        return new OAuthAsyncCompletionHandler<>(callback, converter, call);
    }
}

@@ -507,16 +507,57 @@
 estBody;
+%0Aimport okhttp3.internal.http.HttpMethod;
 %0A%0Aimport
@@ -1549,138 +1549,209 @@
     
-switch (httpVerb) %7B%0A            case GET:%0A                requestBuilder.get();%0A                break;%0A            case POST:%0A    
+final String method = httpVerb.name();%0A%0A        // prepare body%0A        RequestBody body = null;%0A        if (bodyContents != null && !bodyContents.isEmpty() && HttpMethod.permitsRequestBody(method)) %7B%0A
     
@@ -1878,20 +1878,16 @@
         
-    
 headers.
@@ -1944,21 +1944,16 @@
 T_TYPE;%0A
-%0A    
         
@@ -1960,28 +1960,15 @@
     
-requestBuilder.post(
+body = 
 Requ
@@ -2025,17 +2025,16 @@
 ontents)
-)
 ;%0A      
@@ -2039,22 +2039,10 @@
     
-      
-  break;
+%7D%0A
 %0A   
@@ -2050,123 +2050,91 @@
     
-    default:%0A                throw new IllegalArgumentException(%22message b
+// fill HTTP method and body%0A        requestB
 uild
- 
 er
-ror: unknown verb type%22
+.method(method, body
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -2121,34 +2121,47 @@
 body);%0A%0A        
-%7D%0A
+// fill headers
 %0A        for (Ma
@@ -2420,32 +2420,61 @@
 nt);%0A        %7D%0A%0A
+        // create a new call%0A
         final Ca

Support for more HTTP methods