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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 1x 1x 1x | /**
* IdealNet REST Request.
*
* Implements functions and logic to parse the REST request
* coming into IdealNet REST and translate that to a pubsub
* request.
*
* @author michael whobrey <mwhobrey@idealss.com>
*/
const { extractApiFromQuery, getPrivateKey} = require('./utils');
const INRClient = require('./inrClient');
/**
* IdealNet REST Request (INRRequest) class.
*
* Provides a way to process an incoming REST request
* in a PubSub manner and return the results to the
* caller.
*/
class INRRequest {
#path;
#restReq;
#token;
#signature;
#payload;
/**
* Create a request.
*
* @param path {string} the pubsub message path for the request
* @param restReq {Object} the REST request body to parse
* @param restReq.token {string} - the token of the client being used to make the request
* @param restReq.signature {string} - the signature of req.payload + private key
* @param restReq.group {Number} - the group number for the location
* @param restReq.siteId {Number} - the location id
* @param restReq.payload {Object} - the actual api body to be used to make the pubsub request (structure varies)
*/
constructor(path, restReq) {
this.#path = path;
this.#restReq = restReq;
this.#token = restReq.token;
this.#signature = restReq.signature;
this.#payload = restReq.payload;
this.publish = this.publish.bind(this);
this.pubSubIt = this.pubSubIt.bind(this);
}
/**
* Getter for path
*/
get path() {
return this.#path;
}
/**
* Getter for client.
*/
get restReq() {
return this.#restReq;
}
/**
* Getter for token.
*/
get token() {
return this.#token;
}
/**
* Getter for signature.
*/
get signature() {
return this.#signature;
}
/**
* Getter for payload.
*/
get payload() {
return this.#payload;
}
/**
* Publish to pubsub the data.
*
* @param client {INRClient} The client that has established the connection with pubsub.
*/
publish(client) {
client.publish(this.#path, this.#payload);
}
/**
* Send message via IdealNet PubSub and return the response.
*
* Connect to IdealNet PubSub as a proxy for the REST request,
* send the request and wait on the results to return as the
* results for the REST request.
*
* @param dbClient {MongoClient} Database connection client.
*
* @return {Promise} Returns a response from pubsub once received or fails
* with an error.
*/
pubSubIt = async (dbClient) => {
const key = await getPrivateKey(this.#token, dbClient);
const client = new INRClient(extractApiFromQuery(this.#path), this.#restReq.group, this.#restReq.siteId, this.#token, key);
const res = new Promise( (resolve, reject) => {
try {
client.onSubscribed = () => {
this.publish(client);
};
client.connect((res) => {
resolve(res);
});
}
catch (err) {
reject(err);
}
});
return res;
};
}
module.exports = INRRequest;
|