Actionscript - Login, Get Communities, Perform a Query, Logout

Actionscript - Login, Get Communities, Perform a Query, Logout
 <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
    minWidth="955"
    minHeight="600">

    <fx:Script>
        <![CDATA[
            import com.adobe.serialization.json.JSON;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.http.HTTPService;
            
            protected function button1_clickHandler( event:MouseEvent ):void
            {
                //LOGIN
                //set up url
                var username:String = "sterling_archer@ikanow.com";
                var hashedpassword:String = "WZRHGrsBESr8wYFZ9sx0tPURuZgG2lmzyvWpwXPKz8U%3D"; //don't forget to URLEncode your arguments
                var address:String = "http://infinite.ikanow.com/api/auth/login/" + username + "/" + hashedpassword;
                
                //send login request
                var httpService:HTTPService = new HTTPService();
                httpService.addEventListener( ResultEvent.RESULT, httpLoginResultHandler );
                httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                httpService.url = address;
                httpService.send();
            }
            
            protected function httpFaultHandler( event:FaultEvent ):void
            {
                Alert.show( "Http Request had an error: " + event.message );
            }
            
            protected function httpLoginResultHandler( event:ResultEvent ):void
            {
                //convert result json string to an as3 object using the as3corelib library
                //available at: https://github.com/mikechambers/as3corelib
                var jsonObject:Object = JSON.decode( event.result.toString(), true );
                
                if ( jsonObject.response.success == true )
                {
                    //successfully logged in, cookies are automatically stored in flash so no need to handle them
                    //send request for our person object
                    var httpService:HTTPService = new HTTPService();
                    httpService.addEventListener( ResultEvent.RESULT, httpPersonResultHandler );
                    httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                    httpService.url = "http://infinite.ikanow.com/api/social/person/get";
                    httpService.send();
                }
            }
            
            protected function httpPersonResultHandler( event:ResultEvent ):void
            {
                //convert result json string to an as3 object using the as3corelib library
                //available at: https://github.com/mikechambers/as3corelib
                var jsonObject:Object = JSON.decode( event.result.toString(), true );
                
                if ( jsonObject.response.success == true )
                {
                    //here we will grab the communities so we can send them in a query
                    var communities:Array = jsonObject.data.communities;
                    //now we need to turn the community ids into a comma deliminated string
                    //you do not need to send all communities, just the ones you want to query on
                    var communityString:String = communities[ 0 ]._id;
                    
                    for ( var i:int = 1; i < communities.length; i++ )
                    {
                        communityString += "," + communities[ i ]._id;
                    }
                    
                    //create a json object for the query, actionscript objects
                    //can be converted into json so we can just create an anonymous object
                    var queryData:Object = new Object();
                    queryData.qt = new Array();
                    var queryTerm:Object = new Object();
                    queryTerm[ "ftext" ] = "sterling archer";
                    queryData.qt.push( queryTerm );
                    
                    
                    //send request for query
                    var httpService:HTTPService = new HTTPService();
                    httpService.addEventListener( ResultEvent.RESULT, httpQueryResultHandler );
                    httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                    httpService.url = "http://infinite.ikanow.com/api/knowledge/document/query/" + communityString;
                    //to send a post we just put the data in the send handler
                    httpService.send( JSON.encode( queryData ) );
                }
            }
            
            protected function httpQueryResultHandler( event:ResultEvent ):void
            {
                //convert result json string to an as3 object using the as3corelib library
                //available at: https://github.com/mikechambers/as3corelib
                var jsonObject:Object = JSON.decode( event.result.toString(), true );
                
                if ( jsonObject.response.success == true )
                {
                    //now you can do something with the results of query
                    //say get every title?
                    var titles:String = "";
                    
                    for each ( var doc:Object in jsonObject.data )
                    {
                        titles += doc.title.toString() + "\n";
                    }
                    Alert.show( titles );
                    
                    //dont forget to logout
                    var httpService:HTTPService = new HTTPService();
                    httpService.url = "http://infinite.ikanow.com/api/auth/logout";
                    httpService.send();
                }
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            
        ]]>
    </fx:Script>
    <s:Button 
        label="Push Me"
        click="button1_clickHandler(event)" />
</s:Application>