aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
blob: 3e38fd2e51d1a4b3fa60dbb1a035e47af387c3b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;

public class HypixelApi {
	private final Gson gson = new Gson();
	private final ExecutorService es = Executors.newFixedThreadPool(3);

	public CompletableFuture<JsonObject> getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args) {
		return getApiAsync(generateApiUrl(apiKey, method, args));
	}

	public void getHypixelApiAsync(
		String apiKey,
		String method,
		HashMap<String, String> args,
		Consumer<JsonObject> consumer
	) {
		getHypixelApiAsync(apiKey, method, args, consumer, () -> {
		});
	}

	public void getHypixelApiAsync(
		String apiKey,
		String method,
		HashMap<String, String> args,
		Consumer<JsonObject> consumer,
		Runnable error
	) {
		getApiAsync(generateApiUrl(apiKey, method, args), consumer, error);
	}

	private String getMyApiURL() {
		return String.format("https://%s/", NotEnoughUpdates.INSTANCE.config.apiData.moulberryCodesApi);
	}

	public CompletableFuture<JsonObject> getApiAsync(String urlS) {
		CompletableFuture<JsonObject> result = new CompletableFuture<>();
		es.submit(() -> {
			try {
				result.complete(getApiSync(urlS));
			} catch (Exception e) {
				result.completeExceptionally(e);
			}
		});
		return result;
	}

	public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) {
		es.submit(() -> {
			try {
				consumer.accept(getApiSync(urlS));
			} catch (Exception e) {
				error.run();
			}
		});
	}

	public void getMyApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) {
		es.submit(() -> {
			try {
				consumer.accept(getApiSync(getMyApiURL() + urlS));
			} catch (Exception e) {
				if (NotEnoughUpdates.INSTANCE.config.hidden.dev) {
					e.printStackTrace();
				}
				error.run();
			}
		});
	}

	public void getMyApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) {
		es.submit(() -> {
			try {
				consumer.accept(getApiGZIPSync(getMyApiURL() + urlS));
			} catch (Exception e) {
				error.run();
			}
		});
	}

	public void getApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) {
		es.submit(() -> {
			try {
				consumer.accept(getApiGZIPSync(urlS));
			} catch (Exception e) {
				error.run();
			}
		});
	}

	public JsonObject getApiSync(String urlS) throws IOException {
		URL url = new URL(urlS);
		URLConnection connection = url.openConnection();
		connection.setConnectTimeout(10000);
		connection.setReadTimeout(10000);

		String response = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);

		JsonObject json = gson.fromJson(response, JsonObject.class);
		if (json == null) throw new ConnectException("Invalid JSON");
		return json;
	}

	public JsonObject getApiGZIPSync(String urlS) throws IOException {
		URL url = new URL(urlS);
		URLConnection connection = url.openConnection();
		connection.setConnectTimeout(10000);
		connection.setReadTimeout(10000);

		String response = IOUtils.toString(new GZIPInputStream(connection.getInputStream()), StandardCharsets.UTF_8);

		JsonObject json = gson.fromJson(response, JsonObject.class);
		return json;
	}

	public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) {
		if (apiKey != null)
			args.put("key", apiKey.trim());
		StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method);
		boolean first = true;
		for (Map.Entry<String, String> entry : args.entrySet()) {
			if (first) {
				url.append("?");
				first = false;
			} else {
				url.append("&");
			}
			try {
				url.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name())).append("=")
					 .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()));
			} catch (UnsupportedEncodingException e) {
			}
		}
		return url.toString();
	}
}