// Path: UserVoiceSDK/src/com/uservoice/uservoicesdk/rest/RestTask.java
// public class RestTask extends AsyncTask<String, String, RestResult> {
//     private String urlPath;
//     private RestMethod method;
//     private Map<String, String> params;
//     private RestTaskCallback callback;
//     private Context context;
// 
//     public RestTask(Context context, RestMethod method, String urlPath, Map<String, String> params, RestTaskCallback callback) {
//         this.context = context.getApplicationContext();
//         this.method = method;
//         this.urlPath = urlPath;
//         this.callback = callback;
//         this.params = params;
//     }
// 
//     @Override
//     protected RestResult doInBackground(String... args) {
//         try {
//             Request request = createRequest();
//             if (isCancelled())
//                 throw new InterruptedException();
//             OkHttpClient client = new OkHttpClient();
//             OAuthConsumer consumer = Session.getInstance().getOAuthConsumer(context);
//             if (consumer != null) {
//                 AccessToken accessToken = Session.getInstance().getAccessToken();
//                 if (accessToken != null) {
//                     consumer.setTokenWithSecret(accessToken.getKey(), accessToken.getSecret());
//                 }
//                 request = (Request) consumer.sign(request).unwrap();
//             }
//             Log.d("UV", urlPath);
//             if (isCancelled())
//                 throw new InterruptedException();
//             // TODO it would be nice to find a way to abort the request on cancellation
//             Response response = client.newCall(request).execute();
//             if (isCancelled())
//                 throw new InterruptedException();
//             int statusCode = response.code();
//             String body = response.body().string();
//             if (statusCode >= 400) {
//                 Log.d("UV", body);
//             }
//             if (isCancelled())
//                 throw new InterruptedException();
//             return new RestResult(statusCode, new JSONObject(body));
//         } catch (Exception e) {
//             return new RestResult(e);
//         }
//     }
// 
//     private Request createRequest() throws URISyntaxException, UnsupportedEncodingException {
//         Request.Builder builder = new Request.Builder()
//                 .addHeader("Accept-Language", Locale.getDefault().getLanguage())
//                 .addHeader("API-Client", String.format("uservoice-android-%s", UserVoice.getVersion()))
//                 .addHeader("User-Agent", String.format("uservoice-android-%s", UserVoice.getVersion()));
// 
//         String host = Session.getInstance().getConfig(context).getSite();
//         Uri.Builder uriBuilder = new Uri.Builder();
//         uriBuilder.scheme(host.contains(".us.com") ? "http" : "https");
//         uriBuilder.encodedAuthority(host);
//         uriBuilder.path(urlPath);
// 
//         if (method == RestMethod.GET || method == RestMethod.DELETE) {
//             builder.method(method.toString(), null);
//             addParamsToQueryString(builder, uriBuilder);
//         } else {
//             builder.url(uriBuilder.build().toString());
//             addParamsToBody(builder);
//         }
//         return builder.build();
//     }
// 
//     @Override
//     protected void onPostExecute(RestResult result) {
//         if (result.isError()) {
//             callback.onError(result);
//         } else {
//             try {
//                 callback.onComplete(result.getObject());
//             } catch (JSONException e) {
//                 callback.onError(new RestResult(e, result.getStatusCode(), result.getObject()));
//             }
//         }
//         super.onPostExecute(result);
//     }
// 
//     private void addParamsToQueryString(Request.Builder builder, Uri.Builder uriBuilder) throws URISyntaxException {
//         if (params != null) {
//             for (Map.Entry<String,String> param : params.entrySet()) {
//                 uriBuilder.appendQueryParameter(param.getKey(), param.getValue());
//             }
//         }
//         builder.url(uriBuilder.build().toString());
//     }
// 
//     private void addParamsToBody(Request.Builder builder) throws UnsupportedEncodingException, URISyntaxException {
//         if (params != null) {
//             FormBody.Builder paramsBuilder = new FormBody.Builder();
//             for (Map.Entry<String,String> param : params.entrySet()) {
//                 if (param.getValue() != null) {
//                     paramsBuilder.add(param.getKey(), param.getValue());
//                 }
//             }
//             builder.method(method.toString(), paramsBuilder.build());
//         }
//     }
// 
// }


import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.uservoice.uservoicesdk.rest.Callback;
import com.uservoice.uservoicesdk.rest.RestTask;
import com.uservoice.uservoicesdk.rest.RestTaskCallback;
    public Article() {}

    public static void loadPage(Context context, int page, final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        params.put("filter", "published");
        params.put("per_page", "50");
        params.put("page", String.valueOf(page));
        doGet(context, apiPath("/articles.json"), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

    public static void loadPageForTopic(Context context, int topicId, int page, final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        params.put("filter", "published");
        params.put("per_page", "50");
        params.put("page", String.valueOf(page));
        doGet(context, apiPath("/topics/%d/articles.json", topicId), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

// Path: UserVoiceSDK/src/com/uservoice/uservoicesdk/rest/RestTask.java
// public class RestTask extends AsyncTask<String, String, RestResult> {
//     private String urlPath;
//     private RestMethod method;
//     private Map<String, String> params;
//     private RestTaskCallback callback;
//     private Context context;
// 
//     public RestTask(Context context, RestMethod method, String urlPath, Map<String, String> params, RestTaskCallback callback) {
//         this.context = context.getApplicationContext();
//         this.method = method;
//         this.urlPath = urlPath;
//         this.callback = callback;
//         this.params = params;
//     }
// 
//     @Override
//     protected RestResult doInBackground(String... args) {
//         try {
//             Request request = createRequest();
//             if (isCancelled())
//                 throw new InterruptedException();
//             OkHttpClient client = new OkHttpClient();
//             OAuthConsumer consumer = Session.getInstance().getOAuthConsumer(context);
//             if (consumer != null) {
//                 AccessToken accessToken = Session.getInstance().getAccessToken();
//                 if (accessToken != null) {
//                     consumer.setTokenWithSecret(accessToken.getKey(), accessToken.getSecret());
//                 }
//                 request = (Request) consumer.sign(request).unwrap();
//             }
//             Log.d("UV", urlPath);
//             if (isCancelled())
//                 throw new InterruptedException();
//             // TODO it would be nice to find a way to abort the request on cancellation
//             Response response = client.newCall(request).execute();
//             if (isCancelled())
//                 throw new InterruptedException();
//             int statusCode = response.code();
//             String body = response.body().string();
//             if (statusCode >= 400) {
//                 Log.d("UV", body);
//             }
//             if (isCancelled())
//                 throw new InterruptedException();
//             return new RestResult(statusCode, new JSONObject(body));
//         } catch (Exception e) {
//             return new RestResult(e);
//         }
//     }
// 
//     private Request createRequest() throws URISyntaxException, UnsupportedEncodingException {
//         Request.Builder builder = new Request.Builder()
//                 .addHeader("Accept-Language", Locale.getDefault().getLanguage())
//                 .addHeader("API-Client", String.format("uservoice-android-%s", UserVoice.getVersion()))
//                 .addHeader("User-Agent", String.format("uservoice-android-%s", UserVoice.getVersion()));
// 
//         String host = Session.getInstance().getConfig(context).getSite();
//         Uri.Builder uriBuilder = new Uri.Builder();
//         uriBuilder.scheme(host.contains(".us.com") ? "http" : "https");
//         uriBuilder.encodedAuthority(host);
//         uriBuilder.path(urlPath);
// 
//         if (method == RestMethod.GET || method == RestMethod.DELETE) {
//             builder.method(method.toString(), null);
//             addParamsToQueryString(builder, uriBuilder);
//         } else {
//             builder.url(uriBuilder.build().toString());
//             addParamsToBody(builder);
//         }
//         return builder.build();
//     }
// 
//     @Override
//     protected void onPostExecute(RestResult result) {
//         if (result.isError()) {
//             callback.onError(result);
//         } else {
//             try {
//                 callback.onComplete(result.getObject());
//             } catch (JSONException e) {
//                 callback.onError(new RestResult(e, result.getStatusCode(), result.getObject()));
//             }
//         }
//         super.onPostExecute(result);
//     }
// 
//     private void addParamsToQueryString(Request.Builder builder, Uri.Builder uriBuilder) throws URISyntaxException {
//         if (params != null) {
//             for (Map.Entry<String,String> param : params.entrySet()) {
//                 uriBuilder.appendQueryParameter(param.getKey(), param.getValue());
//             }
//         }
//         builder.url(uriBuilder.build().toString());
//     }
// 
//     private void addParamsToBody(Request.Builder builder) throws UnsupportedEncodingException, URISyntaxException {
//         if (params != null) {
//             FormBody.Builder paramsBuilder = new FormBody.Builder();
//             for (Map.Entry<String,String> param : params.entrySet()) {
//                 if (param.getValue() != null) {
//                     paramsBuilder.add(param.getKey(), param.getValue());
//                 }
//             }
//             builder.method(method.toString(), paramsBuilder.build());
//         }
//     }
// 
// }


// Path: UserVoiceSDK/src/com/uservoice/uservoicesdk/model/Article.java
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.uservoice.uservoicesdk.rest.Callback;
import com.uservoice.uservoicesdk.rest.RestTask;
import com.uservoice.uservoicesdk.rest.RestTaskCallback;

    public Article() {}

    public static void loadPage(Context context, int page, final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        params.put("filter", "published");
        params.put("per_page", "50");
        params.put("page", String.valueOf(page));
        doGet(context, apiPath("/articles.json"), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

    public static void loadPageForTopic(Context context, int topicId, int page, final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        params.put("filter", "published");
        params.put("per_page", "50");
        params.put("page", String.valueOf(page));
        doGet(context, apiPath("/topics/%d/articles.json", topicId), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

    public static RestTask loadInstantAnswers(Context context, String query, final Callback<List<BaseModel>> callback) {