Actionscript - Login, Get a Source, Edit the Source, Test the new Source, Save the new Source, Logout

Actionscript - Login, Get a Source, Edit the Source, Test the new Source, Save the new Source, 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;
            
            private var source:Object;
            
            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 )
                {
                    //this is a random microsoft source
                    //you can get a source from config/source/good
                    var sourceID:String = "4ef498756a068f1eee17fac4";
                    
                    //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, httpSourceResultHandler );
                    httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                    httpService.url = "http://infinite.ikanow.com/api/config/source/get/" + sourceID;
                    httpService.send();
                }
            }
            
            protected function httpSourceResultHandler( 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 edit the source to try something new
                    source = jsonObject.data;
                    source.url = "http://rss.cnn.com/rss/cnn_topstories.rss";
                    source.title = "CNN top stories";
                    source.description = "CNNs top news stories around the world";
                    //lets removes some of the fields we dont need anymore
                    delete source._id;
                    delete source.created;
                    delete source.modified;
                    delete source.harvest;
                    delete source.shah256Hash;
                    delete source.ownerId;
                    delete source.key;
                    delete source.tags;
                    delete source.harvestBadSource;
                    
                    //now we will test our new source to see if the docs it brings back are what we want
                    var httpService:HTTPService = new HTTPService();
                    httpService.addEventListener( ResultEvent.RESULT, httpSourceTestHandler );
                    httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                    httpService.method = "POST";
                    httpService.url = "http://infinite.ikanow.com/api/config/source/test?numReturn=1&returnFullText=false";
                    //to send a post we just put the data in the send handler
                    //we are posting the modified source we created and set the content type
                    httpService.contentType = "application/json";
                    httpService.send( JSON.encode( source ) );
                }
            }
            
            protected function httpSourceSaveHandler( 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 )
                {
                    //dont forget to logout
                    var httpService:HTTPService = new HTTPService();
                    httpService.url = "http://infinite.ikanow.com/api/auth/logout";
                    httpService.send();
                }
            }
            
            protected function httpSourceTestHandler( 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 )
                {
                    //if we like how the source was harvest in jsonObject.data 
                    //we can save our source back to the server
                    //you can get community ids from person/get
                    var communityID:String = "4c927585d591d31d7b37097a";
                    var httpService:HTTPService = new HTTPService();
                    httpService.addEventListener( ResultEvent.RESULT, httpSourceSaveHandler );
                    httpService.addEventListener( FaultEvent.FAULT, httpFaultHandler );
                    httpService.method = "POST";
                    httpService.url = "http://infinite.ikanow.com/api/config/source/save/" + communityID;
                    //to send a post we just put the data in the send handler
                    //we are posting the modified source we created and set the content type
                    httpService.contentType = "application/json";
                    httpService.send( JSON.encode( source ) );
                }
            }
        ]]>
    </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>