Skip to content

Instantly share code, notes, and snippets.

@danielsedlacek
Created October 7, 2012 11:18
Show Gist options
  • Save danielsedlacek/3847915 to your computer and use it in GitHub Desktop.
Save danielsedlacek/3847915 to your computer and use it in GitHub Desktop.
package org.wikibudgets {
// Base64 class downlaoded from http://code.google.com/p/as3crypto/
import com.hurlant.util.Base64;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.system.Security;
/**
* @author Daniel Sedlacek
*/
public class KinveyTest extends Sprite {
private static const KINVEY_APP_NAME : String = "<APP NAME>";
private static const KINVEY_APP_SECRET : String = "<APP SECRET>";
private static const KINVEY_ENTRY_POINT : String = "https://baas.kinvey.com/appdata/";
private static const MIME_TYPE : String = "application/json";
private var loader : URLLoader;
private var request : URLRequest;
public function KinveyTest() {
Security.allowDomain("*");
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
request = new URLRequest(KINVEY_ENTRY_POINT + KINVEY_APP_NAME + "/");
request.method = URLRequestMethod.POST;
request.contentType = MIME_TYPE;
//get the Base64 class at http://code.google.com/p/as3crypto/
var auth : String = Base64.encode(KINVEY_APP_NAME + ":" + KINVEY_APP_SECRET);
//mind the extra space after 'Basic '
request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + auth));
//simulate the handshake call - http://docs.kinvey.com/rest-overview.html#handshake
request.data = '{"_httpmethod":"GET"}';
trace('request: ' + request.url);
try {
loader.load(request);
} catch (error : Error) {
trace('request failed: ' + error.message);
}
}
private function completeHandler(event : Event) : void {
trace('request complete: ' + loader.data); //request complete: {"version":"2.4.0","kinvey":"hello wikibudgets"}
}
private function errorHandler(e : Event) : void {
trace('request error: ' + e['text']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment