Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 174 additions & 31 deletions docs/packages/http.md

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions packages/http/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ export enum Action {
* @param {string} myObject - Name of the varibale where will be store myResult
*/
PARALLEL_GET = "execute parallel GET to {} and save the result named {} within the object {}",
/**
* `Given I execute parallel GET to 'myEndpoint' and save the result named 'myResult' within the object 'myObject' with header 'myHeader'`<br><br>
* By repeating this statment *n* times it will perform *n* parallel get to *myEndpoint* with header *myHeader* and store the result in
* *myObject.myResult* which will be a dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myEndpoint - Name of the variable containing the url's endpoint or the url's endpoint itself
* @param {string} myResult - Name of the varibale where will be store the reuslt of the get
* @param {string} myObject - Name of the varibale where will be store myResult
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_GET_HEADER = "execute parallel GET to {} with header {} and save the result named {} within the object {}",
/**
* `Given I execute parallel GET to array 'myEndpointArray' and save the result named 'myResult' within the object 'myObject'`<br><br>
* Perform parallel get to array *myEndpointArray* and store the result in *myObject.myResult* which will be an array of dictionary with two entries:
Expand All @@ -80,6 +92,17 @@ export enum Action {
* @param {string} myObject - Name of the varibale where will be store myResult
*/
PARALLEL_GET_ARRAY = "execute parallel GET to array {} and save the result named {} within the object {}",
/**
* `Given I execute parallel GET to array 'myEndpointArray' and save the result named 'myResult' within the object 'myObject' with header 'myHeader'`<br><br>
* Perform parallel get to array *myEndpointArray* with header *myHeader* and store the result in *myObject.myResult* which will be an array of dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myEndpointArray - Name of the variable containing the array of urls' endpoint
* @param {string} myResult - Name of the varibale where will be store the reuslt of the get
* @param {string} myObject - Name of the varibale where will be store myResult
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_GET_ARRAY_HEADER = "execute parallel GET to array {} with header {} and save the result named {} within the object {}",
/**
* `Given I execute parallel POST with 'myData' to 'myEndpoint' and save the result named 'myResult' within the object 'myObject'`<br><br>
* By repeating this statment *n* times it will perform *n* parallel post to *myEndpoint* with data contained in *myData* and store the result in
Expand All @@ -92,6 +115,19 @@ export enum Action {
* @param {string} myObject - Name of the varibale where will be store myResult
*/
PARALLEL_POST = "execute parallel POST with {} to {} and save the result named {} within the object {}",
/**
* `Given I execute parallel POST with 'myData' to 'myEndpoint' and save the result named 'myResult' within the object 'myObject' with header 'myHeader'`<br><br>
* By repeating this statment *n* times it will perform *n* parallel post to *myEndpoint* with data contained in *myData* and header contained in *myHeader* and store the result in
* *myObject.myResult* which will be an array of dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myData - Name of the variable containing the data of the post
* @param {string} myEndpoint - Name of the variable containing the url's endpoint
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
* @param {string} myObject - Name of the varibale where will be store myResult
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_POST_HEADER = "execute parallel POST with {} to {} with header {} and save the result named {} within the object {}",
/**
* `Given I execute parallel POST with 'myData' to array 'myEndpointArray' and save the result named 'myResult' within the object 'myObject'`<br><br>
* Perform parallel post to array *myEndpointArray* with data contained in *myData* and store the result in
Expand All @@ -104,6 +140,19 @@ export enum Action {
* @param {string} myObject - Name of the varibale where will be store myResult
*/
PARALLEL_POST_ARRAY_WITHIN = "execute parallel POST with {} to array {} and save the result named {} within the object {}",
/**
* `Given I execute parallel POST with 'myData' to array 'myEndpointArray', with header 'myHeader' and save the result named 'myResult' within the object 'myObject' with header 'myHeader'`<br><br>
* Perform parallel post to array *myEndpointArray* with data contained in *myData* and header contained in *myHeader* and store the result in
* *myObject.myResult* which will be an array of dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myData - Name of the variable containing the data of the post
* @param {string} myEndpointArray - Name of the variable containing the array of urls' endpoint
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
* @param {string} myObject - Name of the varibale where will be store myResult
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_POST_ARRAY_WITHIN_HEADER = "execute parallel POST with {} to array {} with header {} and save the result named {} within the object {}",
/**
* `Given I execute parallel POST with 'myData' to array 'myEndpointArray' and save the result named 'myResult'`<br><br>
* Perform parallel post to array *myEndpointArray* with data contained in *myData* and store the result in
Expand All @@ -115,6 +164,18 @@ export enum Action {
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
*/
PARALLEL_POST_ARRAY = "execute parallel POST with {} to array {} and save the result named {}",
/**
* `Given I execute parallel POST with 'myData' to array 'myEndpointArray' with header 'myHeader' and save the result named 'myResult' with header 'myHeader'`<br><br>
* Perform parallel post to array *myEndpointArray* with data contained in *myData* and with header contained in *myHeader* and store the result in
* *myResult* which will be an array of dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myData - Name of the variable containing the data of the post
* @param {string} myEndpointArray - Name of the variable containing the array of urls' endpoint
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_POST_ARRAY_HEADER = "execute parallel POST with {} to array {} with header {} and save the result named {}",
/**
* `Given I execute parallel POST with array 'myDataArray' to array 'myEndpointArray' and save the result named 'myResult'`<br><br>
* Perform parallel post to array *myEndpointArray* and for the *n-th* endpoint uses the *n-th* entry found in the *myDataArray* as data
Expand All @@ -126,4 +187,17 @@ export enum Action {
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
*/
PARALLEL_POST_ARRAY_DIFFERENT_DATA = "execute parallel POST with array {} to array {} and save the result named {}",
/**
* `Given I execute parallel POST with array 'myDataArray' to array 'myEndpointArray' and save the result named 'myResult' with header 'myHeader'`<br><br>
* Perform parallel post to array *myEndpointArray* with header *myHeader* and for the *n-th* endpoint uses the *n-th* entry found in the *myDataArray* as data
* and store the result in *myResult* which will be an array of dictionary with two entries:
* * **status**: will contain the status code
* * **result**: in case of success will contain the result otherwise it is a empty string
* @param {string} myDataArray - Name of the variable containing the array of data of the post
* @param {string} myEndpointArray - Name of the variable containing the array of urls' endpoint
* @param {string} myResult - Name of the varibale where will be store the reuslt of the post
* @param {string} myHeader - Name of the variable containing the header
*/
PARALLEL_POST_ARRAY_DIFFERENT_DATA_HEADER = "execute parallel POST with array {} to array {} with header {} and save the result named {}",

}
118 changes: 83 additions & 35 deletions packages/http/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Restroom } from "@restroom-mw/core";
import { ObjectLiteral } from "@restroom-mw/types";
import { Zencode } from "@restroom-mw/zencode";
import axios from "axios";
import axios, { AxiosRequestConfig } from "axios";
import { NextFunction, Request, Response } from "express";
import https from "https";
import { Action } from "./actions";
Expand All @@ -20,6 +20,46 @@ export default (req: Request, res: Response, next: NextFunction) => {
let parallel_params: { output: string; index: [string , number]}[] = [];
let parallel_promises: Promise<any>[] = [];

function genericGet(url: string, ind: [string, number], o: string, headers?: Record<string, string>){
genericRequest(ind, o, {url, method: "get"}, headers);
}

function genericPost(url: string, ind: [string, number], o: string, data: any, headers?: Record<string, string>){
genericRequest(ind, o, {url, data, method: "post"}, headers);
}

function genericRequest(ind: [string, number], o: string, req: Record<string, string>, headers?: Record<string, string>){
let opts: AxiosRequestConfig = { validateStatus: () => true };
if(headers){
opts.headers = headers;
}
parallel_promises.push(axios({...req, ...opts}));
parallel_params.push({
output: o,
index: ind,
});
}

function requestArray(makeRequest: (j: number, url: string, header: Record<string, string>) => void, urlsName: string, headerName: string){
const urls = content[urlsName];
const header = content[headerName];
if(Array.isArray(header)) {
if(urls.length === header.length){
for(let j = 0; j < urls.length; j++) {
makeRequest(j, urls[j], header[j])
}
} else {
throw new Error(`[HTTP] different length of arrays ${urlsName} and ${headerName}`);
}
} else if (header.constructor === Object){
for(let j = 0; j < urls.length; j++) {
makeRequest(j, urls[j], header)
}
} else {
throw new Error(`[HTTP] unrecognised instance of ${headerName}`);
}
}

rr.onBefore(
async (params: {
zencode: Zencode;
Expand Down Expand Up @@ -58,75 +98,83 @@ export default (req: Request, res: Response, next: NextFunction) => {
for (const [urlsName, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_GET_ARRAY), 3)) {
const urls = content[urlsName]
for(let j = 0; j < urls.length; j++) {
parallel_promises.push(axios.get(urls[j], { validateStatus: () => true }));
parallel_params.push({
output: o,
index: [i, j],
});
genericGet(urls[j], [i, j], o);
}
}
}
if (zencode.match(Action.PARALLEL_GET_ARRAY_HEADER)) {
for (const [urlsName, headerName, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_GET_ARRAY_HEADER), 4)) {
requestArray((j, url, header) => genericGet(url,[i,j], o, header), urlsName, headerName);
}
}

if (zencode.match(Action.PARALLEL_GET)) {
for (const [url, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_GET), 3)) {
parallel_promises.push(axios.get(content[url], { validateStatus: () => true }));
parallel_params.push({
output: o,
index: [i, -1],
});
genericGet(content[url], [i, -1], o);
}
}

if (zencode.match(Action.PARALLEL_GET_HEADER)) {
for (const [url, header, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_GET_HEADER), 4)) {
genericGet(content[url], [i, -1], o, content[header]);
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY_WITHIN)) {
for (const [d, urlsName, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY_WITHIN), 4)) {
const urls = content[urlsName]
for(let j = 0; j < urls.length; j++) {
parallel_promises.push(axios.post(urls[j], content[d], { validateStatus: () => true }));
parallel_params.push({
output: o,
index: [i, j],
});
genericPost(urls[j], [i,j], o, content[d]);
}
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY_WITHIN_HEADER)) {
for (const [d, urlsName, headerName, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY_WITHIN_HEADER), 5)) {
requestArray((j, url, header) => genericPost(url,[i,j], o, content[d], header), urlsName, headerName);
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY)) {
for (const [d, urlsName, i] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY), 3)) {
const urls = content[urlsName]
for(let j = 0; j < urls.length; j++) {
parallel_promises.push(axios.post(urls[j], content[d], { validateStatus: () => true }))
parallel_params.push({
output: null,
index: [i, j],
});
genericPost(urls[j], [i,j], null, content[d]);
}
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY_HEADER)) {
for (const [d, urlsName, headerName, i] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY_HEADER), 4)) {
requestArray((j, url, header) => genericPost(url,[i,j], null, content[d], header), urlsName, headerName);
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY_DIFFERENT_DATA)) {
for (const [dataName, urlsName, i] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY_DIFFERENT_DATA), 3)) {
const urls = content[urlsName];
const data = content[dataName];
for(let j = 0; j < urls.length; j++) {
parallel_promises.push(axios.post(urls[j], data[j], { validateStatus: () => true }));
parallel_params.push({
output: null,
index: [i, j],
});
genericPost(urls[j], [i,j], null, data[j]);
}
}
}

if (zencode.match(Action.PARALLEL_POST_ARRAY_DIFFERENT_DATA_HEADER)) {
for (const [dataName, urlsName, headerName, i] of chunks(zencode.paramsOf(Action.PARALLEL_POST_ARRAY_DIFFERENT_DATA_HEADER), 4)) {
requestArray((j, url, header) => genericPost(url,[i,j], null, content[dataName][j], header), urlsName, headerName);
}
}

if (zencode.match(Action.PARALLEL_POST_HEADER)) {
for (const [d, url, header, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_POST_HEADER), 5)) {
genericPost(content[url], [i,-1], o, d, content[header]);
}
}

if (zencode.match(Action.PARALLEL_POST)) {
for (const [d, url, i, o] of chunks(
zencode.paramsOf(Action.PARALLEL_POST),
4
)) {
parallel_promises.push(axios.post(content[url], content[d]));
parallel_params.push({
output: o,
index: [i, -1],
});
for (const [d, url, i, o] of chunks(zencode.paramsOf(Action.PARALLEL_POST), 4)) {
genericPost(url, [i,-1], o, d);
}
}

Expand Down
Loading