Golem.de API

Golem  alpha

Golem.de API > Golem > Request.js (source view)
Search:
 
Filters
/*
Copyright (c) 2009, Klass und Ihlenfeld Verlag GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Klass und Ihlenfeld Verlag GmbH nor the names of its
      contributors may be used to endorse or promote products derived from 
      this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

$Id: Request.js 1587 2009-03-26 14:46:09Z am $
*/

if(!Golem) {
    /**
     * Base namespace for all Golem functionality
     *
     * @namespace Golem
     * @module Golem
     */
    var Golem = {};
}


if(!Golem.Request) {

    /**
     * Base class for accessing the Golem web interface.
     *
     * <p>
     * Before you can access a web service via <i>doReqest()</i>
     * You must set your develeoper key via <i>Golem_Request.KEY</i>
     * </p>
     *
     * @class Request
     * @namespace Golem
     * @static
     */

    Golem.Request = {

        /**
         * Keeps the list of anonymous functions
         * as callback for the jsonp request.
         *
         * The standard implementation of jsonp requests
         * adds the callback function to the global window scope.
         *
         * But having them in our namespace looks a little
         * bit cleaner.
         *
         * @property fnList
         * @type {Array}
         * @protected
         */
        fnList    : {},

        /**
         * Use a counter to create unique
         * function names
         *
         * @property fnCounter
         * @type {Integer}
         * @protected
         */
        fnCounter : 0,

        /**
         * Store the reference to the documents head element
         *
         * @property elHead
         * @type {Object HTMLElement}
         * @protected
         */
        elHead      : document.getElementsByTagName('HEAD')[0],

        /**
         * Does the request. The request is done via JSONP.
         *
         * @param {String} url                 The URL of the web service
         *
         * @param {String} params              Additional GET-Parameters
         *
         * @param {Function} callbackSuccessFn The function to call, after an succesful
         *                                   request. The first argument of the function
         *                                   will be the data property of the result.
         *
         * @param {Function} callbackFailFn   The function to call after the request
         *                                   could not proccessed. The first argument
         *                                   will be the error code, the second the
         *                                   message.
         *
         * @param {Object} timeout            object with the properties <i>fn</i> and <i>duration</i>.
         *                                   If duration is set, the request will have a timeout
         *                                   limit. If the request could not be processed,
         *                                   until the timeout expire, the fn-function will
         *                                   be called.
         *
         * @return {Boolean}  <i>true</i>, if request could be successfully created -
         *                  <i>false</i>, if neither a developer key was provides nor the
         *                  documents head element was available.
         *
         * @method doRequest
         */
        doRequest : function(url, params, callbackSuccessFn, callbackFailFn, timeout) {

            if(Golem.Request.elHead && null != Golem.Request.KEY) {

                var head = Golem.Request.elHead;
                
                var handleTimeout = null;

                var elScript = document.createElement('SCRIPT');

                Golem.Request.fnCounter++;

                var fnName = 'jsonp'+ Golem.Request.fnCounter;

                Golem.Request.fnList[fnName] = function(success, data) {

                    if(null != handleTimeout) {
                        window.clearTimeout(handleTimeout);
                    }

                    if(true == success && callbackSuccessFn) {
                        callbackSuccessFn.call(this, data);
                    } else if(false == success && callbackFailFn) {
                        callbackFailFn.call(this, data.errorCode, data.errorMessage);
                    }

                    Golem.Request.fnList[fnName] = undefined;

                    try { delete Golem.Request.fnList[fnName]; } catch(e) {};
                    
                    if(false == Golem.Request.DEBUG) {
    					head.removeChild( elScript );
    				}

                }

                elScript.type = "text/javascript";

                var sParams = "";

                if("" != params) {
                    sParams = params + '&';
                }

                if(timeout && timeout.duration && 0 != timeout.duration) {
                    handleTimeout = window.setTimeout(
                        function() {
                            timeout.fn.call(this);
                        }, timeout.duration
                    );
                }

                elScript.src = url + '?' + sParams + 'g_jsonp=' + fnName + '&key=' + encodeURIComponent(Golem.Request.KEY);

                head.appendChild(elScript);

                return true;
            }
            
            return false;
        },

    };
    
    /**
     * Holds the developer access key - MUST BE SET BEFORE ANY REQUEST
     *
     * @property KEY
     * @type {String}
     */
    Golem.Request.KEY   = null;

    /**
     * By default, the JS scripts as result of the JSONP request
     * will be deleted after an successful request. To disable
     * this behavoir, set the property to <i>true</i>.
     *
     * @property DEBUG
     * @type {Boolean}
     * @default <i>false</i>
     */
    Golem.Request.DEBUG = false;

    /**
     * Indicates an internal data access problem.
     *
     * @property ERROR_DATABASE
     * @type {Integer}
     */
    Golem.Request.ERROR_DATABASE = 1;

    /**
     * Developer key was not provided with the request
     *
     * @property ERROR_KEYAUTH_NOT_PROVIDED
     * @type {Integer}
     */
    Golem.Request.ERROR_KEYAUTH_NOT_PROVIDED = 2;

    /**
     * Developer key does not looks like one
     *
     * @property ERROR_KEYAUTH_NOT_VALID
     * @type {Integer}
     */
     Golem.Request.ERROR_KEYAUTH_NOT_VALID = 3;

    /**
     * Developer Key authentication failed
     *
     * @property ERROR_KEYAUTH_FAILED
     * @type {Integer}
     */
    Golem.Request.ERROR_KEYAUTH_FAILED = 4;

    /**
     * Developer Key is suspended
     *
     * @property ERROR_KEYAUTH_NOT_ENABLED
     * @type {Integer}
     */
    Golem.Request.ERROR_KEYAUTH_NOT_ENABLED = 5;

    /**
     * Developer Key access limit exceeded
     *
     * @property ERROR_KEYAUTH_LIMIT_EXCEEDED
     * @type {Integer}
     */
    Golem.Request.ERROR_KEYAUTH_LIMIT_EXCEEDED = 6;

}

Copyright © 2010 Golem.de.