All files / src/Services/Alti/Request AltiRequestREST.js

89.58% Statements 43/48
68% Branches 17/25
100% Functions 3/3
89.58% Lines 43/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                        7x 7x   7x       7x     7x           7x           7x 7x   2x 2x   4x 4x   1x             6x     1x                                                                                                 6x   6x 6x 3x 3x 3x     6x 6x     6x 6x 6x 6x     6x 6x     6x   6x                 6x     6x 3x 3x 3x         6x 6x 2x 2x 4x 4x 4x                
/* jshint multistr : true */
 
import Logger from "../../../Utils/LoggerByDefault";
import _ from "../../../Utils/MessagesResources";
import AltiElevationRequest from "./model/AltiElevationRequest";
import AltiProfilRequest from "./model/AltiProfilRequest";
 
/**
 * @classdesc
 * Classe de gestion des requêtes de type REST sur le service altimetrique.
 *
 * @todo Le service Alti n'implemente pas le POST !?
 * @constructor
 * @alias Gp.Services.Alti.Request.AltiRequestREST
 * @param {Object} options - options
 * @param {Object}   options.param.positions - tableau de coordonnées lon/lat
 * @param {String}   options.param.delimiter - '|'
 * @param {Boolean}  options.param.indent - false|true
 * @param {String}   options.param.crs - 'CRS:84'
 * @param {String}   options.param.sampling - 3
 * @param {String}   options.param.resource - 'rge_alti_corse2a_float32_zip_lamb93'
 * @param {Boolean}  options.param.zonly - false|true
 * @param {Boolean}  options.param.measures - false|true
 * @param {String}   options.param.format - "JSON|XML"
 * @param {String}   options.type - "Profil|Elevation"
 * @param {String}   options.method - GET|POST
 *
 * @example
 *      var options = {
 *           type : 'Profil', // Elevation
 *           method : 'GET',  // par defaut
 *           param : {
 *               positions : [
 *                   {lon:'1.11', lat:'1.11'},
 *                   {lon:'1.10', lat:'1.10'},
 *                   {lon:'1.12', lat:'1.12'}
 *               ],
 *               delimiter : ";",         // par defaut
 *               indent    : true,        // par defaut
 *               crs       : 'EPSG:4326', // par defaut
 *               format    : 'json',      // par defaut (only to POST)
 *               sampling  : 3 ,          // par defaut (only use by Profil)
 *               zonly     : false        // par defaut (only use by Elevation)
 *               measures  : false        // par defaut (only use by Elevation)
 *           }
 *      };
 *
 *      var result;
 *      try {
 *          var obj = new AltiRequestREST (options);
 *          if (! obj.processRequestString ()) {
 *              throw new Error("Request empty !?")
 *          }
 *          result = obj.requestString;
 *      } catch (e) {
 *          // exceptions...
 *      }
 * @private
 */
function AltiRequestREST (options) {
    this.logger = Logger.getLogger("AltiRequestREST");
    this.logger.trace("[Constructeur AltiRequestREST ()]");
 
    Iif (!(this instanceof AltiRequestREST)) {
        throw new TypeError("AltiRequestREST constructor cannot be called as a function.");
    }
 
    this.options = options || {};
 
    // existance des options
    Iif (!this.options) {
        throw new Error(_.getMessage("PARAM_EMPTY", "options"));
    }
 
    // type de requete : Altitude ou Profil
    // (param. à determiner en fonction des parametres d'entrée)
    Iif (!this.options.type) {
        throw new Error(_.getMessage("PARAM_EMPTY", "type (Elevation or Profil)"));
    }
 
    // construction du modele de requête
    // (test du type d'objet candidat)
    this.DataObject = null;
    switch (this.options.type) {
        case "Elevation":
            this.DataObject = new AltiElevationRequest(this.options.param);
            break;
        case "Profil":
            this.DataObject = new AltiProfilRequest(this.options.param);
            break;
        default:
            throw new Error(_.getMessage("PARAM_TYPE", "type (Elevation or Profil)"));
    }
 
    /**
     * methode.
     * Par defaut, "GET".
     */
    this.method = this.options.method || "GET";
}
 
AltiRequestREST.prototype = {
 
    /**
     * request
     * @type {String}
     */
    requestString : null,
 
    /**
     * Constructeur (alias)
     */
    constructor : AltiRequestREST,
 
    /**
     * Template de la requête.
     */
    template : {
        get : {
            // FIXME on retire le param 'delimiter' : &delimiter='__DELIMITER__'
            value : "lon=__LON__&lat=__LAT__&indent=__INDENT__&crs='__CRS__'&resource=__RESOURCE__&measures=__MEASURES__",
            input : {
                point : "&zonly=__ZONLY__",
                profil : "&sampling=__SAMPLING__"
            }
        },
        post : {
            // FIXME on retire le param 'delimiter' : delimiter='__DELIMITER__'\n\
            value : "lon=__LON__\n" +
                "lat=__LAT__\n" +
                "indent=__INDENT__\n" +
                "crs='__CRS__'\n" +
                "resource='__RESOURCE__'\n" +
                "measures='__MEASURES__'\n",
            input : {
                point : "zonly=__ZONLY__\n",
                profil : "sampling=__SAMPLING__"
            }
        }
    },
 
    /**
     * Construction de la requête.
     *
     * @example
     * // GET  out : lon=0.2367,2.1570&lat=48.0551,46.6077&delimiter=,&indent=true&zonly=true&crs='CRS:84'
     * // POST out : Not yet supported method POST !
     * @returns {String}
     */
    processRequestString : function () {
        this.logger.trace("AltiRequestREST::processRequestString ()");
 
        var template = "";
        if (this.method === "POST") {
            template = this.template.post.value;
        } else Eif (this.method === "GET") {
            template = this.template.get.value;
        }
 
        template = template.replace(/__LON__/g, this.DataObject.getLon());
        template = template.replace(/__LAT__/g, this.DataObject.getLat());
        // FIXME on retire le param 'delimiter'
        // template = template.replace(/__DELIMITER__/g, this.DataObject.delimiter);
        template = template.replace(/__INDENT__/g, this.DataObject.indent);
        template = template.replace(/__CRS__/g, this.DataObject.crs);
        template = template.replace(/__RESOURCE__/g, this.DataObject.resource);
        template = template.replace(/__MEASURES__/g, this.DataObject.measures);
 
        // ajout +
        template = template + this.__addDataInputs();
        this.logger.trace("traduction tmpl", template);
 
        // sauvegarde
        this.requestString = template;
 
        return this.requestString;
    },
 
    /**
     * Ajout de parametres spécifiques (ex. zonly)
     *
     * @returns {String}
     */
    __addDataInputs : function () {
        this.logger.trace("AltiRequestREST::addDataInput ()");
 
        var myTemplate;
        if (this.method === "POST") {
            myTemplate = this.template.post;
        } else Eif (this.method === "GET") {
            myTemplate = this.template.get;
        } else {
            throw new Error("No other HTTP method supported by the service !");
        }
 
        var tmpl = null;
        if (this.DataObject.CLASSNAME === "AltiElevationRequest") {
            tmpl = myTemplate.input.point;
            return tmpl.replace(/__ZONLY__/g, this.DataObject.zonly.toString());
        } else Eif (this.DataObject.CLASSNAME === "AltiProfilRequest") {
            tmpl = myTemplate.input.profil;
            return tmpl.replace(/__SAMPLING__/g, this.DataObject.sampling);
        } else {
            throw new Error("No other object supported than elevation or profil !?");
        }
    }
};
 
export default AltiRequestREST;