<commit_before>package floobits.common;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import floobits.FlooContext;
import floobits.utilities.Flog;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class API {

    public static boolean createWorkspace(String owner, String workspace, FlooContext context, boolean notPublic) {
        PostMethod method;

        method = new PostMethod("/api/workspace/");
        Gson gson = new Gson();
        String json = gson.toJson(new HTTPWorkspaceRequest(owner, workspace, notPublic));
        try {
            method.setRequestEntity(new StringRequestEntity(json, "application/json", "UTF-8"));
            apiRequest(method, context);
        } catch (IOException e) {
            context.error_message(String.format("Could not create workspace: %s", e.toString()));
            return false;
        }

        int code = method.getStatusCode();
        switch (code) {
            case 400:
                // Todo: pick a new name or something
                context.error_message("Invalid workspace name (a-zA-Z0-9).");
                return false;
            case 402:
                String details;
                try {
                    String res = method.getResponseBodyAsString();
                    JsonObject obj = (JsonObject)new JsonParser().parse(res);
                    details = obj.get("detail").getAsString();
                } catch (IOException e) {
                    Flog.warn(e);
                    return false;
                }
                context.error_message(details);
                return false;
            case 409:
                context.status_message("The workspace already exists so I am joining it.");
            case 201:
                context.status_message("Workspace created.");
                return true;
            case 401:
                Flog.log("Auth failed");
                context.error_message("There is an invalid username or secret in your ~/.floorc and you were not able to authenticate.");
                VirtualFile floorc = LocalFileSystem.getInstance().findFileByIoFile(new File(Settings.floorcPath));
                if (floorc == null) {
                    return false;
                }
                FileEditorManager.getInstance(context.project).openFile(floorc, true);
                return false;
            default:
                try {
                    Flog.warn(String.format("Unknown error creating workspace:\n%s", method.getResponseBodyAsString()));
                } catch (IOException e) {
                    Flog.warn(e);
                }
                return false;
        }
    }
    public static boolean updateWorkspace(final FlooUrl f, HTTPWorkspaceRequest workspaceRequest, FlooContext context) {

        PutMethod method = new PutMethod(String.format("/api/workspace/%s/%s/", f.owner, f.workspace));
        Gson gson = new Gson();
        String json = gson.toJson(workspaceRequest);
        try {
            method.setRequestEntity(new StringRequestEntity(json, "application/json", "UTF-8"));
            apiRequest(method, context);
        } catch (IOException e) {
            context.error_message(String.format("Could not create workspace: %s", e.toString()));
            return false;
        }
        String responseBodyAsString;
        try {
            responseBodyAsString = method.getResponseBodyAsString();
        } catch (IOException e) {
            Flog.warn(e);
            return false;
        }
        Flog.log(responseBodyAsString);
        return method.getStatusCode() < 300;
    }

    public static HTTPWorkspaceRequest getWorkspace(final FlooUrl f, FlooContext context) {

        HttpMethod method;
        try {
            method = API.getWorkspace(f.owner, f.workspace, context);
        } catch (IOException e) {
            return null;
        }
        String responseBodyAsString;
        try {
            responseBodyAsString = method.getResponseBodyAsString();
        } catch (IOException e) {
            return null;
        }
        return new Gson().fromJson(responseBodyAsString, (Type) HTTPWorkspaceRequest.class);
    }

    public static boolean workspaceExists(final FlooUrl f, FlooContext context) {
        if (f == null) {
            return false;
        }
        HttpMethod method;
        try {
            method = API.getWorkspace(f.owner, f.workspace, context);
        } catch (IOException e) {
            Flog.warn(e);
            return false;
        }

        return method.getStatusCode() < 400;
    }
    static public HttpMethod getWorkspace(String owner, String workspace, FlooContext context) throws IOException {
        return apiRequest(new GetMethod(String.format("/api/workspace/%s/%s/", owner, workspace)), context);
    }

    static public HttpMethod apiRequest(HttpMethod method, FlooContext context) throws IOException, IllegalArgumentException{
        final HttpClient client = new HttpClient();
//        method.setFollowRedirects(true);
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        HttpConnectionParams connectionParams = connectionManager.getParams();
        connectionParams.setParameter("http.protocol.handle-redirects", true);
        connectionParams.setSoTimeout(3000);
        connectionParams.setConnectionTimeout(3000);
        Settings settings = new Settings(context);
        HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);
//        params.setParameter("http.protocol.handle-redirects", true);
        client.setParams(params);
        Credentials credentials = new UsernamePasswordCredentials(settings.get("username"), settings.get("secret"));
        client.getState().setCredentials(AuthScope.ANY, credentials);
        client.getHostConfiguration().setHost(Constants.defaultHost, 443, new Protocol("https", new SecureProtocolSocketFactory() {<commit_msg>don't commit commented out code.<commit_after>package floobits.common;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import floobits.FlooContext;
import floobits.utilities.Flog;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class API {

    public static boolean createWorkspace(String owner, String workspace, FlooContext context, boolean notPublic) {
        PostMethod method;

        method = new PostMethod("/api/workspace/");
        Gson gson = new Gson();
        String json = gson.toJson(new HTTPWorkspaceRequest(owner, workspace, notPublic));
        try {
            method.setRequestEntity(new StringRequestEntity(json, "application/json", "UTF-8"));
            apiRequest(method, context);
        } catch (IOException e) {
            context.error_message(String.format("Could not create workspace: %s", e.toString()));
            return false;
        }

        int code = method.getStatusCode();
        switch (code) {
            case 400:
                // Todo: pick a new name or something
                context.error_message("Invalid workspace name (a-zA-Z0-9).");
                return false;
            case 402:
                String details;
                try {
                    String res = method.getResponseBodyAsString();
                    JsonObject obj = (JsonObject)new JsonParser().parse(res);
                    details = obj.get("detail").getAsString();
                } catch (IOException e) {
                    Flog.warn(e);
                    return false;
                }
                context.error_message(details);
                return false;
            case 409:
                context.status_message("The workspace already exists so I am joining it.");
            case 201:
                context.status_message("Workspace created.");
                return true;
            case 401:
                Flog.log("Auth failed");
                context.error_message("There is an invalid username or secret in your ~/.floorc and you were not able to authenticate.");
                VirtualFile floorc = LocalFileSystem.getInstance().findFileByIoFile(new File(Settings.floorcPath));
                if (floorc == null) {
                    return false;
                }
                FileEditorManager.getInstance(context.project).openFile(floorc, true);
                return false;
            default:
                try {
                    Flog.warn(String.format("Unknown error creating workspace:\n%s", method.getResponseBodyAsString()));
                } catch (IOException e) {
                    Flog.warn(e);
                }
                return false;
        }
    }
    public static boolean updateWorkspace(final FlooUrl f, HTTPWorkspaceRequest workspaceRequest, FlooContext context) {

        PutMethod method = new PutMethod(String.format("/api/workspace/%s/%s/", f.owner, f.workspace));
        Gson gson = new Gson();
        String json = gson.toJson(workspaceRequest);
        try {
            method.setRequestEntity(new StringRequestEntity(json, "application/json", "UTF-8"));
            apiRequest(method, context);
        } catch (IOException e) {
            context.error_message(String.format("Could not create workspace: %s", e.toString()));
            return false;
        }
        String responseBodyAsString;
        try {
            responseBodyAsString = method.getResponseBodyAsString();
        } catch (IOException e) {
            Flog.warn(e);
            return false;
        }
        Flog.log(responseBodyAsString);
        return method.getStatusCode() < 300;
    }

    public static HTTPWorkspaceRequest getWorkspace(final FlooUrl f, FlooContext context) {

        HttpMethod method;
        try {
            method = API.getWorkspace(f.owner, f.workspace, context);
        } catch (IOException e) {
            return null;
        }
        String responseBodyAsString;
        try {
            responseBodyAsString = method.getResponseBodyAsString();
        } catch (IOException e) {
            return null;
        }
        return new Gson().fromJson(responseBodyAsString, (Type) HTTPWorkspaceRequest.class);
    }

    public static boolean workspaceExists(final FlooUrl f, FlooContext context) {
        if (f == null) {
            return false;
        }
        HttpMethod method;
        try {
            method = API.getWorkspace(f.owner, f.workspace, context);
        } catch (IOException e) {
            Flog.warn(e);
            return false;
        }

        return method.getStatusCode() < 400;
    }
    static public HttpMethod getWorkspace(String owner, String workspace, FlooContext context) throws IOException {
        return apiRequest(new GetMethod(String.format("/api/workspace/%s/%s/", owner, workspace)), context);
    }

    static public HttpMethod apiRequest(HttpMethod method, FlooContext context) throws IOException, IllegalArgumentException{
        final HttpClient client = new HttpClient();
//        NOTE: we cant tell java to follow redirects because they can fail.
        HttpConnectionManager connectionManager = client.getHttpConnectionManager();
        HttpConnectionParams connectionParams = connectionManager.getParams();
        connectionParams.setParameter("http.protocol.handle-redirects", true);
        connectionParams.setSoTimeout(3000);
        connectionParams.setConnectionTimeout(3000);
        Settings settings = new Settings(context);
        HttpClientParams params = client.getParams();
        params.setAuthenticationPreemptive(true);
        client.setParams(params);
        Credentials credentials = new UsernamePasswordCredentials(settings.get("username"), settings.get("secret"));
        client.getState().setCredentials(AuthScope.ANY, credentials);
        client.getHostConfiguration().setHost(Constants.defaultHost, 443, new Protocol("https", new SecureProtocolSocketFactory() {
{'fraction_non_alphanumeric': 0.09684846740538207, 'fraction_numerical': 0.005180601525399338, 'mean_word_length': 2.4860797592174566, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '5368538', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3472, 'n_words': 807}