Last active
February 16, 2024 19:20
-
-
Save prashantagarwal/f278b3ca78b3927cde06f68674b82cae to your computer and use it in GitHub Desktop.
Revisions
-
prashantagarwal revised this gist
May 29, 2023 . 1 changed file with 111 additions and 93 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,104 +1,122 @@ function executeReferencedRequest({ collectionUId, requestUId, requestName, apiKey }, callback) { var _ = require('lodash'); let message; if (!apiKey) { message = "Pleas generate an API key and add it in your collection level variables" } if (!collectionUId) { message = "Please enter the UId of the collection from which you want to resuse a request or script" } if (!requestUId && !requestName) { message = "Please provide the UId or the name of the request you want to reuse from the collection" } if (message) { throw new Error(message); } /* Here we are making an API call to Postman (Refer: https://api.getpostman.com) using pm.sendRequest from the script, to fetch the collection from which we need to reuse the request. */ pm.sendRequest({ "method": "GET", "header": [{ "key": "x-api-key", "value": apiKey, "type": "text" }], "url": { "raw": `https://api.getpostman.com/collections/${collectionUId}`, "protocol": "https", "host": [ "api", "getpostman", "com" ], "path": [ "collections", collectionUId ] } }, function (err, response) { if(err) { console.error("Unable to fetch the referenced request", err); callback && callback(err, null); throw new Error("Unable to fetch the request with the provided details") } let collection = response.json().collection; /* This function will find a given request in a collection using request name or id. It will also inherit Authorization and Scripts from its parent chaing accordingly. */ function processCollectionRequest(tree, identifier, identifierType) { if(tree[identifierType] === identifier){ return tree; } else if (!_.isEmpty(tree.item)){ var result; for(let i = 0; !result && i < tree.item.length; i++) { result = processCollectionRequest(tree.item[i], identifier, identifierType); if(result) { // Extend prerequest and test scripts with collection and folder level scripts let nodePreRequestScript = _.find(result.event, {listen: "prerequest"}), parentPreRequestScript = _.find(tree.event, {listen: "prerequest"}), nodeTestScript = _.find(result.event, {listen: "test"}), parentTestScript = _.find(tree.event, {listen: "test"}); nodePreRequestScript ? (nodePreRequestScript.script.exec = _.concat( parentPreRequestScript && parentPreRequestScript.script.exec, nodePreRequestScript.script.exec )) : (nodePreRequestScript = parentPreRequestScript); nodeTestScript ? (nodeTestScript.script.exec = _.concat( parentTestScript && parentTestScript.script.exec, nodeTestScript.script.exec )) : (nodeTestScript = parentTestScript); result.event = _.compact(_.concat(nodePreRequestScript, nodeTestScript)); //Inherit authorization from parent chain if(!result.request.auth && tree.auth) { result.request.auth = tree.auth } } } return result; } return; } //Handle any collection level variables in the referenced collection _.forEach(collection.variables, function (variable) { pm.collectionVariables.set(variable.key, variable.value); }); let identifierType = requestUId ? 'uid' : 'name', request = requestUId || requestName; let referencedRequest = processCollectionRequest(collection, request, identifierType), preRequestScript = _.find(referencedRequest.event, {listen: "prerequest"}), testScript = _.find(referencedRequest.event, {listen: "test"}); // Store pre-request script, test script & request as local variables so that they can be used later pm.variables.set("referencedPreRequestScript", preRequestScript && preRequestScript.script.exec.join("\n")); pm.variables.set("referencedTestScript", testScript && testScript.script.exec.join("\n")); pm.variables.set("referencedRequest", referencedRequest && referencedRequest.request); callback && callback(null, collection); }); } -
prashantagarwal renamed this gist
May 4, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
prashantagarwal created this gist
May 4, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,104 @@ function executeReferencedRequest({ collectionUUID, requestName }, callback) { var _ = require('lodash'); //Please refer Postman docs on how to generate an API KEY https://go.pstmn.io/docs-pro-api-intro const API_KEY = "<API-KEY>"; /* Here we are making an API call to Postman (Refer: https://api.getpostman.com) using pm.sendRequest from the script, to fetch the collection from which we need to reuse the request. */ pm.sendRequest({ "method": "GET", "header": [{ "key": "x-api-key", "value": API_KEY, "type": "text" }], "url": { "raw": `https://api.getpostman.com/collections/${collectionUUID}`, "protocol": "https", "host": [ "api", "getpostman", "com" ], "path": [ "collections", collectionUUID ] } }, function (err, response) { if(err) { console.error("Unable to fetch the referenced request", err); callback && callback(err, null); return; } let collection = response.json().collection; /* This function will find a given request in a collection using request name. It will also inherit Authorization and Scripts from its parent chaing accordingly. */ function processCollectionRequest(tree, identifier) { if(tree.name === identifier){ return tree; } else if (!_.isEmpty(tree.item)){ var result; for(let i = 0; !result && i < tree.item.length; i++) { result = processCollectionRequest(tree.item[i], identifier); if(result) { // Extend prerequest and test scripts with collection and folder level scripts let nodePreRequestScript = _.find(result.event, {listen: "prerequest"}), parentPreRequestScript = _.find(tree.event, {listen: "prerequest"}), nodeTestScript = _.find(result.event, {listen: "test"}), parentTestScript = _.find(tree.event, {listen: "test"}); nodePreRequestScript ? (nodePreRequestScript.script.exec = _.concat( parentPreRequestScript && parentPreRequestScript.script.exec, nodePreRequestScript.script.exec )) : (nodePreRequestScript = parentPreRequestScript); nodeTestScript ? (nodeTestScript.script.exec = _.concat( parentTestScript && parentTestScript.script.exec, nodeTestScript.script.exec )) : (nodeTestScript = parentTestScript); result.event = _.compact(_.concat(nodePreRequestScript, nodeTestScript)); //Inherit authorization from parent chain if(!result.request.auth && tree.auth) { result.request.auth = tree.auth } } } return result; } return; } //Handle any collection level variables in the referenced collection _.forEach(collection.variables, function (variable) { pm.collectionVariables.set(variable.key, variable.value); }); let referencedRequest = processCollectionRequest(collection, requestName), preRequestScript = _.find(referencedRequest.event, {listen: "prerequest"}), testScript = _.find(referencedRequest.event, {listen: "test"}); // Store pre-request script, test script & request as local variables so that they can be used later pm.variables.set("referencedPreRequestScript", preRequestScript && preRequestScript.script.exec.join("\n")); pm.variables.set("referencedTestScript", testScript && testScript.script.exec.join("\n")); pm.variables.set("referencedRequest", referencedRequest && referencedRequest.request); callback && callback(null, collection); }); }