UNPKG

geoportal-access-lib

Version:

French Geoportal resources access library

140 lines (125 loc) 6.07 kB
/** * Interface de dialogue avec les webservices * * @module Protocols * @private * @alias Gp.Protocols */ import Helper from "../Utils/Helper"; import XHR from "./XHR"; import JSONP from "./JSONP"; var Protocol = { /** * Interface unique d"envoi d"une requête. * * @method send * @static * @param {Object} options - options generales * @param {String} options.url - url du service * @param {String} options.method - GET, POST, PUT, DELETE * @param {String} options.protocol - XHR | JSONP * @param {String} options.format - format de la reponse du service : json, xml ou null (brute)... * @param {String} options.wrap - encapsuler la reponse du service dans du JSON : true|false (true par defaut sur le protocole JSONP) * @param {String} options.callbackSuffix - suffixe de la fonction de callback (JSONP uniquement) (ex: si callbackSuffix="", la fonction s'appellera "callback") * @param {String} options.timeOut - 0 ms * @param {Boolean} options.nocache - true|false * @param {Object|String} options.data - content (post) ou param (get) * @param {Object|String} options.headers - (post) ex. referer * @param {Object|String} options.content - (post) ex. "application/json" * @param {String} options.scope - this (TODO) * @param {Function} options.onResponse - callback * @param {Function} options.onFailure - callback * @param {Function} options.onTimeOut - callback * @param {String} options.proxyUrl - (TODO) */ send : function (options) { // INFO // "output" - param est interne à la classe "Protocol" (parametrable via "wrap"), et à ajouter à l"url // ce param est independant du service car il est géré par le filtre LUA : // ex. json|xml (json par defaut). // Ce param. permet d"encapsuler du XML dans du JSON : // {http : {status:200, error:null},xml :"réponse du service"} // Utile pour les services qui ne repondent que du XML (ex. Geocodage) // // |-------------------------------------------------| // | \service | | | | // | output\ format| json | xml | remarques | // |--------\------|------|-----|--------------------| // | json | json | json| json/xml encapsulé | // | xml | json | xml | param inactif | // |-------------------------------------------------| // ex. le service demande une reponse native au "format" json et avec un "output" json. // on a donc une reponse json encapsulé dans un json : ce qu'on ne souhaite pas ! // dans ce cas on ne renseigne pas output=json // INFO // "wrap" - choix d"encapsuler ou non les reponses dans du JSON. // Par defaut, on encapsule uniquement les reponses sur le protocole JSONP (et qui sont en xml) ! // INFO // "callback" - param est interne à la classe "Protocol" (non parametrable), et à ajouter à l"url // ce param est independant du service car il est géré aussi par le filtre LUA : // ex. callback|null // Ce param. permet de renvoyer une reponse javascript : // callback ({http : {status:200, error:null},xml :"réponse du service"}) // Ce param. est non renseigné par defaut car pour du JSONP, on utilise le // le protocol JSONP, et ce dernier implemente déjà le callback ! // settings par defaut var settings = options || { method : "GET", // protocol : "JSONP", protocol : "XHR", timeOut : 0, format : null, wrap : true, nocache : true, output : "json", callback : null, callbackSuffix : null }; // on determine l'environnement d'execution : browser ou non ? // et on stoppe pour nodeJS... sur un protocole JSONP ! if (typeof window === "undefined" && options.protocol === "JSONP") { console.log("Value (s) for parameter (s) 'protocol=JSONP (instead use XHR)' not supported to NodeJS"); return; } if (options.protocol === "XHR" || options.format === "json") { settings.wrap = false; } else if (options.protocol === "JSONP" && options.format === "xml") { settings.wrap = true; } settings.callback = null; // FIXME non géré !? settings.output = settings.wrap ? "json" : null; // on encapsule les reponses dans un objet JSON if (settings.wrap) { var params = {}; params.output = settings.output; params.callback = settings.callback; delete params.callback; // FIXME non géré !? settings.url = Helper.normalyzeUrl(options.url, params); } // choix de l"implementation : // XHR ou JSONP switch (settings.protocol) { case "XHR": // on normalise l'url (gestion du cache) if (options.method === "GET" && options.nocache) { settings.url = Helper.normalyzeUrl(settings.url, { t : new Date().getTime() }); } // appel du service en XHR XHR.call(settings); break; case "JSONP": // on normalise l'url si les params. sont renseignés dans la string|object "data" if (settings.data) { settings.url = Helper.normalyzeUrl(settings.url, settings.data); } // appel du service en JSONP JSONP.call(settings); break; default: throw new Error("protocol not supported (XHR|JSONP) !"); } } }; export default Protocol;