package com.xbox.httpclient;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.RequestBody;

public class HttpClientRequest {
    private static OkHttpClient OK_CLIENT;

    private Request okHttpRequest;
    private Request.Builder requestBuilder;

    static {
        OK_CLIENT = new OkHttpClient.Builder()
                .retryOnConnectionFailure(false) // Explicitly disable retries; retry logic will be managed by native code in libHttpClient
                .build();
    }

    public HttpClientRequest() {
        requestBuilder = new Request.Builder();
    }

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public static HttpClientRequest createClientRequest() {
        return new HttpClientRequest();
    }

    public void setHttpUrl(String url) {
        this.requestBuilder = this.requestBuilder.url(url);
    }

    public void setHttpMethodAndBody(String method, String contentType, byte[] body) {
        if (body == null || body.length == 0) {
            this.requestBuilder = this.requestBuilder.method(method, null);
        } else {
            this.requestBuilder = this.requestBuilder.method(method, RequestBody.create(MediaType.parse(contentType), body));
        }
    }

    public void setHttpHeader(String name, String value) {
        this.requestBuilder = requestBuilder.addHeader(name, value);
    }

    public void doRequestAsync(final long sourceCall) {
        OK_CLIENT.newCall(this.requestBuilder.build()).enqueue(new Callback() {
            @Override
            public void onFailure(final Call call, IOException e) {
                Log.e("HttpRequestClient", "Failed to execute async request", e);
                OnRequestCompleted(sourceCall, null);
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                OnRequestCompleted(sourceCall, new HttpClientResponse(response));
            }
        });
    }

    private native void OnRequestCompleted(long call, HttpClientResponse response);
}

@@ -130,33 +130,8 @@
 nfo;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -411,16 +411,71 @@
 _CLIENT;
+%0A    private static final byte%5B%5D NO_BODY = new byte%5B0%5D;
 %0A%0A    pr
@@ -1575,24 +1575,223 @@
 gth == 0) %7B%0A
+            if (%22POST%22.equals(method) %7C%7C %22PUT%22.equals(method)) %7B%0A                this.requestBuilder = this.requestBuilder.method(method, RequestBody.create(null, NO_BODY));%0A            %7D else %7B%0A    
             
@@ -1846,32 +1846,46 @@
 (method, null);%0A
+            %7D%0A
         %7D else %7B
@@ -2313,32 +2313,32 @@
       @Override%0A
+
             publ
@@ -2393,90 +2393,8 @@
 ) %7B%0A
-                Log.e(%22HttpRequestClient%22, %22Failed to execute async request%22, e);%0A
     

Handle empty HTTP POST/PUTs on Android (#349)