Skip to content

Instantly share code, notes, and snippets.

@anissen
Created August 19, 2016 22:24
Show Gist options
  • Save anissen/e33e15d92d886e3ef8c856b1b1b3000c to your computer and use it in GitHub Desktop.
Save anissen/e33e15d92d886e3ef8c856b1b1b3000c to your computer and use it in GitHub Desktop.
Testing GameAnalytics REST API integration from Haxe
import haxe.crypto.Base64;
import haxe.crypto.Hmac;
import haxe.io.Bytes;
class Analytics {
var game_key :String;
var secret_key :String;
public function new(game_key :String, secret_key :String) {
this.game_key = game_key;
this.secret_key = secret_key;
}
public function init() {
var body = '{"platform": "ios", "sdk_version": "rest api v2", "os_version": "ios 8.2"}';
request(get_url('init'), body);
}
function get_url(endpoint :String) {
return 'http://sandbox-api.gameanalytics.com/v2/$game_key/$endpoint';
}
function request(url :String, body :String) {
var http = new haxe.Http(url);
http.onData = function(data :String) {
var json = haxe.Json.parse(data);
trace('Result: ${data}');
if (json.enabled) trace('enabled!')
else trace('not enabled');
};
http.onStatus = function (status :Int) { trace('Status: $status'); };
http.onError = function (msg :String) { trace('Error: $msg'); };
http.addHeader('Content-Type', 'application/json');
http.addHeader('Authorization', secret_hash(body));
http.setPostData(body);
http.async = true;
http.request();
}
function secret_hash(body :String) :String {
return Base64.encode(new Hmac(SHA256).make(Bytes.ofString(secret_key), Bytes.ofString(body)));
}
}
class Test {
static function main() {
var analytics = new Analytics('5c6bcb5402204249437fb5a7a80a4959', '16813a12f718bc5c620f56944e1abc3ea13ccbac');
analytics.init();
}
}
/*
Trace:
Status: 200
Result: {"enabled":true,"server_ts":1471645267,"flags":[]}
enabled!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment