-
Notifications
You must be signed in to change notification settings - Fork 16
/
Call.hx
318 lines (229 loc) · 8.4 KB
/
Call.hx
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
package io.newgrounds;
import io.newgrounds.utils.Dispatcher;
import io.newgrounds.utils.AsyncHttp;
import io.newgrounds.objects.Error;
import io.newgrounds.objects.events.Result;
import io.newgrounds.objects.events.Response;
import io.newgrounds.objects.events.Outcome;
import haxe.ds.StringMap;
import haxe.Json;
/** A generic way to handle calls agnostic to their type */
interface ICallable {
public var component(default, null):String;
public function send():Void;
public function queue():Void;
public function destroy():Void;
}
class Call<T:BaseData>
implements ICallable {
public var component(default, null):String;
var _core:NGLite;
var _properties:StringMap<Dynamic>;
var _parameters:StringMap<Dynamic>;
var _requireSession:Bool;
var _isSecure:Bool;
// --- BASICALLY SIGNALS
var _responseHandlers:TypedDispatcher<Response<T>>;
var _successHandlers:Dispatcher;
var _httpErrorHandlers:TypedDispatcher<Error>;
var _statusHandlers:TypedDispatcher<Int>;
var _outcomeHandlers:TypedDispatcher<CallOutcome<T>>;
public function new (core:NGLite, component:String, requireSession:Bool = false, isSecure:Bool = false) {
_core = core;
this.component = component;
_requireSession = requireSession;
_isSecure = isSecure && core.encryptionHandler != null;
}
/** adds a property to the input's object. **/
public function addProperty(name:String, value:Dynamic):Call<T> {
if (_properties == null)
_properties = new StringMap<Dynamic>();
_properties.set(name, value);
return this;
}
/** adds a parameter to the call's component object. **/
public function addComponentParameter(name:String, value:Dynamic, defaultValue:Dynamic = null):Call<T> {
if (value == defaultValue)//TODO?: allow sending null value
return this;
if (_parameters == null)
_parameters = new StringMap<Dynamic>();
_parameters.set(name, value);
return this;
}
/**
* Handy callback setter for chained call modifiers. Unlike `addOutcomeHandler` this returns
* the entire server response, if one was successfuly received.
**/
public function addResponseHandler(handler:Response<T>->Void):Call<T> {
if (_responseHandlers == null)
_responseHandlers = new TypedDispatcher<Response<T>>();
_responseHandlers.add(handler);
return this;
}
/** Handy callback setter for chained call modifiers. Called when ng.io replies successfully */
public function addSuccessHandler(handler:Void->Void):Call<T> {
if (_successHandlers == null)
_successHandlers = new Dispatcher();
_successHandlers.add(handler);
return this;
}
/** Handy callback setter for chained call modifiers. Called when ng.io does not reply for any reason */
public function addErrorHandler(handler:Error->Void):Call<T> {
if (_httpErrorHandlers == null)
_httpErrorHandlers = new TypedDispatcher<Error>();
_httpErrorHandlers.add(handler);
return this;
}
/** Handy callback setter for chained call modifiers. No idea when this is called; */
public function addStatusHandler(handler:Int->Void):Call<T> {//TODO:learn what this is for
if (_statusHandlers == null)
_statusHandlers = new TypedDispatcher<Int>();
_statusHandlers.add(handler);
return this;
}
/**
* Handy callback setter for chained call modifiers. This callback is always called with
* The following Values:
* - `SUCCESS(data:T)` - The server responded with no errors, `data` is `response.result.data`.
* - `FAIL(HTTP(error:String))` - There was an error sending the request or receiving the result.
* - `FAIL(RESPONSE(error:Error))` - There was an error understanding the call.
* - `FAIL(RESULT(error:Error))` - There was an error executing the call.
**/
public function addOutcomeHandler(handler:(CallOutcome<T>)->Void):Call<T> {
if (_outcomeHandlers == null)
_outcomeHandlers = new TypedDispatcher<CallOutcome<T>>();
_outcomeHandlers.add(handler);
return this;
}
/** Handy callback setter for chained call modifiers. Called when ng.io does not reply for any reason */
public function safeAddOutcomeHandler(handler:Null<(CallOutcome<T>)->Void>):Call<T> {
if (handler != null)
addOutcomeHandler(handler);
return this;
}
/** Sends the call to the server, do not modify this object after calling this **/
inline public function send() sendHelper();
/**
* Sends the call but replaces the `app_id` property with the external app id.
* NOTE: This is NOT meant for call like `Medal.getList`, `ScoreBoard.getScores` or
* `CloudSave.loadSlot`, for those use the provided `app_id` parameter. This is just a handy
* helper to make calls to external apps that do not require a session id, like
* `ScoreBoard.getBoards`.
*
* @param id The id of the external app.
*/
inline public function sendExternalAppId(id:String) sendHelper(id);
function sendHelper(?externalAppId:String):Void {
final isExternal = externalAppId != null;
var data:Dynamic = {};
data.app_id = isExternal ? externalAppId : _core.appId;
data.call = {};
data.call.component = component;
if (_core.debug)
addProperty("debug", true);
if (_properties == null || !_properties.exists("session_id")) {
// --- HAS NO SESSION ID
if (_core.sessionId != null && isExternal == false) {
// --- AUTO ADD SESSION ID
addProperty("session_id", _core.sessionId);
} else if (_requireSession){
if (isExternal)
_core.logError(new Error('cannot send "$component" call to an external app'));
else
_core.logError(new Error('cannot send "$component" call without a sessionId'));
return;
}
}
if (_properties != null) {
for (field in _properties.keys())
Reflect.setField(data, field, _properties.get(field));
}
if (_parameters != null) {
data.call.parameters = {};
for (field in _parameters.keys())
Reflect.setField(data.call.parameters, field, _parameters.get(field));
}
_core.logVerbose('Post - ${Json.stringify(data)}');
if (_isSecure) {
if (isExternal) {
_core.logError(new Error('cannot send "$component" call to an external app that requires encryption'));
return;
}
var secureData = _core.encryptionHandler(Json.stringify(data.call));
data.call = {};
data.call.secure = secureData;
_core.logVerbose(' secure - $secureData');
}
_core.markCallPending(this);
AsyncNGCall.send(_core, Json.stringify(data), onData, onHttpError, onStatus);
}
/** Adds the call to the queue */
public function queue():Void {
_core.queueCall(this);
}
function onData(reply:String):Void {
_core.logVerbose('Reply - $reply');
if (_responseHandlers == null && _successHandlers == null && _outcomeHandlers == null)
return;
var response = new Response<T>(_core, reply);
if (_outcomeHandlers != null)
{
if (response.success == false)
_outcomeHandlers.dispatch(FAIL(RESPONSE(response.error)));
else if (response.result.success == false)
_outcomeHandlers.dispatch(FAIL(RESULT(response.result.error)));
else
_outcomeHandlers.dispatch(SUCCESS(response.result.data));
}
if (_responseHandlers != null)
_responseHandlers.dispatch(response);
if (response.success && response.result.success && _successHandlers != null)
_successHandlers.dispatch();
destroy();
}
function onHttpError(message:String):Void {
_core.logError(message);
if (_outcomeHandlers == null)
_outcomeHandlers.dispatch(FAIL(HTTP(message)));
if (_httpErrorHandlers == null)
_httpErrorHandlers.dispatch(new Error(message));
}
function onStatus(status:Int):Void {
if (_statusHandlers == null)
return;
_statusHandlers.dispatch(status);
}
public function destroy():Void {
_core = null;
_properties = null;
_parameters = null;
_responseHandlers = null;
_successHandlers = null;
_httpErrorHandlers = null;
_statusHandlers = null;
_outcomeHandlers = null;
}
}
typedef CallOutcome<T> = TypedOutcome<T, CallError>;
@:using(io.newgrounds.Call.CallErrorTools)
enum CallError
{
/** There was an error sending the request or receiving the result. */
HTTP(error:String);
/** There was an error understanding the call. */
RESPONSE(error:Error);
/** There was an error executing the call. */
RESULT(error:Error);
}
class CallErrorTools
{
static public function toString(error:CallError)
{
return switch (error)
{
case HTTP(e): 'Http Error: $e';
case RESPONSE(e): 'Response Error: $e';
case RESULT(e): 'Result Error: $e';
}
}
}