-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
RestAction.java
176 lines (150 loc) · 4.82 KB
/
RestAction.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
/*
* Copyright 2015-2016 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 net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.exceptions.ErrorResponseException;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.requests.restaction.CompletedFuture;
import net.dv8tion.jda.core.requests.restaction.RequestFuture;
import net.dv8tion.jda.core.utils.SimpleLog;
import java.util.concurrent.*;
import java.util.function.Consumer;
public abstract class RestAction<T>
{
public static final SimpleLog LOG = SimpleLog.getLog("RestAction");
public static final Consumer DEFAULT_SUCCESS = o -> {};
public static final Consumer<Throwable> DEFAULT_FAILURE = t ->
{
if (LOG.getEffectiveLevel().getPriority() <= SimpleLog.Level.DEBUG.getPriority())
{
LOG.log(t);
}
else
{
LOG.fatal("RestAction queue returned failure: [" + t.getClass().getSimpleName() + "] " + t.getMessage());
}
};
protected final JDAImpl api;
protected final Route.CompiledRoute route;
protected Object data;
public RestAction(JDA api, Route.CompiledRoute route, Object data)
{
this.api = (JDAImpl) api;
this.route = route;
this.data = data != null ? data : "";
}
public JDA getJDA()
{
return api;
}
public void queue()
{
queue(null, null);
}
public void queue(Consumer<T> success)
{
queue(success, null);
}
public void queue(Consumer<T> success, Consumer<Throwable> failure)
{
finalizeData();
if (success == null)
success = DEFAULT_SUCCESS;
if (failure == null)
failure = DEFAULT_FAILURE;
api.getRequester().request(new Request<T>(this, success, failure, true));
}
public Future<T> submit()
{
return submit(true);
}
public Future<T> submit(boolean shouldQueue)
{
finalizeData();
return new RequestFuture<T>(this, shouldQueue);
}
public T complete()
{
try
{
return complete(true);
}
catch (RateLimitedException ignored)
{
//This is so beyond impossible, but on the off chance that the laws of nature are rewritten
// after the writing of this code, I'm placing this here.
//Better safe than sorry?
throw new RuntimeException(ignored);
}
}
public T complete(boolean shouldQueue) throws RateLimitedException
{
try
{
return submit(shouldQueue).get();
}
catch (Throwable e)
{
if (e instanceof ExecutionException)
{
Throwable t = e.getCause();
if (t instanceof RateLimitedException)
throw (RateLimitedException) t;
else if (t instanceof PermissionException)
throw (PermissionException) t;
else if (t instanceof ErrorResponseException)
throw (ErrorResponseException) t;
}
throw new RuntimeException(e);
}
}
@Deprecated
public T block() throws RateLimitedException
{
return complete(false);
}
protected void finalizeData() { }
protected abstract void handleResponse(Response response, Request request);
public static class EmptyRestAction<T> extends RestAction<T>
{
private final T returnObj;
public EmptyRestAction(T returnObj)
{
super(null, null, null);
this.returnObj = returnObj;
}
@Override
public void queue(Consumer<T> success, Consumer<Throwable> failure)
{
if (success != null)
success.accept(returnObj);
}
@Override
public Future<T> submit(boolean shouldQueue)
{
return new CompletedFuture<>(returnObj);
}
@Override
public T complete(boolean shouldQueue)
{
return returnObj;
}
@Override
protected void handleResponse(Response response, Request request) { }
}
}