<commit_before>package com.familybiz.greg.taqueue.network;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by <NAME>
 */
public class NetworkRequest {

	private String BASE_URL = "http://nine.eng.utah.edu";

	private RequestQueue mQueue;

	public NetworkRequest(Context context) {
		mQueue = Volley.newRequestQueue(context);
	}

	/**
	 * Creates a new url using the BASE_URL and the given url: BASE_URL + url.
	 */
	public void executeGetRequest(String url) {
		executeGetRequest(url, "", "");
	}

	/**
	 * Creates a new url using the BASE_URL and the given url: BASE_URL + url.  Adds the username (id)
	 * and password (token) as basic authorization to the header.
	 */
	public void executeGetRequest(String url, String id, String token) {
		// TODO: Make the change to using Uri.Builder

		// Make it so the authorization variables can be accessed from the inner class
		final boolean authorize = !id.isEmpty();
		final String username = id;
		final String password = <PASSWORD>;

		StringRequest stringRequest = new StringRequest(
				Request.Method.GET,
				BASE_URL + url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						parseResponse(response);
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Get request error", "Something was wrong with the request.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Accept", "application/json");

				// Encode the username and password if provided
				if (authorize) {
					String code = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
					code = code.replaceAll("\n", "");
					code = "Basic " + code;
					headers.put("Authorization", code);
				}
				return headers;
			}
		};

		mQueue.add(stringRequest);
	}

	public void executePostRequest(String url, JSONObject params) {
		executePostRequest(url, params, "", "");
	}

	public void executePostRequest(String url, JSONObject params, String id, String token) {
		// TODO: Make the change to using Uri.Builder

		// Make it so the authorization variables can be accessed from the inner class
		final boolean authorize = !id.isEmpty();
		final String username = id;
		final String password = <PASSWORD>;

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				BASE_URL + url,
				params,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						parseResponse(response.toString());
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Post request error", "Something went wrong.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Content-Type", "application/json");
				headers.put("Accept", "application/json");

				// Encode the username and password if provided
				if (authorize) {
					String code = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
					headers.put("Authorization", code);
				}
				return headers;
			}
		};

		mQueue.add(jsonObjectRequest);
	}

	public void executeDeleteRequest(String url, String id, String token) {
		final String username = id;
		final String password = <PASSWORD>;

		StringRequest stringRequest = new StringRequest(
				Request.Method.DELETE,
				BASE_URL + url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						parseResponse(response);
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Delete request error", "Something was wrong with the request.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Accept", "application/json");

				// Encode the username and password
				String code = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
				headers.put("Authorization", code);
				return headers;
			}
		};

		mQueue.add(stringRequest);
	}


	/**
	 * Parses the HTTP response into either a json object or a json array, logs the error if it
	 * fails.  Once parsed, it will trigger all appropriate listeners.
	 */
	private void parseResponse(String response) {
		// Json array
		try {
			new JSONArray(response);
			for (OnJsonArrayReceivedListener listener : mOnJsonArrayReceivedListeners)
				listener.onJsonArrayReceived(response);
		}
		catch (JSONException e) {

			// Json object
			try {
				new JSONObject(response);
				for (OnJsonObjectReceivedListener listener : mOnJsonObjectReceivedListeners)
					listener.onJsonObjectReceived(response);
			}<commit_msg>Fixed bug with incorrect authorization encoding.<commit_after>package com.familybiz.greg.taqueue.network;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by <NAME>
 */
public class NetworkRequest {

	private String BASE_URL = "http://nine.eng.utah.edu";

	private RequestQueue mQueue;

	public NetworkRequest(Context context) {
		mQueue = Volley.newRequestQueue(context);
	}

	/**
	 * Creates a new url using the BASE_URL and the given url: BASE_URL + url.
	 */
	public void executeGetRequest(String url) {
		executeGetRequest(url, "", "");
	}

	/**
	 * Creates a new url using the BASE_URL and the given url: BASE_URL + url.  Adds the username (id)
	 * and password (token) as basic authorization to the header.
	 */
	public void executeGetRequest(String url, final String id, String token) {
		// TODO: Make the change to using Uri.Builder

		// Make it so the authorization variables can be accessed from the inner class
		final boolean authorize = !id.isEmpty();
		final String username = id;
		final String password = <PASSWORD>;

		StringRequest stringRequest = new StringRequest(
				Request.Method.GET,
				BASE_URL + url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						parseResponse(response);
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Get request error", "Something was wrong with the request.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Accept", "application/json");

				// Encode the username and password if provided
				if (authorize)
					encodeHeader(headers, username, password);

				return headers;
			}
		};

		mQueue.add(stringRequest);
	}

	public void executePostRequest(String url, JSONObject params) {
		executePostRequest(url, params, "", "");
	}

	public void executePostRequest(String url, JSONObject params, String id, String token) {
		// TODO: Make the change to using Uri.Builder

		// Make it so the authorization variables can be accessed from the inner class
		final boolean authorize = !id.isEmpty();
		final String username = id;
		final String password = <PASSWORD>;

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				BASE_URL + url,
				params,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						parseResponse(response.toString());
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Post request error", "Something went wrong.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Content-Type", "application/json");
				headers.put("Accept", "application/json");

				// Encode the username and password if provided
				if (authorize)
					encodeHeader(headers, username, password);

				return headers;
			}
		};

		mQueue.add(jsonObjectRequest);
	}

	public void executeDeleteRequest(String url, String id, String token) {
		final String username = id;
		final String password = <PASSWORD>;

		StringRequest stringRequest = new StringRequest(
				Request.Method.DELETE,
				BASE_URL + url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						parseResponse(response);
					}
				},
				new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("Delete request error", "Something was wrong with the request.");
					}
				}){

			// Set the correct header to prevent getting html back
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Accept", "application/json");

				// Encode the username and password
				encodeHeader(headers, username, password);
				return headers;
			}
		};

		mQueue.add(stringRequest);
	}

	private void encodeHeader(Map<String, String> headers, String username, String password) {
		String code = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
		code = code.replaceAll("\n", "");
		code = "Basic " + code;
		headers.put("Authorization", code);
	}


	/**
	 * Parses the HTTP response into either a json object or a json array, logs the error if it
	 * fails.  Once parsed, it will trigger all appropriate listeners.
	 */
	private void parseResponse(String response) {
		// Json array
		try {
			new JSONArray(response);
			for (OnJsonArrayReceivedListener listener : mOnJsonArrayReceivedListeners)
				listener.onJsonArrayReceived(response);
		}
		catch (JSONException e) {

			// Json object
			try {
				new JSONObject(response);
				for (OnJsonObjectReceivedListener listener : mOnJsonObjectReceivedListeners)
					listener.onJsonObjectReceived(response);
			}
{'fraction_non_alphanumeric': 0.10520381613183001, 'fraction_numerical': 0.0017346053772766695, 'mean_word_length': 3.674098094852047, 'pattern_counts': {'":': 4, '<': 36, '<?xml version=': 0, '>': 36, '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': '2145188', 'n_tokens_mistral': 3864, 'n_tokens_neox': 3177, 'n_words': 974}