User:Avanidhar/WPAPIHandler.js

/** * Wikipedia API Handler * * A simple object for interacting with Wikipedia's API. */ function WPAPIHandler{ }

WPAPIHandler.requests = new Array; WPAPIHandler.timeoutSeconds = REQUESTTIMEOUT;

/** * Perform Request * * Uses a callback function to perform a request to the WP api. * * @param	params	An object with members reflecting the parameters to be used when calling the API * @param	func	The function to be called with the results of the request to the API * @param	args	A list of arguements to pass to the function along with the result of the request */ WPAPIHandler.performGET = function(params, successFun, args, errorFun){ paramString = "?foo=lol" for(key in params){ paramString += "&" + key + "=" + escape(params[key]) }	url = "http://en.wikipedia.org/w/api.php" + paramString request = this.createRequest; if(!request){ errorFun("Your browser does not support the ability to make requests to the Wikipedia database."); return; }	request.open("GET", url, true); request.setRequestHeader("Pragma", "cache=yes"); request.setRequestHeader("Cache-Control", "no-transform"); request.successFun = successFun request.errorFun = errorFun request.args = args request.onreadystatechange = function { if (this.readyState != 4) return; if (this.status != 200){ this.errorFun("The Wikipedia server responded with an error: " + this.status + " " + this.statusText + ": " + this.responseText); return; }		clearTimeout(this.timeoutRef); this.successFun(this.responseText, this.args); return; }	request.send(null) currentRequestId = this.requests.length this.requests[currentRequestId] = request request.timeoutRef = setTimeout("WPAPIHandler.timeout(" + currentRequestId + ")", this.timeoutSeconds) }

WPAPIHandler.timeout = function(requestId){ request = this.requests[requestId]; request.errorFun("Request to Wikipedia database timed out."); try{ request.abort; }catch(e){ //then don't abort I guess } }

/** * Perform Request * * Uses a callback function to perform a request to the WP api. * * @param	params	An object with members reflecting the parameters to be used when calling the API * @param	func	The function to be called with the results of the request to the API * @param	args	A list of arguements to pass to the function along with the result of the request */ WPAPIHandler.performPOST = function(params, successFun, args, errorFun){ paramString = "foo=lol" for(key in params){ paramString += "&" + key + "=" + this.escape(params[key]) }	url = "http://en.wikipedia.org/w/api.php" request = this.createRequest; if(!request){ errorFun("Your browser does not support the ability to make requests to the Wikipedia database."); return; }	request.open("POST", url, true); request.setRequestHeader("Pragma", "cache=yes"); request.setRequestHeader("Cache-Control", "no-transform"); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.setRequestHeader("Content-length", paramString.length); request.setRequestHeader("Connection", "close"); request.successFun = successFun request.errorFun = errorFun request.args = args request.onreadystatechange = function { if (this.readyState != 4) return; if (this.status != 200){ this.errorFun("The Wikipedia server responded with an error: " + this.status + " " + this.statusText + ": " + this.responseText); return; }		clearTimeout(this.timeoutRef); this.successFun(this.responseText, this.args); return; }	request.send(paramString) currentRequestId = this.requests.length this.requests[currentRequestId] = request request.timeoutRef = setTimeout("WPAPIHandler.timeout(" + currentRequestId + ")", this.timeoutSeconds) } /** * Create Request * * Generates an appropriate XMLHttpRequest object for the browser being * used or raises an exception. */ WPAPIHandler.createRequest = function { var requestObject; try { requestObject = new XMLHttpRequest; } catch (e) { try { requestObject=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { requestObject=new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) { requestObject=null; }		}	}	return requestObject; } WPAPIHandler.escape = function(string){ resultString = escape(string); return resultString.replace(/\+/, "%2B") }