All files / src/Services/ProcessIsoCurve/Request ProcessIsoCurveRequest.js

86.36% Statements 38/44
60% Branches 9/15
100% Functions 2/2
86.36% Lines 38/44

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                                                                                          2x 2x   2x         2x             2x             2x     1x                                                                                                   2x 2x   2x   1x     1x 1x   1x 10x 10x 9x   10x     1x     1x   1x   1x   1x   1x 1x 1x           1x   1x 1x         1x   1x   1x   1x     1x 1x           2x 2x   2x          
 
import Logger from "../../../Utils/LoggerByDefault";
import _ from "../../../Utils/MessagesResources";
import ProcessIsoCurveParam from "./model/ProcessIsoCurveParam";
 
/**
 * @classdesc
 * Classe de gestion des requêtes sur le service de calcul d'isoschrone/isodistance.
 *  Les requêtes peuvent être en mode GET ou POST,
 *  et le format de sorti est en JSON.
 *
 * @constructor
 * @alias Gp.Services.ProcessIsoCurve.Request.ProcessIsoCurveRequest
 * @param {Object} options - options
 *
 * @example
 * var options = {
 *      httpMethod : 'GET', // GET|POST
 *      // spécifique au service
 *      position : {
 *          x : 2.3242664298058053,
 *          y : 48.86118017324745
 *      },
 *      graph : "car",
 *      method : 'time',
 *      time : 1000, //distance : 200
 *      reverse : false,
 *      srs : 'EPSG:4326'
 *  };
 *
 * try {
 *
 *      var oIsoCurve = new ProcessIsoCurveRequest (options);
 *      if (!oIsoCurve.processRequestString ()) {
 *          // error
 *      }
 *
 *      var request = oIsoCurve.requestString;
 *
 * } catch (e) {
 *      // error
 * }
 * @private
 */
function ProcessIsoCurveRequest (options) {
    this.logger = Logger.getLogger("ProcessIsoCurveRequest");
    this.logger.trace("[Constructeur ProcessIsoCurveRequest ()]");
 
    Iif (!(this instanceof ProcessIsoCurveRequest)) {
        throw new TypeError("ProcessIsoCurveRequest constructor cannot be called as a function.");
    }
 
    // existance des options
    Iif (!options) {
        throw new Error(_.getMessage("PARAM_EMPTY", "options"));
    }
 
    /**
     * Liste des options
     */
    this.settings = options;
 
    /**
     * Mode HTPP.
     * Par defaut, "GET".
     * @type {String}
     */
    this.mode = this.settings.httpMethod || "GET";
}
 
ProcessIsoCurveRequest.prototype = {
 
    /**
     * @lends module:ProcessIsoCurveRequest#
     */
 
    /**
     * Requête
     * @type {String}
     */
    requestString : null,
 
    /**
     * Constructeur (alias)
     */
    constructor : ProcessIsoCurveRequest,
 
    /**
     * Construction de la requête.
     *
     * @example
     * // GET  out :
     *   //  (http://wxs.ign.fr/KEY/isochrone?)
     *   //  resource=&
     *   //  point=&
     *   //  costValue=&
     *   //  costType=&
     *   //  profile=&
     *   //  constraints=&
     *   //  direction=&
     *   //  crs=
     *
     * // POST out :
     * {
     *  resource: "bduni-idf-pgr",
     *  point: "2.337306,48.849319",
     *  costValue: 100,
     *  costType: "time",
     *  profile: "car",
     *  constraints: [{
     *      constraintType: "banned",
     *      key: "ways_type",
     *      operator: "=",
     *      value: "autoroute"
     *  }]
     * }
     *
     * @returns {String} request
     */
    processRequestString : function () {
        var request = "";
        var i = 0;
 
        switch (this.mode) {
            case "GET":
                this.logger.trace("Process GET Request");
 
                // Mapping des options avec le service de l'API REST
                var oParams = new ProcessIsoCurveParam(this.settings);
                var params = oParams.getParams();
 
                for (i = 0; i < params.length; i++) {
                    var o = params[i];
                    if (request) {
                        request += "&";
                    }
                    request += o.k + "=" + o.v;
                }
 
                break;
 
            case "POST":
                this.logger.trace("Process POST Request");
                // creation du JSON
                var postRequest = {};
 
                postRequest.resource = this.settings.resource;
 
                postRequest.point = this.settings.position.x + "," + this.settings.position.y;
 
                Eif (this.settings.method === "distance") {
                    postRequest.costType = "distance";
                    postRequest.costValue = this.settings.distance;
                } else {
                    postRequest.costType = "time";
                    postRequest.costValue = this.settings.time;
                }
 
                postRequest.profile = this.settings.graph;
 
                Eif (this.settings.reverse) {
                    postRequest.direction = "arrival";
                } else {
                    postRequest.direction = "departure";
                }
 
                postRequest.constraints = this.settings.constraints;
 
                postRequest.distanceUnit = this.settings.distanceUnit;
 
                postRequest.timeUnit = this.settings.timeUnit;
 
                postRequest.crs = this.settings.srs;
 
                // conversion en chaîne de caractères
                request = JSON.stringify(postRequest);
                break;
 
            default:
                this.logger.error("No other HTTP method supported by the service !");
        }
 
        this.logger.trace(request);
        this.requestString = request;
 
        return this.requestString;
    }
};
 
export default ProcessIsoCurveRequest;