<commit_before>package com.gurkensalat.android.xingsync.api;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.gurkensalat.android.xingsync.keys.XingOAuthKeys;
import com.gurkensalat.android.xingsync.preferences.SyncPrefs_;

@EBean
public class ContactsCall
{
	private static Logger LOG = LoggerFactory.getLogger(ContactsCall.class);

	@Pref
	SyncPrefs_ syncPrefs;

	public JSONObject perform(final Object... args)
	{
		JSONObject json = new JSONObject();

		if (syncPrefs.debugMockApiCalls().get())
		{
			LOG.info("About to mock API call");

			StringBuffer sb = new StringBuffer(1024);

			sb.append("{");
			sb.append("  \"contacts\": {");
			sb.append("    \"total\": 297,");
			sb.append("    \"users\": [");
			sb.append("      {");
			sb.append("        \"id\": \"6841253_007b9e\",");
			sb.append("        \"first_name\": \"Hasmet\",");
			sb.append("        \"last_name\": \"Acar\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      },");
			sb.append("      {");
			sb.append("        \"id\": \"4261890_ffd916\",");
			sb.append("        \"first_name\": \"Murat\",");
			sb.append("        \"last_name\": \"Acun\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      },");
			sb.append("      {");
			sb.append("        \"id\": \"12824542_5078cc\",");
			sb.append("        \"first_name\": \"Petra\",");
			sb.append("        \"last_name\": \"Ghotra\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      }");
			sb.append("    ]");
			sb.append("  }");
			sb.append("}");

			LOG.info(sb.toString());

			try
			{
				json = new JSONObject(sb.toString());
			}
			catch (JSONException e)
			{
				LOG.error("while parsing answer string", e);
			}
		}
		else
		{
			LOG.info("About to make real API call");

			// For OAuth.debugOut()
			System.setProperty("debug", "true");

			String access_token = syncPrefs.oauth_token().get();
			String token_secret = syncPrefs.oauth_token_secret().get();

			LOG.info("    TOKEN: '{}'", access_token);
			LOG.info("    TOKEN SECRET: '{}'", token_secret);

			// create a consumer object and configure it with the access
			// token and token secret obtained from the service provider
			//
			// OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
			// CONSUMER_SECRET);
			OAuthConsumer consumer = new CommonsHttpOAuthConsumer(XingOAuthKeys.CONSUMER_KEY, XingOAuthKeys.CONSUMER_SECRET);
			LOG.info("    consumer: {}", consumer);
			consumer.setTokenWithSecret(access_token, token_secret);
			LOG.info("    consumer: {}", consumer);

			StringBuilder callUrl = new StringBuilder();

			callUrl.append(XingOAuthKeys.API_URL_BASE);
			callUrl.append("/users/me/contacts.json");
			callUrl.append("?limit=10");
			// callUrl.append("&offset=86");
			// callUrl.append("&order_by=last_name");
			callUrl.append("&user_fields=").append(syncPrefs.fieldsToFetch().get());
			// callUrl.append("");
			// callUrl.append("");

			try
			{
				// create an HTTP request to a protected resource
				LOG.info("    url: {}", callUrl.toString());

				DefaultHttpClient httpclient = new DefaultHttpClient();
				LOG.info("    httpclient: {}", httpclient);

				HttpGet request = new HttpGet(callUrl.toString());
				LOG.info("    request: {}", request);

				// sign the request
				consumer.sign(request);
				LOG.info("    consumer: {}", consumer);

				// send the request
				HttpResponse response = httpclient.execute(request);
				LOG.info("    status : {}", response.getStatusLine());

				InputStream data = response.getEntity().getContent();
				BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data));
				String responseLine;
				StringBuilder responseBuilder = new StringBuilder();
				while ((responseLine = bufferedReader.readLine()) != null)
				{
					LOG.info("    read: '{}'", responseLine);
					responseBuilder.append(responseLine);
				}
				// Log.i(TAG,"Response : " + responseBuilder.toString());
				// return responseBuilder.toString();
			}
			catch (Exception e)
			{
				LOG.error("While making real API call", e);
			}
		}

		return json;<commit_msg>And actually convert answer to JSON<commit_after>package com.gurkensalat.android.xingsync.api;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.gurkensalat.android.xingsync.keys.XingOAuthKeys;
import com.gurkensalat.android.xingsync.preferences.SyncPrefs_;

@EBean
public class ContactsCall
{
	private static Logger LOG = LoggerFactory.getLogger(ContactsCall.class);

	@Pref
	SyncPrefs_ syncPrefs;

	public JSONObject perform(final Object... args)
	{
		JSONObject json = new JSONObject();

		if (syncPrefs.debugMockApiCalls().get())
		{
			LOG.info("About to mock API call");

			StringBuffer sb = new StringBuffer(1024);

			sb.append("{");
			sb.append("  \"contacts\": {");
			sb.append("    \"total\": 297,");
			sb.append("    \"users\": [");
			sb.append("      {");
			sb.append("        \"id\": \"6841253_007b9e\",");
			sb.append("        \"first_name\": \"Hasmet\",");
			sb.append("        \"last_name\": \"Acar\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      },");
			sb.append("      {");
			sb.append("        \"id\": \"4261890_ffd916\",");
			sb.append("        \"first_name\": \"Murat\",");
			sb.append("        \"last_name\": \"Acun\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      },");
			sb.append("      {");
			sb.append("        \"id\": \"12824542_5078cc\",");
			sb.append("        \"first_name\": \"Petra\",");
			sb.append("        \"last_name\": \"Ghotra\",");
			sb.append("        \"display_name\": \"<NAME>\",");
			sb.append("        \"active_email\": null");
			sb.append("      }");
			sb.append("    ]");
			sb.append("  }");
			sb.append("}");

			LOG.info(sb.toString());

			try
			{
				json = new JSONObject(sb.toString());
			}
			catch (JSONException e)
			{
				LOG.error("while parsing answer string", e);
			}
		}
		else
		{
			LOG.info("About to make real API call");

			// For OAuth.debugOut()
			System.setProperty("debug", "true");

			String access_token = syncPrefs.oauth_token().get();
			String token_secret = syncPrefs.oauth_token_secret().get();

			LOG.info("    TOKEN: '{}'", access_token);
			LOG.info("    TOKEN SECRET: '{}'", token_secret);

			// create a consumer object and configure it with the access
			// token and token secret obtained from the service provider
			//
			// OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
			// CONSUMER_SECRET);
			OAuthConsumer consumer = new CommonsHttpOAuthConsumer(XingOAuthKeys.CONSUMER_KEY, XingOAuthKeys.CONSUMER_SECRET);
			LOG.info("    consumer: {}", consumer);
			consumer.setTokenWithSecret(access_token, token_secret);
			LOG.info("    consumer: {}", consumer);

			StringBuilder callUrl = new StringBuilder();

			callUrl.append(XingOAuthKeys.API_URL_BASE);
			callUrl.append("/users/me/contacts.json");
			callUrl.append("?limit=100");
			// callUrl.append("&offset=86");
			// callUrl.append("&order_by=last_name");
			callUrl.append("&user_fields=").append(syncPrefs.fieldsToFetch().get());
			// callUrl.append("");
			// callUrl.append("");

			try
			{
				// create an HTTP request to a protected resource
				LOG.info("    url: {}", callUrl.toString());

				DefaultHttpClient httpclient = new DefaultHttpClient();
				LOG.info("    httpclient: {}", httpclient);

				HttpGet request = new HttpGet(callUrl.toString());
				LOG.info("    request: {}", request);

				// sign the request
				consumer.sign(request);
				LOG.info("    consumer: {}", consumer);

				// send the request
				HttpResponse response = httpclient.execute(request);
				LOG.info("    status : {}", response.getStatusLine());

				InputStream data = response.getEntity().getContent();
				BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data));
				String responseLine;
				StringBuilder responseBuilder = new StringBuilder();
				while ((responseLine = bufferedReader.readLine()) != null)
				{
					LOG.info("    read: '{}'", responseLine);
					responseBuilder.append(responseLine);
				}
				// Log.i(TAG,"Response : " + responseBuilder.toString());
				// return responseBuilder.toString();

				LOG.info(responseBuilder.toString());

				try
				{
					json = new JSONObject(responseBuilder.toString());
				}
				catch (JSONException e)
				{
					LOG.error("while parsing answer string", e);
				}
			}
			catch (Exception e)
			{
				LOG.error("While making real API call", e);
			}
		}

		return json;
{'fraction_non_alphanumeric': 0.1704353023209483, 'fraction_numerical': 0.009263870903476442, 'mean_word_length': 3.2687074829931975, 'pattern_counts': {'":': 36, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3024435', 'n_tokens_mistral': 3729, 'n_tokens_neox': 3172, 'n_words': 615}