Jump To …

JSTACK.Comm.js

/*
The MIT License

Copyright (c) 2012 Universidad Politecnica de Madrid

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


JSTACK Communication Module



This module provides functions to send GET, POST, PUT and DELETE HTTP requests to OpenStack components. It is compatible with the token-based authentication proposed by Keystone.


JSTACK.Comm = (function (JS, undefined) {
    "use strict";

    var send, get, post, put, del;


Private functions



Function _send is internally used to make detailed low-level requests to components.

    send = function (method, url, data, token, callBackOK, callbackError) {
        var xhr, body, result;


This function receives a method that can be "GET", "POST", "PUT", or "DELETE". It also receives the url to which it has to send the request, the data to be sent, that has to be a JSON object, the ´token´ to authenticate the request, and success and error callbacks.

        xhr = new XMLHttpRequest();

        xhr.open(method, url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.setRequestHeader("Accept", "application/json");

        xhr.onreadystatechange = function () {

            if (xhr.readyState === 4) {
                switch (xhr.status) {


In case of successful response it calls the callbackOK function.

                case 100:
                case 200:
                case 201:
                case 202:
                case 203:
                case 204:
                case 205:
                    result = undefined;
                    if (xhr.responseText !== undefined && xhr.responseText !== '') {
                        result = JSON.parse(xhr.responseText);
                    }
                    callBackOK(result);
                    break;


In case of error it sends an error message to callbackError.

                case 400:
                    callbackError("400 Bad Request");
                    break;
                case 401:
                    callbackError("401 Unauthorized");
                    break;
                case 403:
                    callbackError("403 Forbidden");
                    break;
                default:
                    callbackError(xhr.status + " Error");
                }
            }
        };

        if (token !== undefined) {
            xhr.setRequestHeader('X-Auth-Token', token);
        }

        if (data !== undefined) {
            body = JSON.stringify(data);
            xhr.send(body);
        } else {
            xhr.send();
        }
    };


Public functions



  • Function get receives the url, the authentication token (which is optional), and callbacks. It sends a HTTP GET request, so it does not send any data.
    get = function (url, token, callbackOK, callbackError) {
        send("GET", url, undefined, token, callbackOK, callbackError);
    };

  • Function post receives the url, the authentication token (which is optional), the data to be sent (a JSON Object), and callbacks. It sends a HTTP POST request.
    post = function (url, data, token, callbackOK, callbackError) {
        send("POST", url, data, token, callbackOK, callbackError);
    };

  • Function put receives the same parameters as post. It sends a HTTP PUT request.
    put = function (url, data, token, callbackOK, callbackError) {
        send("PUT", url, data, token, callbackOK, callbackError);
    };

  • Function del receives the same paramaters as get. It sends a HTTP DELETE request.
    del = function (url, token, callbackOK, callbackError) {
        send("DELETE", url, undefined, token, callbackOK, callbackError);
    };

Public Functions and Variables

This is the list of available public functions and variables

    return {


Functions:

        get : get,
        post : post,
        put : put,
        del : del
    };
}(JSTACK));