1 | //
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const BlueprintManager = require('./BlueprintManager');
|
7 | const HttpRequestor = require('./HttpRequestor');
|
8 | const JsonPathReplacer = require('./JsonPathReplacer');
|
9 | const MultipartRelatedResponse = require('./MultipartRelatedResponse');
|
10 | const ResponseMergerBase = require('./ResponseMergerBase');
|
11 | const SubrequestsManager = require('./SubrequestsManager');
|
12 |
|
13 | /**
|
14 | * Execute a blueprint of requests using a requestor, get a single response.
|
15 | *
|
16 | * @param {string} blueprint
|
17 | * The tree of requests in the expected blueprint format.
|
18 | * @param {RequestorInterface} requestor
|
19 | * The wrapped HTTP library to execute all communications.
|
20 | * @param {function} MergerClass
|
21 | * The class with static methods to merge all the subresponses into one.
|
22 | *
|
23 | * @return {Promise.<string>}
|
24 | * The promise of a single response.
|
25 | */
|
26 | function request(
|
27 | blueprint ,
|
28 | requestor = new HttpRequestor(),
|
29 | MergerClass = MultipartRelatedResponse
|
30 | ) {
|
31 | // Parse the blueprint to build the request tree.
|
32 | const tree = BlueprintManager.parse(blueprint);
|
33 | // Make all the sequential requests that, in turn, contain parallel requests.
|
34 | return SubrequestsManager.request(tree, requestor)
|
35 | // Merge all the responses into a single response string.
|
36 | .then(subresponses => MergerClass.mergeResponses(subresponses));
|
37 | }
|
38 |
|
39 | /**
|
40 | * Execute a blueprint of requests using a requestor, get an array of responses.
|
41 | *
|
42 | * @param {string} blueprint
|
43 | * The tree of requests in the expected blueprint format.
|
44 | * @param {RequestorInterface} requestor
|
45 | * The wrapped HTTP library to execute all communications.
|
46 | *
|
47 | * @return {Promise.<string>}
|
48 | * The promise of a single response.
|
49 | */
|
50 | function requestArray(
|
51 | blueprint ,
|
52 | requestor = new HttpRequestor()
|
53 | ) {
|
54 | // Parse the blueprint to build the request tree.
|
55 | const tree = BlueprintManager.parse(blueprint);
|
56 | // Make all the sequential requests that, in turn, contain parallel requests.
|
57 | return SubrequestsManager.request(tree, requestor);
|
58 | }
|
59 |
|
60 | module.exports = {
|
61 | request,
|
62 | requestArray,
|
63 | lib: {
|
64 | BlueprintManager,
|
65 | HttpRequestor,
|
66 | JsonPathReplacer,
|
67 | MultipartRelatedResponse,
|
68 | ResponseMergerBase,
|
69 | SubrequestsManager,
|
70 | },
|
71 | };
|