<commit_before>package com.namelessmc.java_api;

import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.namelessmc.java_api.exception.*;
import com.namelessmc.java_api.modules.websend.WebsendAPI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.math.BigInteger;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public final class NamelessAPI {

	static final Gson GSON = new Gson();

	@NotNull
	private final RequestHandler requests;

	NamelessAPI(@NotNull final RequestHandler requests) {
		this.requests = Objects.requireNonNull(requests, "Request handler is null");
	}

	@NotNull
	RequestHandler getRequestHandler() {
		return this.requests;
	}

	@NotNull
	public URL getApiUrl() {
		return this.getRequestHandler().getApiUrl();
	}

	@NotNull
	public String getApiKey() {
		return getApiKey(this.getApiUrl().toString());
	}

	@NotNull
	static String getApiKey(@NotNull final String url) {
		if (url.endsWith("/")) {
			return getApiKey(url.substring(0, url.length() - 1));
		}

		return url.substring(url.lastIndexOf('/'));
	}

	// TODO Add back when NamelessMC has reimplemented it
//	/**
//	 * Get announcements visible to guests. Use {@link #getAnnouncements(NamelessUser)} for non-guest announcements.
//	 * @return list of current announcements
//	 * @throws NamelessException if there is an error in the request
//	 */
//	@NotNull
//	public List<@NotNull Announcement> getAnnouncements() throws NamelessException {
//		final JsonObject response = this.requests.get();
//
//		return getAnnouncements(response);
//	}

	/**
	 * Get all announcements visible for the player with the specified uuid
	 *
	 * @param user player to get visible announcements for
	 * @return list of current announcements visible to the player
	 * @throws NamelessException if there is an error in the request
	 */
	@NotNull
	public List<@NotNull Announcement> getAnnouncements(@NotNull final NamelessUser user) throws NamelessException {
		final JsonObject response = this.requests.get("users/" + user.getId() + "/announcements");

		return getAnnouncements(response);
	}

	@NotNull
	private static Set<@NotNull String> toStringSet(@NotNull final JsonArray jsonArray) {
		return StreamSupport.stream(jsonArray.spliterator(), false).map(JsonElement::getAsString).collect(Collectors.toSet());
	}

	@NotNull
	private List<@NotNull Announcement> getAnnouncements(@NotNull final JsonObject response) {
		return StreamSupport.stream(response.getAsJsonArray("announcements").spliterator(), false)<commit_msg>Website has guest announcements endpoint again<commit_after>package com.namelessmc.java_api;

import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.namelessmc.java_api.exception.*;
import com.namelessmc.java_api.modules.websend.WebsendAPI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.math.BigInteger;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public final class NamelessAPI {

	static final Gson GSON = new Gson();

	@NotNull
	private final RequestHandler requests;

	NamelessAPI(@NotNull final RequestHandler requests) {
		this.requests = Objects.requireNonNull(requests, "Request handler is null");
	}

	@NotNull
	RequestHandler getRequestHandler() {
		return this.requests;
	}

	@NotNull
	public URL getApiUrl() {
		return this.getRequestHandler().getApiUrl();
	}

	@NotNull
	public String getApiKey() {
		return getApiKey(this.getApiUrl().toString());
	}

	@NotNull
	static String getApiKey(@NotNull final String url) {
		if (url.endsWith("/")) {
			return getApiKey(url.substring(0, url.length() - 1));
		}

		return url.substring(url.lastIndexOf('/'));
	}

	/**
	 * Get announcements visible to guests. Use {@link #getAnnouncements(NamelessUser)} for non-guest announcements.
	 * @return list of current announcements
	 * @throws NamelessException if there is an error in the request
	 */
	@NotNull
	public List<@NotNull Announcement> getAnnouncements() throws NamelessException {
		final JsonObject response = this.requests.get("announcements");
		return getAnnouncements(response);
	}

	/**
	 * Get all announcements visible for the player with the specified uuid
	 *
	 * @param user player to get visible announcements for
	 * @return list of current announcements visible to the player
	 * @throws NamelessException if there is an error in the request
	 */
	@NotNull
	public List<@NotNull Announcement> getAnnouncements(@NotNull final NamelessUser user) throws NamelessException {
		final JsonObject response = this.requests.get("users/" + user.getId() + "/announcements");

		return getAnnouncements(response);
	}

	@NotNull
	private static Set<@NotNull String> toStringSet(@NotNull final JsonArray jsonArray) {
		return StreamSupport.stream(jsonArray.spliterator(), false).map(JsonElement::getAsString).collect(Collectors.toSet());
	}

	@NotNull
	private List<@NotNull Announcement> getAnnouncements(@NotNull final JsonObject response) {
		return StreamSupport.stream(response.getAsJsonArray("announcements").spliterator(), false)
{'fraction_non_alphanumeric': 0.10837619397501837, 'fraction_numerical': 0.0007347538574577516, 'mean_word_length': 5.180476730987515, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1450297', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1561, 'n_words': 422}