<commit_before>package com.name.temp.keeper.Data;

import android.content.Context;

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.Volley;

import org.json.JSONObject;


public class Data{

    private static Data instance;
    private RequestQueue requestQueue;

    private static final String CONTROLLERS_ROOT = "https://fletchto99.com/other/sites/school/seg2105/controllers/";
    private static final String CONTROLLER_GET = "get/";
    private static final String CONTROLLER_UPDATE = "update/";
    private static final String CONTROLLER_REMOVE = "remove/";
    private static final String CONTROLLER_TYPE = ".php";

    private Data(Context context){
        this.requestQueue = Volley.newRequestQueue(context.getApplicationContext());
    }

    public static synchronized Data getInstance(Context context) {
        if (Data.instance == null) {
            Data.instance = new Data(context);
        }
        return Data.instance;
    }

    public void get(final JSONObject data, final String controller, final Response.Listener <JSONObject> listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_GET+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    public void update(final JSONObject data, final String controller, final Response.Listener<JSONObject> listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_UPDATE+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    public void remove(final JSONObject data, final String controller, final Response.Listener<JSONObject>  listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_REMOVE+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    private Response.ErrorListener generateVolleyErrorListener(final WebErrorListener wel) {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    wel.onError(new JSONObject(new String(error.networkResponse.data, "utf-8")));
                } catch ( Exception e ) {
                    //TODO: Handle errors not generated by the conroller?
                }
            }
        };
    }

}<commit_msg>Added the ability to authenticated via the client side<commit_after>package com.name.temp.keeper.Data;

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

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.JSONObject;

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


public class Data{

    private static Data instance = null;
    private RequestQueue requestQueue;

    private static final String CONTROLLERS_ROOT = "https://fletchto99.com/other/sites/school/seg2105/controllers/";
    private static final String CONTROLLER_GET = "get/";
    private static final String CONTROLLER_UPDATE = "update/";
    private static final String CONTROLLER_REMOVE = "remove/";
    private static final String CONTROLLER_TYPE = ".php";
    private static final String LOGIN_CONTROLLER = "login";
    private static final String LOGOUT_CONTROLLER = "logout";

    private Data(Context context){
        this.requestQueue = Volley.newRequestQueue(context.getApplicationContext());
    }

    public static synchronized Data createInstance(final Context context) {
        if (Data.instance == null) {
            Data.instance = new Data(context);
        }
        return Data.getInstance();
    }

    public static synchronized Data getInstance() {
        if (Data.instance == null) {
            throw new NullPointerException("Instance has not yet been initialized!");
        }
        return Data.instance;
    }

    public void authenticate(final String username, final String password, final Response.Listener<String> listener, final WebErrorListener errorListener) {
        String url = CONTROLLERS_ROOT+LOGIN_CONTROLLER+CONTROLLER_TYPE;
        StringRequest request = new StringRequest(
                Request.Method.GET,
                url,
                listener,
                generateVolleyErrorListener(errorListener)) {

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

        requestQueue.add(request);
    }

    public void logout(final Response.Listener <JSONObject> listener, final WebErrorListener errorListener) {
        String url = CONTROLLERS_ROOT+CONTROLLER_UPDATE+LOGOUT_CONTROLLER+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, null, listener, generateVolleyErrorListener(errorListener)));

    }

    public void get(final JSONObject data, final String controller, final Response.Listener <JSONObject> listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_GET+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    public void update(final JSONObject data, final String controller, final Response.Listener<JSONObject> listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_UPDATE+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    public void remove(final JSONObject data, final String controller, final Response.Listener<JSONObject>  listener, final WebErrorListener errorListener){
        String url = CONTROLLERS_ROOT+CONTROLLER_REMOVE+controller+CONTROLLER_TYPE;
        requestQueue.add(new JsonObjectRequest(Request.Method.POST, url, data, listener, generateVolleyErrorListener(errorListener)));
    }

    private Response.ErrorListener generateVolleyErrorListener(final WebErrorListener wel) {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    wel.onError(new JSONObject(new String(error.networkResponse.data, "utf-8")));
                } catch ( Exception e ) {
                    e.printStackTrace();
                    //TODO: Handle errors not generated by the conroller?
                }
            }
        };
    }

    private Map<String, String> createBasicAuthHeader(String username, String password) {
        Map<String, String> headerMap = new HashMap<>();
        String credentials = username + ":" + password;
        String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        headerMap.put("Authorization", "Basic " + base64EncodedCredentials);
        return headerMap;
    }

}
{'fraction_non_alphanumeric': 0.08362779740871613, 'fraction_numerical': 0.0031409501374165686, 'mean_word_length': 3.5218934911242603, 'pattern_counts': {'":': 1, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '5107838', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1970, 'n_words': 482}