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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
// ================== DOUBLE INCLUDE ========================= //
#if defined _GlobalAPI_RequestData_included_
#endinput
#endif
#define _GlobalAPI_RequestData_included_
// =========================================================== //
#include <json>
// =========================================================== //
/*
Helper methodmap for wrapping data related to requests
*/
methodmap GlobalAPIRequestData < JSON_Object
{
/**
* Creates a new GlobalAPIRequestData
*
* @note You can pass a plugin handle or name and/or version
* @note Plugin handle is always preferred
* @param plugin Handle to calling plugin
* @param pluginName Name of the calling plugin
* @param pluginVersion Version of the calling plugin
* @return A new GlobalAPIRequestData handle
*/
public GlobalAPIRequestData(Handle plugin = null, char[] pluginName = "Unknown", char[] pluginVersion = "Unknown")
{
JSON_Object requestData = new JSON_Object();
if (plugin == null)
{
requestData.SetString("pluginName", pluginName);
requestData.SetString("pluginVersion", pluginVersion);
}
else
{
requestData.SetString("pluginName", GetPluginDisplayName(plugin));
requestData.SetString("pluginVersion", GetPluginVersion(plugin));
}
requestData.SetKeyHidden("pluginName", true);
requestData.SetKeyHidden("pluginVersion", true);
requestData.SetInt("acceptType", 0);
requestData.SetKeyHidden("acceptType", true);
requestData.SetInt("contentType", 0);
requestData.SetKeyHidden("contentType", true);
return view_as<GlobalAPIRequestData>(requestData);
}
/**
* Sets a key as default
*
* @note This sets them as "Handle" type
* @note - See GlobalAPI.inc for default values
* @param key Key to set as default
* @noreturn
*/
public void SetDefault(char[] key)
{
this.SetHandle(key);
this.SetKeyHidden(key, true);
}
/**
* Sets url to the request data
*
* @param url Url to set
* @noreturn
*/
public void AddUrl(char[] url)
{
this.SetString("url", url);
this.SetKeyHidden("url", true);
}
/**
* Sets endpoint to the request data
*
* @param endpoint Endpoint to set
* @noreturn
*/
public void AddEndpoint(char[] endpoint)
{
this.SetString("endpoint", endpoint);
this.SetKeyHidden("endpoint", true);
}
/**
* Sets body file path to the request data
*
* @note Path to file with data to be posted
* @param path Body file (path) to set
* @noreturn
*/
public void AddBodyFile(char[] path)
{
this.SetString("bodyFile", path);
this.SetKeyHidden("bodyFile", true);
}
/**
* Sets data file path to the request data
*
* @note Path for downloaded files
* @param path Data path to set
* @noreturn
*/
public void AddDataPath(char[] path)
{
this.SetString("dataFilePath", path);
this.SetKeyHidden("dataFilePath", true);
}
/*
Get or set the request's "acceptType"
*/
property int AcceptType
{
public get()
{
return this.GetInt("acceptType");
}
public set(int type)
{
this.SetInt("acceptType", type);
}
}
/*
Get or set the request's "contentType"
*/
property int ContentType
{
public get()
{
return this.GetInt("contentType");
}
public set(int type)
{
this.SetInt("contentType", type);
}
}
/*
Get or set the request's "keyRequired"
*/
property bool KeyRequired
{
public get()
{
return this.GetBool("keyRequired");
}
public set(bool required)
{
this.SetBool("keyRequired", required);
this.SetKeyHidden("keyRequired", true);
}
}
/*
Get or set the request's "isRetried"
*/
property bool IsRetried
{
public get()
{
return this.GetBool("isRetried");
}
public set(bool retried)
{
this.SetBool("isRetried", retried);
this.SetKeyHidden("isRetried", true);
}
}
/*
Get or set the request's "bodyLength"
*/
property int BodyLength
{
public get()
{
return this.GetInt("bodyLength");
}
public set(int length)
{
this.SetInt("bodyLength", length);
this.SetKeyHidden("bodyLength", true);
}
}
/*
Get or set the request's "status"
*/
property int Status
{
public get()
{
return this.GetInt("status");
}
public set(int status)
{
this.SetInt("status", status);
this.SetKeyHidden("status", true);
}
}
/*
Get or set the request's "responseTime"
*/
property int ResponseTime
{
public get()
{
return this.GetInt("responseTime");
}
public set(int responseTime)
{
this.SetInt("responseTime", responseTime);
this.SetKeyHidden("responseTime", true);
}
}
/*
Get or set the request's "requestType"
*/
property int RequestType
{
public get()
{
return this.GetInt("requestType");
}
public set(int type)
{
this.SetInt("requestType", type);
this.SetKeyHidden("requestType", true);
}
}
/*
Get or set the request's "failure"
*/
property bool Failure
{
public get()
{
return this.GetBool("failure");
}
public set(bool failure)
{
this.SetBool("failure", failure);
this.SetKeyHidden("failure", true);
}
}
/*
Get or set the request's "callback"
*/
property Handle Callback
{
public get()
{
return view_as<Handle>(this.GetInt("callback"));
}
public set(Handle hFwd)
{
this.SetHandle("callback", hFwd);
this.SetKeyType("callback", Type_Int);
this.SetKeyHidden("callback", true);
}
}
/*
Get or set the request's "data"
*/
property any Data
{
public get()
{
return this.GetInt("data");
}
public set(any data)
{
this.SetInt("data", data);
this.SetKeyHidden("data", true);
}
}
/**
* Adds a number to the request data
*
* @note Default values are added as "defaults"
* @note See GlobalAPI.inc for the default values
* @param key Key name to set
* @param value Value of the key
* @noreturn
*/
public void AddNum(char[] key, int value)
{
if (value == -1)
{
this.SetDefault(key);
}
else
{
this.SetInt(key, value);
}
}
/**
* Adds a float to the request data
*
* @note Default values are added as "defaults"
* @note See GlobalAPI.inc for the default values
* @param key Key name to set
* @param value Value of the key
* @noreturn
*/
public void AddFloat(char[] key, float value)
{
if (value == -1.000000)
{
this.SetDefault(key);
}
else
{
this.SetFloat(key, value);
}
}
/**
* Adds a string to the request data
*
* @note Default values are added as "defaults"
* @note See GlobalAPI.inc for the default values
* @param key Key name to set
* @param value Value of the key
* @noreturn
*/
public void AddString(char[] key, char[] value)
{
if (StrEqual(value, ""))
{
this.SetDefault(key);
}
else
{
this.SetString(key, value);
}
}
/**
* Adds a boolean to the request data
*
* @note Default values are added as "defaults"
* @note See GlobalAPI.inc for the default values
* @param key Key name to set
* @param value Value of the key
* @noreturn
*/
public void AddBool(char[] key, bool value)
{
if (value != true && value != false)
{
this.SetDefault(key);
}
else
{
this.SetBool(key, value);
}
}
/**
* Adds integer array to the request data
*
* @note Max length <= 0 are added as defaults
* @param key Key name to set
* @param value Values (array) of the key
* @param maxlength Max length of the values array
* @noreturn
*/
public void AddIntArray(char[] key, int[] value, int maxlength)
{
if (maxlength <= 0)
{
this.SetDefault(key);
}
else
{
JSON_Object hArray = new JSON_Object(true);
for (int i = 0; i < maxlength; i++)
{
hArray.PushInt(value[i]);
}
this.SetObject(key, hArray);
}
}
/**
* Adds string array to the request data
*
* @note Item count <= 0 are added as defaults
* @param key Key name to set
* @param itemCount Amount of strings in the array
* @noreturn
*/
public void AddStringArray(char[] key, char[][] value, int itemCount)
{
if (itemCount <= 0)
{
this.SetDefault(key);
}
else
{
JSON_Object hArray = new JSON_Object(true);
for (int i = 0; i < itemCount; i++)
{
hArray.PushString(value[i]);
}
this.SetObject(key, hArray);
}
}
/**
* Converts all of the request data into a query string representation
*
* @note This ignores "hidden" keys
* @param queryString Buffer to store the result in
* @param maxlength Max length of the buffer
* @noreturn
*/
public void ToString(char[] queryString, int maxlength)
{
StringMapSnapshot paramsMap = this.Snapshot();
char key[64];
char value[1024];
int paramCount = 0;
for (int i = 0; i < paramsMap.Length; i++)
{
paramsMap.GetKey(i, key, sizeof(key));
if (this.GetKeyHidden(key) || json_is_meta_key(key))
{
continue;
}
switch(this.GetKeyType(key))
{
case Type_String:
{
this.GetString(key, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
case Type_Float:
{
float temp = this.GetFloat(key);
FloatToString(temp, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
case Type_Int:
{
int temp = this.GetInt(key);
IntToString(temp, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
case Type_Bool:
{
bool temp = this.GetBool(key);
BoolToString(temp, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
case Type_Object:
{
JSON_Object hObject = this.GetObject(key);
if (!hObject.IsArray) continue;
for (int x = 0; x < hObject.Length; x++)
{
switch (hObject.GetKeyTypeIndexed(x))
{
case Type_Int:
{
int temp = hObject.GetIntIndexed(x);
IntToString(temp, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
case Type_String:
{
hObject.GetStringIndexed(x, value, sizeof(value));
AppendToQueryString(paramCount, queryString, maxlength, key, value);
}
}
}
}
}
}
delete paramsMap;
}
}
// =====[ PRIVATE ]=====
static void BoolToString(bool value, char[] buffer, int maxlength)
{
FormatEx(buffer, maxlength, "%s", value ? "true" : "false");
}
static void AppendToQueryString(int &index, char[] buffer, int maxlength, char[] key, char[] value)
{
if (index == 0)
{
index++;
Format(buffer, maxlength, "?%s=%s", key, value);
}
else
{
index++;
Format(buffer, maxlength, "%s&%s=%s", buffer, key, value);
}
}
|