Created
June 11, 2015 10:16
-
-
Save freyr/0c965c91908768e40c95 to your computer and use it in GitHub Desktop.
GetResponse APIv2 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
/* | |
Implementation of sample scenario for Adobe AIR application using GetResponse API: | |
Aplication starts on simple button click. | |
Add new contact to campaign 'sample_marketing'. | |
Start his follow-up cycle and set custom field | |
'last_purchased_product' to 'netbook'. | |
Author: | |
Maciej Gorski, Przemyslaw Kowalewski | |
http://implix.com | |
http://dev.getresponse.com | |
*/ | |
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" | |
xmlns:s="library://ns.adobe.com/flex/spark" | |
xmlns:mx="library://ns.adobe.com/flex/mx"> | |
/* | |
JSON-RPC module is required | |
available in distribution via import method | |
*/ | |
<fx:Script> | |
<![CDATA[ | |
import com.adobe.serialization.json.JSON; | |
import mx.rpc.events.FaultEvent; | |
import mx.rpc.events.ResultEvent; | |
/* | |
your API key | |
available at http://www.getresponse.com/my_api_key.html | |
*/ | |
private static const API_KEY:String = 'ENTER_YOUR_API_KEY_HERE'; | |
/* | |
get CAMPAIGN_ID of 'sample_marketing' campaign | |
*/ | |
protected function sendButton_clickHandler(event:MouseEvent):void | |
{ | |
var params:Object = { | |
'jsonrpc' : '2.0', | |
'method' : 'get_campaigns', | |
'params' : [ | |
API_KEY, { | |
'name' : { 'EQUALS' : 'sample_marketing' } | |
}], | |
'id' : 1 | |
}; | |
var requestStrin:String = JSON.encode(params); | |
var loader:URLLoader = new URLLoader(); | |
configureListeners(loader); | |
/* | |
API 2.x URL | |
*/ | |
var request:URLRequest = new URLRequest("http://api2.getresponse.com"); | |
request.method = URLRequestMethod.POST; | |
request.data = requestStrin; | |
loader.addEventListener(Event.COMPLETE, _prepareAddContact); | |
try { | |
loader.load(request); | |
} catch (error:Error) { | |
trace("Unable to load requested document."); | |
} | |
} | |
private function _prepareAddContact(event:Event):void | |
{ | |
event.target.removeEventListener(Event.COMPLETE, _prepareAddContact); | |
var loader:URLLoader = URLLoader(event.target); | |
var responseObj:Object = JSON.decode(loader.data); | |
for(var x:String in responseObj.result) | |
{ | |
_addContact(x); | |
} | |
} | |
private function _addContact(CAMPAIGN_ID:String):void{ | |
var params:Object = { | |
'jsonrpc' : '2.0', | |
"method" : "add_contact", | |
"params" : [ | |
API_KEY, | |
{ | |
"campaign" : CAMPAIGN_ID, | |
"name" : "Sample Name", | |
"email" : "[email protected]", | |
"cycle_day" : "0", | |
"customs" : [ | |
{ | |
"name" : "last_purchased_product", | |
"content" : "netbook" | |
} | |
] | |
} | |
], | |
'id' : 2 | |
} | |
var requestStrin:String = JSON.encode(params); | |
var loader:URLLoader = new URLLoader(); | |
configureListeners(loader); | |
var request:URLRequest = new URLRequest("http://api2.getresponse.com"); | |
request.method = URLRequestMethod.POST; | |
request.data = requestStrin; | |
/* | |
loader.addEventListener(Event.COMPLETE, function(event:Event):void{ | |
trace("add_contact_complete"); | |
}); | |
loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, function(event:Event):void{ | |
trace("res status"); | |
}); | |
*/ | |
try { | |
loader.load(request); | |
} catch (error:Error) { | |
trace("Unable to load requested document."); | |
} | |
} | |
private function configureListeners(dispatcher:IEventDispatcher):void { | |
dispatcher.addEventListener(Event.COMPLETE, completeHandler); | |
/* | |
dispatcher.addEventListener(Event.OPEN, openHandler); | |
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); | |
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); | |
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); | |
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); | |
*/ | |
} | |
private function completeHandler(event:Event):void { | |
var loader:URLLoader = URLLoader(event.target); | |
trace("completeHandler: " + loader.data); | |
var responseObj:Object = JSON.decode(loader.data); | |
trace(responseObj); | |
} | |
/* | |
private function openHandler(event:Event):void { | |
trace("openHandler: " + event); | |
} | |
private function progressHandler(event:ProgressEvent):void { | |
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); | |
} | |
private function securityErrorHandler(event:SecurityErrorEvent):void { | |
trace("securityErrorHandler: " + event); | |
} | |
private function httpStatusHandler(event:HTTPStatusEvent):void { | |
trace("httpStatusHandler: " + event.status); | |
} | |
private function ioErrorHandler(event:IOErrorEvent):void { | |
trace("ioErrorHandler: " + event); | |
} | |
*/ | |
]]> | |
</fx:Script> | |
<fx:Declarations> | |
<!-- Place non-visual elements (e.g., services, value objects) here --> | |
</fx:Declarations> | |
/* | |
Button required to run the application. | |
*/ | |
<s:Button id="sendButton" click="sendButton_clickHandler(event)"/> | |
</s:WindowedApplication> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment