-
Notifications
You must be signed in to change notification settings - Fork 2
/
GJTransaction.hx
208 lines (139 loc) · 4.83 KB
/
GJTransaction.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
/*
* Copyright (c) 2019 Charles Griffiths
*/
package gjCloud;
import haxe.crypto.Sha1;
import haxe.Json;
import haxe.ds.StringMap;
import js.html.XMLHttpRequest;
class GJTransaction
{
static var gjurl:String = "https://api.gamejolt.com/api/game/v1_2";
public static var appID:String = null;
public static var appPrivateKey:String = null;
public static var userName:String = null;
public static var userAuth:String = null;
public static var userID:String = null;
public var requestSentTime:Float;
public var responseReceivedTime:Float;
public var response:Dynamic;
public var responseStatus:Int;
var fields:StringMap<String>;
var id:String;
var key:String;
var url:String;
public function new( dir:String )
{
requestSentTime = -1;
responseReceivedTime = -1;
response = null;
responseStatus = -1;
fields = new StringMap();
setAppId( appID, appPrivateKey );
url = null==dir?"":dir;
}
public function setAppId( id:String, key:String ):GJTransaction
{
this.id = id;
this.key = key;
return this;
}
public function add( batch:GJTransaction ):GJTransaction
{
return addArg( "requests[]", StringTools.urlEncode( addSignature( batch.url )));
}
public function addGameID():GJTransaction
{
return addArg( "game_id", id );
}
public function addUserName( ?name:String = null ):GJTransaction
{
if (null == name) name = userName;
return addArg( "username", name );
}
public function addUserID( ?id:String = null ):GJTransaction
{
if (null == id) id = userID;
return addArg( "user_id", id );
}
public function addUserToken( ?token:String = null ):GJTransaction
{
if (null == token) token = userAuth;
return addArg( "user_token", token );
}
public function addTableID( ?id:Int=null ):GJTransaction
{
if (null == id) return this;
return addArg( "table_id", Std.string( id ));
}
public function addLimit( limit:Int, def:Int, max:Int ):GJTransaction
{
if (null == limit || def == limit) return this;
if (limit > max) limit = max;
return addArg( "limit", Std.string( limit ));
}
public function addArg( name:String, value:String ):GJTransaction
{
url = addStringArg( url, name, value );
return this;
}
public function addArgs( names:Array<String>, values:Array<String> ):GJTransaction
{
url = addStringArgs( url, names, values );
return this;
}
public function addSignature( url:String, ?data:String=null ):String
{
if (null == data) data = "";
return addStringArg( url, "signature", Sha1.encode( url + data + key ));
}
public static function addStringArg( url:String, name:String, value:String ):String
{
if (null == value || "" == value || "null" == value) return url;
return url + (-1==url.indexOf("?") ? "?" : "&") + name + "=" + value;
}
public static function addStringArgs( url:String, names:Array<String>, values:Array<String> ):String
{
for (index in 0...names.length)
url = addStringArg( url, names[index], values[index] );
return url;
}
public function get( ?callbackData:()->Void=null, ?callbackError:()->Void=null ):GJTransaction
{
sendGet( addSignature( gjurl+url ), null, callbackData, callbackError );
return this;
}
public function sendGet( url:String, ?headers:StringMap<String>=null, ?callbackData:()->Void=null, ?callbackError:()->Void=null ):Void
{
requestSentTime = Date.now().getTime();
var req = new XMLHttpRequest( "" );
req.open( "GET", url, true );
if (null != headers)
for (key in headers.keys())
req.setRequestHeader( key, headers.get( key ));
req.onreadystatechange = function() { onResponse( req, callbackData, callbackError ); }
req.send();
}
public function sendPost( url:String, data:String, ?headers:StringMap<String>=null, callbackData:()->Void, callbackError:()->Void ):Void
{
requestSentTime = Date.now().getTime();
var req = new XMLHttpRequest( "" );
req.open( "POST", url, true );
if (null != headers)
for (key in headers.keys())
req.setRequestHeader( key, headers.get( key ));
req.onreadystatechange = function() { onResponse( req, callbackData, callbackError ); }
req.send( data );
}
function onResponse( request:XMLHttpRequest, callbackData:()->Void, callbackError:()->Void ):Void
{
if (4 != request.readyState) return;
responseReceivedTime = Date.now().getTime();
response = Json.parse( request.responseText );
responseStatus = request.status;
if (200 == request.status && null != callbackData)
callbackData();
if (200 != request.status && null != callbackError)
callbackError();
}
}