<commit_before>package org.spacehq.mc.auth.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.spacehq.mc.auth.exception.request.RequestException;
import org.spacehq.mc.auth.exception.request.ServiceUnavailableException;
import org.spacehq.mc.auth.exception.request.InvalidCredentialsException;
import org.spacehq.mc.auth.exception.request.UserMigratedException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;

/**
 * Utilities for making HTTP requests.
 */
public class HTTP {
    private static final Gson GSON;

    static {
        GSON = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDSerializer()).create();
    }

    private HTTP() {
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @throws RequestException If an error occurs while making the request.
     */
    public static void makeRequest(Proxy proxy, String url, Object input) throws RequestException {
        makeRequest(proxy, url, input, null);
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @param clazz Class to provide the response as.
     * @return The response of the request.
     * @throws RequestException If an error occurs while making the request.
     */
    public static <T> T makeRequest(Proxy proxy, String url, Object input, Class<T> clazz) throws RequestException {
        JsonElement response = null;
        try {
            String jsonString = input == null ? performGetRequest(proxy, url) : performPostRequest(proxy, url, GSON.toJson(input), "application/json");
            response = GSON.fromJson(jsonString, JsonElement.class);
        } catch(Exception e) {
            throw new ServiceUnavailableException("Could not make request to '" + url + "'.", e);
        }

        if(response.isJsonObject()) {
            JsonObject object = response.getAsJsonObject();
            if(object.has("error")) {
                String error = object.get("error").getAsString();
                String cause = object.has("cause") ? object.get("cause").getAsString() : "";
                String errorMessage = object.has("errorMessage") ? object.get("errorMessage").getAsString() : "";
                if(!error.equals("")) {
                    if(error.equals("ForbiddenOperationException")) {
                        if(cause != null && cause.equals("UserMigratedException")) {
                            throw new UserMigratedException(errorMessage);
                        } else {
                            throw new InvalidCredentialsException(errorMessage);
                        }
                    } else {
                        throw new RequestException(errorMessage);
                    }
                }
            }
        }

        return clazz != null ? GSON.fromJson(response, clazz) : null;
    }<commit_msg>Fix NPE when receiving a null response.<commit_after>package org.spacehq.mc.auth.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.spacehq.mc.auth.exception.request.RequestException;
import org.spacehq.mc.auth.exception.request.ServiceUnavailableException;
import org.spacehq.mc.auth.exception.request.InvalidCredentialsException;
import org.spacehq.mc.auth.exception.request.UserMigratedException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;

/**
 * Utilities for making HTTP requests.
 */
public class HTTP {
    private static final Gson GSON;

    static {
        GSON = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDSerializer()).create();
    }

    private HTTP() {
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @throws RequestException If an error occurs while making the request.
     */
    public static void makeRequest(Proxy proxy, String url, Object input) throws RequestException {
        makeRequest(proxy, url, input, null);
    }

    /**
     * Makes an HTTP request.
     *
     * @param proxy Proxy to use when making the request.
     * @param url   URL to make the request to.
     * @param input Input to provide in the request.
     * @param clazz Class to provide the response as.
     * @return The response of the request.
     * @throws RequestException If an error occurs while making the request.
     */
    public static <T> T makeRequest(Proxy proxy, String url, Object input, Class<T> clazz) throws RequestException {
        JsonElement response = null;
        try {
            String jsonString = input == null ? performGetRequest(proxy, url) : performPostRequest(proxy, url, GSON.toJson(input), "application/json");
            response = GSON.fromJson(jsonString, JsonElement.class);
        } catch(Exception e) {
            throw new ServiceUnavailableException("Could not make request to '" + url + "'.", e);
        }

        if(response != null) {
            if(response.isJsonObject()) {
                JsonObject object = response.getAsJsonObject();
                if(object.has("error")) {
                    String error = object.get("error").getAsString();
                    String cause = object.has("cause") ? object.get("cause").getAsString() : "";
                    String errorMessage = object.has("errorMessage") ? object.get("errorMessage").getAsString() : "";
                    if(!error.equals("")) {
                        if(error.equals("ForbiddenOperationException")) {
                            if(cause != null && cause.equals("UserMigratedException")) {
                                throw new UserMigratedException(errorMessage);
                            } else {
                                throw new InvalidCredentialsException(errorMessage);
                            }
                        } else {
                            throw new RequestException(errorMessage);
                        }
                    }
                }
            }

            if(clazz != null) {
                return GSON.fromJson(response, clazz);
            }
        }

        return null;
    }
{'fraction_non_alphanumeric': 0.1011461318051576, 'fraction_numerical': 0.0, 'mean_word_length': 2.275926794931957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5388673', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1798, 'n_words': 497}