DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Delicious AS3 API

05.14.2009
| 2670 views |
  • submit to reddit
        // description of your code here

package com.delicious.api
{
	import flash.events.*;
	import flash.net.*;
	import flash.xml.*;
	import com.dynamicflash.util.Base64;
	
	import com.delicious.api.events.*;

	public class Delicious extends EventDispatcher
	{
		
		private static const BOOKMARK_URL:String = "bookmarkURL";
		private static const LOGIN:String = "kalisurfer";
		private static const APIKEY:String = "xxxxxxx";
		private static const DEL_BOOKMARKADD_URL:String = 
			"https://api.del.icio.us/v1/posts/add?";
		
		// internal variables
		private var loaders:Array;
		
		// for auth
		private var authorizationHeader:URLRequestHeader;
		private var useHttps:Boolean = false;
		
		// CONSTRUCTOR
		public function Delicious(target:IEventDispatcher=null)
		{
			super(target);
			loaders = [];
			this.addLoader(BOOKMARK_URL, bookmark_urlHandler);
			//setAuthenticationCredentials(LOGIN, APIKEY);
		}
		
		
		// HEAVY LIFTING
		public function bookmarkURL(url:String, description:String = "", tags:String =""):void
		{
			
			trace("coming in with "+ url);
			var urlLoader:URLLoader = this.getLoader(BOOKMARK_URL);
			var newUrl:String = url;
			var variables:URLVariables = new URLVariables();
			var urlStr:String = DEL_BOOKMARKADD_URL;
			urlStr = urlStr + "url=" + escape(newUrl);
			if (description == "") {
				urlStr = urlStr + "&description=" + escape("From Twubbles"); 
			} else {
				urlStr = urlStr + "&description=" + escape(description);
			}
			
			if (tags == "") {
				urlStr = urlStr + "&tags=" + escape("Twubbles"); 
			} else {
				urlStr = urlStr + "&tags=" + escape(tags);
			}
			
			urlStr = urlStr + "&username=" + LOGIN + "&password=" + APIKEY;
			
			trace("url = '" + newUrl + "'");
			trace(" urlSTR IS " + urlStr);
			urlLoader.load(deliciousRequest(urlStr));
		}
		
		// EVENT HANDLERS
		public function bookmark_urlHandler(evt:Event):void 
		{
			var xml:XML = new XML(this.getLoader(BOOKMARK_URL).data);
			trace("BACK FROM DELICIOUS WITH " + xml.toString());
			
			//for each(var xml:XML in xmlList) { trace("yeah yeah yeah " + xml.someNode); }
			var r:DeliciousEvent = new DeliciousEvent (DeliciousEvent.ON_ADD_RESULT);
			r.data = xml.toString();
			dispatchEvent (r);
		}
		
		
		// AUTH
		public function setAuthenticationCredentials(username:String, password:String):void 
		 {
		 	if (username != null && password != null){
		 		this.useHttps = true;
		 		var creds:String = username + ":" + password;
		 		var encodedCredentials:String = Base64.encode(creds);
		 		authorizationHeader = 
		 			new URLRequestHeader("Authorization", 
		 				"Basic " + encodedCredentials);
		 	}
		 }
		 
		 /*
		   HELPER FUNCTIONS
		 */
		 
		 private function errorHandler (errorEvent : IOErrorEvent) : void
		{
			trace("THERE WAS AN ERROR");
			trace (errorEvent.text);
			/*var t:TwitterEvent = new TwitterEvent(TwitterEvent.ON_ERROR);
			t.data = errorEvent.text;
			this.dispatchEvent(t);*/
		}
		
		private function addLoader(name:String, completeHandler:Function):void
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			this.loaders[name] = loader;
		}
		 
		 private function getLoader(name:String):URLLoader
		{
			return this.loaders[name] as URLLoader;
		}
		
		private function deliciousRequest (url : String):URLRequest
		{
			/*
			if (this.useHttps){
				url = url.replace("http://", "https://");
			}
			*/
			var r:URLRequest = new URLRequest (url);
			if (this.authorizationHeader){
				r.requestHeaders = [this.authorizationHeader];	
			}
			return r;
		}
		 
	}
}