forked from HypixelDev/PublicAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypixelAPI.java
More file actions
292 lines (244 loc) · 9.9 KB
/
Copy pathHypixelAPI.java
File metadata and controls
292 lines (244 loc) · 9.9 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package net.hypixel.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import net.hypixel.api.adapters.BoostersTypeAdapterFactory;
import net.hypixel.api.adapters.DateTimeTypeAdapter;
import net.hypixel.api.adapters.GameTypeTypeAdapter;
import net.hypixel.api.adapters.UUIDTypeAdapter;
import net.hypixel.api.exceptions.APIThrottleException;
import net.hypixel.api.exceptions.HypixelAPIException;
import net.hypixel.api.reply.*;
import net.hypixel.api.reply.skyblock.ResourceReply;
import net.hypixel.api.reply.skyblock.SkyBlockAuctionsReply;
import net.hypixel.api.reply.skyblock.SkyBlockNewsReply;
import net.hypixel.api.reply.skyblock.SkyBlockProfileReply;
import net.hypixel.api.util.GameType;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.time.ZonedDateTime;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HypixelAPI {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.registerTypeAdapter(GameType.class, new GameTypeTypeAdapter())
.registerTypeAdapter(ZonedDateTime.class, new DateTimeTypeAdapter())
.registerTypeAdapterFactory(new BoostersTypeAdapterFactory<>(BoostersReply.Booster.class))
.create();
private static final String BASE_URL = "https://api.hypixel.net/";
private final UUID apiKey;
private final ExecutorService executorService;
private final HttpClient httpClient;
public HypixelAPI(UUID apiKey) {
this.apiKey = apiKey;
this.executorService = Executors.newCachedThreadPool();
this.httpClient = HttpClientBuilder.create().build();
}
/**
* Shuts down the internal executor service
*/
public void shutdown() {
executorService.shutdown();
}
/**
* @return currently set API key
*/
public UUID getApiKey() {
return apiKey;
}
public CompletableFuture<BoostersReply> getBoosters() {
return get(BoostersReply.class, "boosters");
}
public CompletableFuture<LeaderboardsReply> getLeaderboards() {
return get(LeaderboardsReply.class, "leaderboards");
}
public CompletableFuture<WatchdogStatsReply> getWatchdogStats() {
return get(WatchdogStatsReply.class, "watchdogStats");
}
/**
* This is now included inside {@link HypixelAPI#getGameCounts()}
*/
@Deprecated
public CompletableFuture<PlayerCountReply> getPlayerCount() {
return get(PlayerCountReply.class, "playerCount");
}
/**
* Session endpoint is bound to be removed at some point,
* data is mainly internal and highly inaccurate for online checking
*/
@Deprecated
public CompletableFuture<SessionReply> getSessionByUuid(UUID player) {
return get(SessionReply.class, "session", "uuid", player);
}
/**
* Session endpoint is bound to be removed at some point,
* data is mainly internal and highly inaccurate for online checking
*/
@Deprecated
public CompletableFuture<SessionReply> getSessionByUuid(String player) {
return get(SessionReply.class, "session", "uuid", player);
}
public CompletableFuture<PlayerReply> getPlayerByUuid(UUID player) {
return get(PlayerReply.class, "player", "uuid", player);
}
/**
* @param player uuid of a player in string format, can be both dashed or undashed.
* @return the future
*/
public CompletableFuture<PlayerReply> getPlayerByUuid(String player) {
return get(PlayerReply.class, "player", "uuid", player);
}
@Deprecated
public CompletableFuture<PlayerReply> getPlayerByName(String player) {
return get(PlayerReply.class, "player", "name", player);
}
public CompletableFuture<FriendsReply> getFriends(UUID player) {
return get(FriendsReply.class, "friends", "uuid", player);
}
/**
* @param player uuid of a player in string format, can be both dashed or undashed.
* @return the future
*/
public CompletableFuture<FriendsReply> getFriends(String player) {
return get(FriendsReply.class, "friends", "uuid", player);
}
public CompletableFuture<GuildReply> getGuildByPlayer(UUID player) {
return get(GuildReply.class, "guild", "player", player);
}
/**
* @param player uuid of a player in string format, can be both dashed or undashed.
* @return the future
*/
public CompletableFuture<GuildReply> getGuildByPlayer(String player) {
return get(GuildReply.class, "guild", "player", player);
}
public CompletableFuture<GuildReply> getGuildByName(String name) {
return get(GuildReply.class, "guild", "name", name);
}
/**
* @param id mongo id hex string
* @return the future
*/
public CompletableFuture<GuildReply> getGuildById(String id) {
return get(GuildReply.class, "guild", "id", id);
}
/**
* You can directly get the guild using {@link HypixelAPI#getGuildByPlayer(UUID)}
*/
@Deprecated
public CompletableFuture<FindGuildReply> findGuildByPlayer(UUID player) {
return get(FindGuildReply.class, "findGuild", "byUuid", player);
}
/**
* You can directly get the guild using {@link HypixelAPI#getGuildByPlayer(String)}
*/
@Deprecated
public CompletableFuture<FindGuildReply> findGuildByPlayer(String player) {
return get(FindGuildReply.class, "findGuild", "byUuid", player);
}
/**
* You can directly get the guild using {@link HypixelAPI#getGuildByName(String)})}
*/
@Deprecated
public CompletableFuture<GuildReply> findGuildByName(String name) {
return get(GuildReply.class, "findGuild", "byName", name);
}
public CompletableFuture<KeyReply> getKey() {
return get(KeyReply.class, "key");
}
public CompletableFuture<GameCountsReply> getGameCounts() {
return get(GameCountsReply.class, "gameCounts");
}
public CompletableFuture<SkyBlockProfileReply> getSkyBlockProfile(String profile) {
return get(SkyBlockProfileReply.class, "skyblock/profile", "profile", profile);
}
public CompletableFuture<SkyBlockNewsReply> getSkyBlockNews() {
return get(SkyBlockNewsReply.class, "skyblock/news");
}
public CompletableFuture<SkyBlockAuctionsReply> getSkyBlockAuctions(int page) {
return get(SkyBlockAuctionsReply.class, "skyblock/auctions", "page", page);
}
public CompletableFuture<ResourceReply> getResource(String resource) {
return requestResource(resource);
}
/**
* Execute Request asynchronously, executes Callback when finished
*
* @param request Request to get
*/
// TODO use a map of string to object?
private <R extends AbstractReply> CompletableFuture<R> get(Class<R> clazz, String request, Object... params) {
CompletableFuture<R> future = new CompletableFuture<>();
try {
if (params.length % 2 != 0)
throw new IllegalArgumentException("Need both key and value for parameters");
StringBuilder url = new StringBuilder(BASE_URL);
url.append(request);
url.append("?key=").append(apiKey);
for (int i = 0; i < params.length - 1; i += 2) {
url.append("&").append(params[i]).append("=").append(params[i + 1]);
}
executorService.submit(() -> {
try {
R response = httpClient.execute(new HttpGet(url.toString()), obj -> {
String content = EntityUtils.toString(obj.getEntity(), "UTF-8");
if (clazz == ResourceReply.class) {
return (R) new ResourceReply(GSON.fromJson(content, JsonObject.class));
} else {
return GSON.fromJson(content, clazz);
}
});
checkReply(response);
future.complete(response);
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
} catch (Throwable throwable) {
future.completeExceptionally(throwable);
}
return future;
}
private CompletableFuture<ResourceReply> requestResource(String resource) {
CompletableFuture<ResourceReply> future = new CompletableFuture<>();
try {
StringBuilder url = new StringBuilder(BASE_URL);
url.append("resources/").append(resource);
executorService.submit(() -> {
try {
ResourceReply response = httpClient.execute(new HttpGet(url.toString()), obj -> {
String content = EntityUtils.toString(obj.getEntity(), "UTF-8");
return new ResourceReply(GSON.fromJson(content, JsonObject.class));
});
checkReply(response);
future.complete(response);
} catch (Throwable t) {
future.completeExceptionally(t);
}
});
} catch (Throwable throwable) {
future.completeExceptionally(throwable);
}
return future;
}
/**
* Checks reply and throws appropriate exceptions based on it's content
*
* @param reply The reply to check
* @param <T> The class of the reply
*/
private <T extends AbstractReply> void checkReply(T reply) {
if (reply != null) {
if (reply.isThrottle()) {
throw new APIThrottleException();
} else if (!reply.isSuccess()) {
throw new HypixelAPIException(reply.getCause());
}
}
}
}