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 Request okHttpRequest;
    private Request.Builder requestBuilder;

    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) {
        OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(false) // Explicitly disable retries; retry logic will be managed by native code in libHttpClient
                .build();
        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);
}

@@ -394,16 +394,60 @@
 quest %7B%0A
+    private static OkHttpClient OK_CLIENT;%0A%0A
     priv
@@ -518,16 +518,249 @@
 ilder;%0A%0A
+    static %7B%0A        OK_CLIENT = new OkHttpClient.Builder()%0A                .retryOnConnectionFailure(false) // Explicitly disable retries; retry logic will be managed by native code in libHttpClient%0A                .build();%0A    %7D%0A%0A
     publ
@@ -1989,236 +1989,16 @@
    O
-kHttpClient client = new OkHttpClient.Builder()%0A                .retryOnConnectionFailure(false) // Explicitly disable retries; retry logic will be managed by native code in libHttpClient%0A                .build();%0A        client
+K_CLIENT
 .new

Make the OkHttpClient object a static variable so it is not re-created for each request. (#274)