-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
Requester.java
280 lines (242 loc) · 9.91 KB
/
Requester.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
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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 net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDAInfo;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.requests.ratelimit.BotRateLimiter;
import net.dv8tion.jda.core.requests.ratelimit.ClientRateLimiter;
import net.dv8tion.jda.core.utils.JDALogger;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.internal.http.HttpMethod;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
public class Requester
{
public static final Logger LOG = JDALogger.getLog(Requester.class);
public static final String DISCORD_API_PREFIX = String.format("https://discordapp.com/api/v%d/", JDAInfo.DISCORD_REST_VERSION);
public static final String USER_AGENT = "DiscordBot (" + JDAInfo.GITHUB + ", " + JDAInfo.VERSION + ")";
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
public static final RequestBody EMPTY_BODY = RequestBody.create(null, new byte[]{});
protected final JDAImpl api;
private final RateLimiter rateLimiter;
private final OkHttpClient httpClient;
private volatile boolean retryOnTimeout = false;
public Requester(JDA api)
{
this(api, api.getAccountType());
}
public Requester(JDA api, AccountType accountType)
{
if (accountType == null)
throw new NullPointerException("Provided accountType was null!");
this.api = (JDAImpl) api;
if (accountType == AccountType.BOT)
rateLimiter = new BotRateLimiter(this, 5);
else
rateLimiter = new ClientRateLimiter(this, 5);
this.httpClient = this.api.getHttpClientBuilder().build();
}
public JDAImpl getJDA()
{
return api;
}
public <T> void request(Request<T> apiRequest)
{
if (rateLimiter.isShutdown)
throw new IllegalStateException("The Requester has been shutdown! No new requests can be requested!");
if (apiRequest.shouldQueue())
rateLimiter.queueRequest(apiRequest);
else
execute(apiRequest, true);
}
public Long execute(Request<?> apiRequest)
{
return execute(apiRequest, false);
}
/**
* Used to execute a Request. Processes request related to provided bucket.
*
* @param apiRequest
* The API request that needs to be sent
* @param handleOnRateLimit
* Whether to forward rate-limits, false if rate limit handling should take over
*
* @return Non-null if the request was ratelimited. Returns a Long containing retry_after milliseconds until
* the request can be made again. This could either be for the Per-Route ratelimit or the Global ratelimit.
* <br>Check if globalCooldown is {@code null} to determine if it was Per-Route or Global.
*/
public Long execute(Request<?> apiRequest, boolean handleOnRateLimit)
{
return execute(apiRequest, false, handleOnRateLimit);
}
public Long execute(Request<?> apiRequest, boolean retried, boolean handleOnRatelimit)
{
Route.CompiledRoute route = apiRequest.getRoute();
Long retryAfter = rateLimiter.getRateLimit(route);
if (retryAfter != null)
{
if (handleOnRatelimit)
apiRequest.handleResponse(new Response(retryAfter, Collections.emptySet()));
return retryAfter;
}
okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
String url = DISCORD_API_PREFIX + route.getCompiledRoute();
builder.url(url);
String method = apiRequest.getRoute().getMethod().toString();
RequestBody body = apiRequest.getBody();
if (body == null && HttpMethod.requiresRequestBody(method))
body = EMPTY_BODY;
builder.method(method, body)
.header("user-agent", USER_AGENT)
.header("accept-encoding", "gzip");
//adding token to all requests to the discord api or cdn pages
//we can check for startsWith(DISCORD_API_PREFIX) because the cdn endpoints don't need any kind of authorization
if (url.startsWith(DISCORD_API_PREFIX) && api.getToken() != null)
builder.header("authorization", api.getToken());
// Apply custom headers like X-Audit-Log-Reason
// If customHeaders is null this does nothing
if (apiRequest.getHeaders() != null)
{
for (Entry<String, String> header : apiRequest.getHeaders().entrySet())
builder.addHeader(header.getKey(), header.getValue());
}
okhttp3.Request request = builder.build();
Set<String> rays = new LinkedHashSet<>();
okhttp3.Response[] responses = new okhttp3.Response[4];
// we have an array of all responses to later close them all at once
//the response below this comment is used as the first successful response from the server
okhttp3.Response firstSuccess = null;
try
{
int attempt = 0;
do
{
//If the request has been canceled via the Future, don't execute.
if (apiRequest.isCanceled())
return null;
Call call = httpClient.newCall(request);
firstSuccess = call.execute();
responses[attempt] = firstSuccess;
String cfRay = firstSuccess.header("CF-RAY");
if (cfRay != null)
rays.add(cfRay);
if (firstSuccess.code() < 500)
break; // break loop, got a successful response!
attempt++;
LOG.debug("Requesting {} -> {} returned status {}... retrying (attempt {})",
apiRequest.getRoute().getMethod(),
url, firstSuccess.code(), attempt);
try
{
Thread.sleep(50 * attempt);
}
catch (InterruptedException ignored) {}
}
while (attempt < 3 && firstSuccess.code() >= 500);
if (firstSuccess.code() >= 500)
{
//Epic failure from other end. Attempted 4 times.
return null;
}
retryAfter = rateLimiter.handleResponse(route, firstSuccess);
if (!rays.isEmpty())
LOG.debug("Received response with following cf-rays: {}", rays);
if (retryAfter == null)
apiRequest.handleResponse(new Response(firstSuccess, -1, rays));
else if (handleOnRatelimit)
apiRequest.handleResponse(new Response(firstSuccess, retryAfter, rays));
return retryAfter;
}
catch (SocketTimeoutException e)
{
if (retryOnTimeout && !retried)
return execute(apiRequest, true, handleOnRatelimit);
LOG.error("Requester timed out while executing a request", e);
apiRequest.handleResponse(new Response(firstSuccess, e, rays));
return null;
}
catch (Exception e)
{
LOG.error("There was an exception while executing a REST request", e); //This originally only printed on DEBUG in 2.x
apiRequest.handleResponse(new Response(firstSuccess, e, rays));
return null;
}
finally
{
for (okhttp3.Response r : responses)
{
if (r == null)
break;
r.close();
}
}
}
public OkHttpClient getHttpClient()
{
return this.httpClient;
}
public RateLimiter getRateLimiter()
{
return rateLimiter;
}
public void setRetryOnTimeout(boolean retryOnTimeout)
{
this.retryOnTimeout = retryOnTimeout;
}
public void shutdown(long time, TimeUnit unit)
{
rateLimiter.shutdown(time, unit);
}
public void shutdownNow()
{
rateLimiter.forceShutdown();
}
/**
* Retrieves an {@link java.io.InputStream InputStream} for the provided {@link okhttp3.Response Response}.
* <br>When the header for {@code content-encoding} is set with {@code gzip} this will wrap the body
* in a {@link java.util.zip.GZIPInputStream GZIPInputStream} which decodes the data.
*
* <p>This is used to make usage of encoded responses more user-friendly in various parts of JDA.
*
* @param response
* The not-null Response object
*
* @throws IOException
* If a GZIP format error has occurred or the compression method used is unsupported
*
* @return InputStream representing the body of this response
*/
public static InputStream getBody(okhttp3.Response response) throws IOException
{
String encoding = response.header("content-encoding", "");
if (encoding.equals("gzip"))
return new GZIPInputStream(response.body().byteStream());
return response.body().byteStream();
}
}