<commit_before>package com.hipsheep.volleywrapper.network.request;

import android.net.Uri;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.RequestFuture;
import com.hipsheep.volleywrapper.Configuration;
import com.hipsheep.volleywrapper.network.model.ResponseCallback;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Base class for any request that is sent to the server.
 *
 * @author <NAME> (<EMAIL>)
 */
public abstract class BaseRequest<T> extends Request<T> {

	/**
	 * Default configuration to use for all requests (set through
	 * {@link com.hipsheep.volleywrapper.VolleyWrapper#setDefaultConfiguration(Configuration)}).
	 */
	private static Configuration sDefaultConfiguration = new Configuration();

	/**
	 * Tag used to log errors, warnings, etc. on standard output, with the name of the class that extends {@link BaseRequest}.
	 */
	private final String mLogTag = getClass().getName();

	/**
	 * Request URL, needed as a local variable to optimize the use of the {@link #getUrl()} method,
	 * which is called several times during the Request life cycle.
	 */
	private String mUrl;

	/**
	 * Query parameters used on the request.
	 */
	private Map<String, String> mQueryParams = new HashMap<>();
	/**
	 * Body parameters used on the request.
	 */
	private Map<String, Object> mBodyParams = new HashMap<>();

	/**
	 * Body of the request.
	 */
	private String mBody;

	/**
	 * Callback to use for asynchronous calls.
	 */
	private ResponseCallback<T> mResponseCallback;
	/**
	 * Callback to use for synchronous calls.
	 */
	private RequestFuture<T> mRequestFuture;


	/**
	 * Constructor method for requests that return JSON responses.
	 *
	 * @param method
	 * 		Request method to use (see {@link com.android.volley.Request.Method} to see the possible options).
	 * @param url
	 * 		URL of the request.
	 */
	protected BaseRequest(int method, String url) {
		super(method, url, null);

		// Set whether to cache responses or not, if the user set the value through VolleyWrapper
		Boolean defaultIsShouldCache = sDefaultConfiguration.isShouldCache();
		if (defaultIsShouldCache != null) {
			setShouldCache(defaultIsShouldCache);
		}

		// Set the default retry policy, if the user set one through VolleyWrapper
		RetryPolicy defaultRetryPolicy = sDefaultConfiguration.getRetryPolicy();
		if (defaultRetryPolicy != null) {
			setRetryPolicy(defaultRetryPolicy);
		}
	}

	@Override
	public String getUrl() {
		if (mUrl != null) {
			// If the URL was generated once, return it from its saved variable, instead of regenerating it again
			return mUrl;
		}

		String url = super.getUrl();

		if (!mQueryParams.isEmpty()) {
			Uri.Builder urlBuilder = Uri.parse(url).buildUpon();

			for (Map.Entry<String, String> param : mQueryParams.entrySet()) {
				urlBuilder.appendQueryParameter(param.getKey(), param.getValue());
			}

			url = urlBuilder.build().toString();
		}

		// Save URL so it doesn't need to be regenerated every time this method is called
		mUrl = url;

		return url;
	}

	@Override
	public String getBodyContentType() {
		// We must set the content type here for POST requests, otherwise the server will fail to parse
		// the headers (because Volley uses a different content type value by default)
		String defaultBodyContentType = sDefaultConfiguration.getBodyContentType();
		return defaultBodyContentType != null ? defaultBodyContentType : super.getBodyContentType();
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		Map<String, String> defaultHeaders = sDefaultConfiguration.getHeaders();

		return defaultHeaders != null ? defaultHeaders : super.getHeaders();
	}

	/**
	 * Adds a parameter to the request's query, but only if the param's value is not {@code null}.
	 *
	 * @param key
	 * 		Key of the param to add to the request's query.
	 * @param value
	 * 		Value of the param to add to the request's query. If this value is {@code null} the param won't be added.
	 */
	protected void addQueryParam(String key, Object value) {
		if (value != null) {
			mQueryParams.put(key, value.toString());
		}
	}

	/**
	 * Adds a parameter to the request's body.<commit_msg>Modified headers management in BaseRequest.<commit_after>package com.hipsheep.volleywrapper.network.request;

import android.net.Uri;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.RequestFuture;
import com.hipsheep.volleywrapper.Configuration;
import com.hipsheep.volleywrapper.network.model.ResponseCallback;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Base class for any request that is sent to the server.
 *
 * @author <NAME> (<EMAIL>)
 */
public abstract class BaseRequest<T> extends Request<T> {

	/**
	 * Default configuration to use for all requests (set through
	 * {@link com.hipsheep.volleywrapper.VolleyWrapper#setDefaultConfiguration(Configuration)}).
	 */
	private static Configuration sDefaultConfiguration = new Configuration();

	/**
	 * Tag used to log errors, warnings, etc. on standard output, with the name of the class that extends {@link BaseRequest}.
	 */
	private final String mLogTag = getClass().getName();

	/**
	 * Request URL, needed as a local variable to optimize the use of the {@link #getUrl()} method,
	 * which is called several times during the Request life cycle.
	 */
	private String mUrl;

	/**
	 * Headers used on the request.
	 */
	private Map<String, String> mHeaders = new HashMap<>();

	/**
	 * Query parameters used on the request.
	 */
	private Map<String, String> mQueryParams = new HashMap<>();
	/**
	 * Body parameters used on the request.
	 */
	private Map<String, Object> mBodyParams = new HashMap<>();

	/**
	 * Body of the request.
	 */
	private String mBody;

	/**
	 * Callback to use for asynchronous calls.
	 */
	private ResponseCallback<T> mResponseCallback;
	/**
	 * Callback to use for synchronous calls.
	 */
	private RequestFuture<T> mRequestFuture;


	/**
	 * Constructor method for requests that return JSON responses.
	 *
	 * @param method
	 * 		Request method to use (see {@link com.android.volley.Request.Method} to see the possible options).
	 * @param url
	 * 		URL of the request.
	 */
	protected BaseRequest(int method, String url) {
		super(method, url, null);

		// Set whether to cache responses or not (if it was set)
		Boolean defaultIsShouldCache = sDefaultConfiguration.isShouldCache();
		if (defaultIsShouldCache != null) {
			setShouldCache(defaultIsShouldCache);
		}

		// Set the default retry policy (if it was set)
		RetryPolicy defaultRetryPolicy = sDefaultConfiguration.getRetryPolicy();
		if (defaultRetryPolicy != null) {
			setRetryPolicy(defaultRetryPolicy);
		}

		// Add the default headers (if any)
		Map<String, String> defaultHeaders = sDefaultConfiguration.getHeaders();
		if (defaultHeaders != null) {
			mHeaders.putAll(defaultHeaders);
		}
	}

	@Override
	public String getUrl() {
		if (mUrl != null) {
			// If the URL was generated once, return it from its saved variable, instead of regenerating it again
			return mUrl;
		}

		String url = super.getUrl();

		if (!mQueryParams.isEmpty()) {
			Uri.Builder urlBuilder = Uri.parse(url).buildUpon();

			for (Map.Entry<String, String> param : mQueryParams.entrySet()) {
				urlBuilder.appendQueryParameter(param.getKey(), param.getValue());
			}

			url = urlBuilder.build().toString();
		}

		// Save URL so it doesn't need to be regenerated every time this method is called
		mUrl = url;

		return url;
	}

	@Override
	public String getBodyContentType() {
		String defaultBodyContentType = sDefaultConfiguration.getBodyContentType();
		return defaultBodyContentType != null ? defaultBodyContentType : super.getBodyContentType();
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		return mHeaders;
	}

	/**
	 * Adds a header to the request, but only if the header's value is not {@code null}.
	 *
	 * @param key
	 * 		Key of the header to add to the request.
	 * @param value
	 * 		Value of the header to add to the request. If this value is {@code null} the header won't be added.
	 */
	protected void addHeader(String key, String value) {
		if (value != null) {
			mHeaders.put(key, value);
		}
	}

	/**
	 * Adds a parameter to the request's query, but only if the param's value is not {@code null}.
	 *
	 * @param key
	 * 		Key of the param to add to the request's query.
	 * @param value
	 * 		Value of the param to add to the request's query. If this value is {@code null} the param won't be added.
	 */
	protected void addQueryParam(String key, Object value) {
		if (value != null) {
			mQueryParams.put(key, value.toString());
		}
	}

	/**
	 * Adds a parameter to the request's body.
{'fraction_non_alphanumeric': 0.10479267635971998, 'fraction_numerical': 0.0, 'mean_word_length': 3.931492299522039, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '2375144', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2619, 'n_words': 921}