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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | 31x 31x 30x 30x 30x 30x 2x 1x 3x 3x 1x 3x 3x 8x 8x 8x 8x 8x 8x 3x 3x 3x 5x 5x 5x 8x 8x 8x 7x 1x 8x 8x 8x 8x 8x 8x 4x 4x 3x 4x 4x 4x 8x 8x 8x 6x 2x 6x 6x 5x 5x 3x 3x 3x 1x 2x 1x 3x 6x 2x 1x 2x 1x 2x 31x | "use strict";
const _ = require("lodash");
const Resource = require("./Resource");
/**
* Javascript class which implements FunctionImport funcionality
*
* @class FunctionImport
* @extends {Resource}
*/
class FunctionImport extends Resource {
/**
* Creates an instance of <code>FunctionImport</code>.
* @param {Agent} agent instance of the Agent class @see Agent.js
* @param {Object} functionImportProperties information about FunctionImport from Metadata
* @memberof FunctionImport
*/
constructor(agent, functionImportProperties) {
super(agent, {
_parameters: {},
});
Object.defineProperty(this, "meta", {
value: functionImportProperties,
writable: false,
});
// was exposed as api, should be obsolete now
let api = functionImportProperties.getLegacyApiObject();
Object.defineProperty(this, "properties", {
value: api,
writable: false,
});
}
/**
* Create function which directly call's function import without
* additional selection of the \"call\" method.
*
* @private
*
* @return {Function} function which directly send request to the
* FunctionImport
*
* @memberof FunctionImport
*/
createDirectCaller() {
return (...args) => {
return this.call.apply(this, args);
};
}
/**
* Call post/get method (base on the metadata) to create FunctionImport request
*
* @public
*
* @param {Object} [parameters] is object which contains key/values definiton
* of parameter names and values (see service metadata
* for parameter names). The parameter is not mandatory,
* because parameters could be defined by queryParameter
* or parameter method
*
* @return {Promise} promise which is resolved/rejected when request is done
*
* @memberof FunctionImport
*/
call(parameters) {
this.defaultRequest.parameters(parameters);
return this[this.httpMethod()]();
}
/**
* Gets parameter definition.
*
* @protected
* @param {string} parameterName name of the parameter
* @returns {object} parameter definition, containing at least 'type'
* @memberof FunctionImport
*/
getParameterDefinition(parameterName) {
return this.meta.getParameter(parameterName);
}
/**
* Determine method of the class used for the HTTP request
* for of the FunctionImport
*
* @private
*
* @returns {String} name of the method of the FunctionImport class
*
* @memberof FunctionImport
*/
httpMethod() {
const SUPPORTED_METHODS = {
GET: "get",
POST: "post",
};
return SUPPORTED_METHODS[this.meta.httpMethod] || "post";
}
/**
* Send HTTP POST request to the OData server with url which define FunctionImport call
*
* @private
*
* @return {Promise} promise which is done where request is finished
*
* @memberof FunctionImport
*/
post() {
let query = this.queryFromParameters();
let defaultBatch = this.agent.batchManager.defaultBatch;
let defaultChangeSet = this.agent.batchManager.defaultChangeSet;
let request = this.defaultRequest;
let path = `/${this.meta.name}?${query}`;
let callRequestPromise;
if (defaultBatch) {
this.header("Accept", "application/json");
callRequestPromise = this._handleBatchCall(() => {
return defaultBatch.post(
path,
request._headers,
undefined,
defaultChangeSet
);
}, defaultBatch);
} else {
request.header("Content-type", "application/json");
request.header("Accept", "application/json");
callRequestPromise = this.agent.post(path, request._headers, undefined);
}
this.reset();
return new Promise((resolve, reject) => {
callRequestPromise
.then((res) => {
this.normalizeResponse(res, request._isRaw).then(resolve);
})
.catch((err) => {
reject(new Error(err.message));
});
});
}
/**
* Send HTTP GET request to the OData server with url which define FunctionImport call
*
* @private
*
* @return {Promise} promise which is done where request is finished
*
* @memberof FunctionImport
*/
get() {
let query = this.queryFromParameters();
let defaultBatch = this.agent.batchManager.defaultBatch;
let defaultChangeSet = this.agent.batchManager.defaultChangeSet;
let request = this.defaultRequest;
let path = `/${this.meta.name}?${query}`;
let callRequestPromise;
if (defaultBatch) {
this.header("Accept", "application/json");
callRequestPromise = this._handleBatchCall(() => {
return defaultBatch.get(
path,
request._headers,
undefined,
defaultChangeSet
);
}, defaultBatch);
} else {
request.header("Content-type", "application/json");
request.header("Accept", "application/json");
callRequestPromise = this.agent.get(path, request._headers);
}
this.reset();
return new Promise((resolve, reject) => {
callRequestPromise
.then((res) => {
this.normalizeResponse(res, request._isRaw).then(resolve);
})
.catch((err) => {
reject(new Error(err.message));
});
});
}
/**
* Normalize response and returns raw response or object or array
*
* @param {IncomingMessage} rawResponse from HTTP client
* @param {Boolean} raw force to use raw response
*
* @returns {Object|Array} raw response object or object or results array
*
* @memberof FunctionImport
*/
normalizeResponse(rawResponse, raw) {
let promise = Promise.resolve(rawResponse);
let contentType;
if (!raw) {
contentType = rawResponse.headers.get("content-type");
if (_.isString(contentType) && contentType.match(/application\/json/)) {
promise = rawResponse.json().then((json) => {
let result = json;
if (_.isArray(_.get(json, this.agent._listResultPath))) {
result = _.get(json, this.agent._listResultPath);
} else if (_.has(json, this.agent._instanceResultPath)) {
result = _.get(json, this.agent._instanceResultPath);
}
return result;
});
}
}
return promise;
}
/**
* Take FunctionImport parameters from queryParamters
*
* @private
*
* @return {[String]} array witch URL query parameters
*
* @memberof FunctionImport
*/
queryFromParameters() {
let queryParameters = _.map(this.defaultRequest._query, (value, key) => {
return `${key}=${value}`;
});
let parameters = _.map(this.defaultRequest._parameters, (value, key) => {
return `${key}=${value}`;
});
return _.concat(queryParameters, parameters).join("&");
}
}
module.exports = FunctionImport;
|