-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Route.java
357 lines (305 loc) · 16.6 KB
/
Route.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.requests;
import com.mashape.unirest.http.HttpMethod;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.mashape.unirest.http.HttpMethod.*;
public class Route
{
public static class Self
{
public static final Route GET_SELF = new Route(GET, "users/@me");
public static final Route MODIFY_SELF = new Route(PATCH, "users/@me");
public static final Route GET_GUILDS = new Route(GET, "users/@me/guilds");
public static final Route LEAVE_GUILD = new Route(DELETE, "users/@me/guilds/{guild_id}");
public static final Route GET_PRIVATE_CHANNELS = new Route(GET, "users/@me/channels");
public static final Route CREATE_PRIVATE_CHANNEL = new Route(POST, "users/@me/channels");
public static final Route GATEWAY = new Route(GET, "gateway");
// Client-only
public static final Route USER_SETTINGS = new Route(GET, "users/@me/settings");
}
public static class Users
{
public static final Route GET_USER = new Route(GET, "users/{user_id}");
}
public static class Guilds
{
public static final Route GET_GUILD = new Route(GET, "guilds/{guild_id}", "guild_id");
public static final Route MODIFY_GUILD = new Route(PATCH, "guilds/{guild_id}", "guild_id");
public static final Route CREATE_CHANNEL = new Route(POST, "guilds/{guild_id}/channels", "guild_id");
public static final Route GET_CHANNELS = new Route(GET, "guilds/{guild_id}/channels", "guild_id");
public static final Route MODIFY_CHANNELS = new Route(PATCH, "guilds/{guild_id}/channels", "guild_id");
public static final Route MODIFY_ROLES = new Route(PATCH, "guilds/{guild_id}/roles", "guild_id");
public static final Route GET_BANS = new Route(GET, "guilds/{guild_id}/bans", "guild_id");
public static final Route BAN = new Route(PUT, "guilds/{guild_id}/bans/{user_id}", "guild_id");
public static final Route BAN_WITH_DELETE = new Route(PUT, "guilds/{guild_id}/bans/{user_id}?delete-message-days={}", "guild_id");
public static final Route UNBAN = new Route(DELETE, "guilds/{guild_id}/bans/{user_id}", "guild_id");
public static final Route KICK_MEMBER = new Route(DELETE, "guilds/{guild_id}/members/{user_id}", "guild_id");
public static final Route MODIFY_MEMBER = new Route(PATCH, "guilds/{guild_id}/members/{user_id}", "guild_id");
public static final Route MODIFY_SELF_NICK = new Route(PATCH, "guilds/{guild_id}/members/@me/nick", "guild_id");
public static final Route PRUNABLE_COUNT = new Route(GET, "guilds/{guild_id}/prune?days={}", "guild_id");
public static final Route PRUNE_MEMBERS = new Route(POST, "guilds/{guild_id}/prune?days={}", "guild_id");
public static final Route GET_WEBHOOKS = new Route(GET, "guilds/{guild_id}/webhooks", "guild_id");
//Client Only
public static final Route CREATE_GUILD = new Route(POST, "guilds");
public static final Route DELETE_GUILD = new Route(POST, "guilds/{guild_id}/delete");
}
public static class Emotes
{
// These are all client endpoints and thus don't need defined major parameters
public static final Route MODIFY_EMOTE = new Route(PATCH, "guilds/{guild_id}/emojis/{emote_id}");
public static final Route DELETE_EMOTE = new Route(DELETE, "guilds/{guild_id}/emojis/{emote_id}");
public static final Route CREATE_EMOTE = new Route(POST, "guilds/{guild_id}/emojis");
}
public static class Webhooks
{
public static final Route DELETE_WEBHOOK = new Route(DELETE, "webhooks/{webhook_id}");
public static final Route DELETE_TOKEN_WEBHOOK = new Route(DELETE, "webhooks/{webhook_id}/{token}");
public static final Route MODIFY_WEBHOOK = new Route(PATCH, "webhooks/{webhook_id}");
public static final Route MODIFY_TOKEN_WEBHOOK = new Route(PATCH, "webhooks/{webhook_id}/{token}");
}
public static class Roles
{
public static final Route GET_ROLES = new Route(GET, "guilds/{guild_id}/roles", "guild_id");
public static final Route CREATE_ROLE = new Route(POST, "guilds/{guild_id}/roles", "guild_id");
public static final Route GET_ROLE = new Route(GET, "guilds/{guild_id}/roles/{role_id}", "guild_id");
public static final Route MODIFY_ROLE = new Route(PATCH, "guilds/{guild_id}/roles/{role_id}", "guild_id");
public static final Route DELETE_ROLE = new Route(DELETE, "guilds/{guild_id}/roles/{role_id}", "guild_id");
}
public static class Channels
{
public static final Route DELETE_CHANNEL = new Route(DELETE, "channels/{channel_id}");
public static final Route MODIFY_CHANNEL = new Route(PATCH, "channels/{channel_id}", "channel_id");
public static final Route SEND_TYPING = new Route(POST, "channels/{channel_id}/typing", "channel_id");
public static final Route GET_PERMISSIONS = new Route(GET, "channels/{channel_id}/permissions", "channel_id");
public static final Route GET_PERM_OVERRIDE = new Route(GET, "channels/{channel_id}/permissions/{permoverride_id}", "channel_id");
public static final Route GET_WEBHOOKS = new Route(GET, "channels/{channel_id}/webhooks", "channel_id");
public static final Route CREATE_WEBHOOK = new Route(POST, "channels/{channel_id}/webhooks", "channel_id");
public static final Route CREATE_PERM_OVERRIDE = new Route(PUT, "channels/{channel_id}/permissions/{permoverride_id}", "channel_id");
public static final Route MODIFY_PERM_OVERRIDE = new Route(PUT, "channels/{channel_id}/permissions/{permoverride_id}", "channel_id");
public static final Route DELETE_PERM_OVERRIDE = new Route(DELETE, "channels/{channel_id}/permissions/{permoverride_id}", "channel_id");
}
public static class Messages
{
public static final Route SEND_MESSAGE = new Route(POST, "channels/{channel_id}/messages", "channel_id");
public static final Route EDIT_MESSAGE = new Route(PATCH, "channels/{channel_id}/messages/{message_id}", "channel_id");
public static final Route DELETE_MESSAGE = new Route(DELETE, "channels/{channel_id}/messages/{message_id}", "channel_id");
public static final Route GET_PINNED_MESSAGES = new Route(GET, "channels/{channel_id}/pins", "channel_id");
public static final Route ADD_PINNED_MESSAGE = new Route(PUT, "channels/{channel_id}/pins/{message_id}", "channel_id");
public static final Route REMOVE_PINNED_MESSAGE = new Route(DELETE, "channels/{channel_id}/pins/{message_id}", "channel_id");
public static final Route ADD_REACTION = new Route(PUT, "channels/{channel_id}/messages/{message_id}/reactions/{reaction_code}/@me", "channel_id");
public static final Route REMOVE_REACTION = new Route(DELETE, "channels/{channel_id}/messages/{message_id}/reactions/{reaction_code}/{user_id}", "channel_id");
public static final Route REMOVE_ALL_REACTIONS = new Route(DELETE, "channels/{channel_id}/messages/{message_id}/reactions", "channel_id");
public static final Route GET_REACTION_USERS = new Route(GET, "channels/{channel_id}/messages/{message_id}/reactions/{reaction_code}?limit={limit}", "channel_id");
public static final Route GET_MESSAGE_HISTORY = new Route(GET, "channels/{channel_id}/messages?limit={}", "channel_id");
public static final Route GET_MESSAGE_HISTORY_BEFORE = new Route(GET, "channels/{channel_id}/messages?limit={}&before={}", "channel_id");
public static final Route GET_MESSAGE_HISTORY_AFTER = new Route(GET, "channels/{channel_id}/messages?limit={}&after={}", "channel_id");
public static final Route GET_MESSAGE_HISTORY_AROUND = new Route(GET, "channels/{channel_id}/messages?limit={}&around={}", "channel_id");
//Bot only
public static final Route GET_MESSAGE = new Route(GET, "channels/{channel_id}/messages/{message_id}", "channel_id");
public static final Route DELETE_MESSAGES = new Route(POST, "channels/{channel_id}/messages/bulk-delete", "channel_id");
}
public static class Invites
{
public static final Route GET_INVITE = new Route(GET, "invites/{code}");
public static final Route DELETE_INVITE = new Route(DELETE, "invites/{code}");
public static final Route GET_GUILD_INVITES = new Route(GET, "guilds/{guild_id}/invites", "guild_id");
public static final Route GET_CHANNEL_INVITES = new Route(GET, "channels/{channel_id}/invites", "channel_id");
public static final Route CREATE_INVITE = new Route(POST, "channels/{channel_id}/invites", "channel_id");
//Client Only
public static final Route ACCEPT_INVITE = new Route(POST, "invites/{code}");
}
public static class Custom
{
public static final Route DELETE_ROUTE = new Route(DELETE, "{}");
public static final Route GET_ROUTE = new Route(GET, "{}");
public static final Route POST_ROUTE = new Route(POST, "{}");
public static final Route PUT_ROUTE = new Route(PUT, "{}");
public static final Route PATCH_ROUTE = new Route(PATCH, "{}");
}
private final String route;
private final String ratelimitRoute;
private final String compilableRoute;
private final int paramCount;
private final HttpMethod method;
private final List<Integer> majorParamIndexes = new ArrayList<>();
private Route(HttpMethod method, String route, String... majorParameters)
{
this.method = method;
this.route = route;
this.paramCount = StringUtils.countMatches(route, '{'); //All parameters start with {
if (paramCount != StringUtils.countMatches(route, '}'))
throw new IllegalArgumentException("An argument does not have both {}'s for route: " + method + " " + route);
//Create a String.format compilable route for parameter compiling.
compilableRoute = route.replaceAll("\\{.*?\\}","%s");
//If this route has major parameters that are unique markers for the ratelimit route, then we need to
// create a ratelimit compilable route. This goes through and replaces the parameters specified by majorParameters
// and records the parameter index so that when we compile it later we can select the proper parameters
// from the ones provided to make sure we inject in the proper indexes.
if (majorParameters.length != 0)
{
int paramIndex = 0;
String replaceRoute = route;
Pattern keyP = Pattern.compile("\\{(.*?)\\}");
Matcher keyM = keyP.matcher(route);
//Search the route for all parameters
while(keyM.find())
{
String param = keyM.group(1);
//Attempt to match the found parameter with any of our majorParameters
for (String majorParam : majorParameters)
{
//If the parameter is a major parameter, replace it with a string token and record its
// parameter index for later ratelimitRoute compiling.
if (param.equals(majorParam))
{
replaceRoute = replaceRoute.replace(keyM.group(0), "%s");
majorParamIndexes.add(paramIndex);
}
}
paramIndex++;
}
ratelimitRoute = replaceRoute;
}
else
ratelimitRoute = route;
}
public HttpMethod getMethod()
{
return method;
}
public String getRoute()
{
return route;
}
public String getRatelimitRoute()
{
return ratelimitRoute;
}
public String getCompilableRoute()
{
return compilableRoute;
}
public int getParamCount()
{
return paramCount;
}
public CompiledRoute compile(String... params)
{
if (params.length != paramCount)
{
throw new IllegalArgumentException("Error Compiling Route: [" + route + "], incorrect amount of parameters provided." +
"Expected: " + paramCount + ", Provided: " + params.length);
}
//Compile the route for interfacing with discord.
String compiledRoute = String.format(compilableRoute, (Object[]) params);
String compiledRatelimitRoute = ratelimitRoute;
//If this route has major parameters which help to uniquely distinguish it from others of this route type then
// compile it using the major parameter indexes we discovered in the constructor.
if (!majorParamIndexes.isEmpty())
{
String[] majorParams = new String[majorParamIndexes.size()];
for (int i = 0; i < majorParams.length; i++)
{
majorParams[i] = params[majorParamIndexes.get(i)];
}
compiledRatelimitRoute = String.format(compiledRatelimitRoute, (Object[]) majorParams);
}
return new CompiledRoute(this, compiledRatelimitRoute, compiledRoute);
}
@Override
public int hashCode()
{
return (route + method.toString()).hashCode();
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Route))
return false;
Route oRoute = (Route) o;
return method.equals(oRoute.method) && route.equals(oRoute.route);
}
@Override
public String toString()
{
return "Route(" + method + ": " + route + ")";
}
public class CompiledRoute
{
private final Route baseRoute;
private final String ratelimitRoute;
private final String compiledRoute;
private CompiledRoute(Route baseRoute, String ratelimitRoute, String compiledRoute)
{
this.baseRoute = baseRoute;
this.ratelimitRoute = ratelimitRoute;
this.compiledRoute = compiledRoute;
}
public String getRatelimitRoute()
{
return ratelimitRoute;
}
public String getCompiledRoute()
{
return compiledRoute;
}
public Route getBaseRoute()
{
return baseRoute;
}
public HttpMethod getMethod()
{
return baseRoute.method;
}
@Override
public int hashCode()
{
return (compiledRoute + method.toString()).hashCode();
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof CompiledRoute))
return false;
CompiledRoute oCompiled = (CompiledRoute) o;
return baseRoute.equals(oCompiled.getBaseRoute()) && compiledRoute.equals(oCompiled.compiledRoute);
}
@Override
public String toString()
{
return "CompiledRoute(" + method + ": " + compiledRoute + ")";
}
}
//TODO: Remove once all Routes have been implemented.
public static void main(String[] args)
{
System.out.println("Forcing static load to test for invalid {} parameters!");
Route r;
r = Self.CREATE_PRIVATE_CHANNEL;
r = Users.GET_USER;
r = Guilds.BAN;
r = Roles.CREATE_ROLE;
r = Channels.CREATE_PERM_OVERRIDE;
r = Messages.ADD_PINNED_MESSAGE;
r = Invites.ACCEPT_INVITE;
System.out.println("Done!");
}
}