-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated new DeployBulk to update collection head or full
- Loading branch information
Showing
2 changed files
with
52 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,51 @@ | ||
// Deploy collection in bulks | ||
// -------------------------------------------------------------- | ||
// Use the collection PUT to deploy the new collection in bulk | ||
// using the constructed collection from the previous step. | ||
// It first updates the collection with an empty collection | ||
// to remove all folders and requests. | ||
// Then it updates the collection with the new collection | ||
// to add all folders and requests. | ||
// ------------------------------------------------------------ | ||
// We beleive this to be the source of the problem, where | ||
// erractic behaviour is observed when deploying the collection | ||
// in bulk, with an HTTP 500 server error. | ||
// ------------------------------------------------------------ | ||
// const pmConvert = require('./PostmanCovertions') | ||
const pmAPI = require('./postmanAPI') | ||
|
||
const axios = require('axios') | ||
|
||
const deployBulk = async (collectionId, localCollection) => { | ||
// prevent old folders from remaining in place by first removing all items | ||
const emptyCollection = { ...localCollection } | ||
emptyCollection.item = [] | ||
|
||
// console.log('Empty Collection:',{ collection: emptyCollection }) | ||
|
||
// first publish an empty collection to ensure all folders are removed | ||
await axios.put( | ||
`https://api.getpostman.com/collections/${collectionId}`, | ||
// JSON.stringify({ collection: emptyCollection}), | ||
{ collection: emptyCollection }, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Api-Key': process.env.POSTMAN_API_KEY | ||
} | ||
}) | ||
.then(function () { | ||
console.log('EMPTY COLLECTION PUT OK: ', localCollection.info.name) | ||
// then publish the new collection | ||
axios.put( | ||
`https://api.getpostman.com/collections/${collectionId}`, | ||
// JSON.stringify({ collection }), | ||
{ collection: localCollection }, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Api-Key': process.env.POSTMAN_API_KEY | ||
} | ||
}) | ||
.then(function () { | ||
console.log('FULL COLLECTION PUT OK:', localCollection.info.name) | ||
} | ||
) | ||
.catch(function (error) { | ||
// console.dir(error.response, { depth: 100 }) | ||
logAxiosError(error) | ||
// throw error | ||
}) | ||
const deployColectionHead = async (localCollection, remoteCollectionID) => { | ||
const collectionHead = { ...localCollection } | ||
collectionHead.item = [] | ||
const msg = `\nDeploying collection head ${collectionHead.info.name} to ${remoteCollectionID}` | ||
await new pmAPI.Collection(remoteCollectionID) | ||
.update(collectionHead) | ||
.then(() => { console.log(msg, '-> OK\n') }) | ||
.catch((error) => { | ||
console.log(msg, '-> FAIL') | ||
handlePostmanAPIError(error) | ||
}) | ||
.catch(function (error) { | ||
// console.dir(error.response, { depth: 100 }) | ||
logAxiosError(error) | ||
// throw error | ||
} | ||
|
||
const deployColectionFull = async (localCollection, remoteCollectionID) => { | ||
const msg = `\nDeploying full collection ${localCollection.info.name} to ${remoteCollectionID}` | ||
await new pmAPI.Collection(remoteCollectionID) | ||
.update(localCollection) | ||
.then(() => { console.log(msg, '-> OK\n') }) | ||
.catch((error) => { | ||
console.log(msg, '-> FAIL') | ||
handlePostmanAPIError(error) | ||
}) | ||
} | ||
function logAxiosError (error) { | ||
|
||
// Handle axios error | ||
const handlePostmanAPIError = (error) => { | ||
if (error.response) { | ||
// The request was made and the server responded with a status code | ||
// that falls out of the range of 2xx | ||
console.log('ERROR DATA', error.response.data) | ||
console.log('ERROR STATUS', error.response.status) | ||
console.log('ERROR HEADERS', error.response.headers) | ||
} else if (error.request) { | ||
// The request was made but no response was received | ||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | ||
// http.ClientRequest in node.js | ||
console.log('ERROR REQUEST', error.request) | ||
// The request was made and the server responded with a status code | ||
// that falls out of the range of 2xx | ||
console.log('API ERROR:', error.response.data) | ||
} else { | ||
// Something happened in setting up the request that triggered an Error | ||
console.log('ERROR MESSAGE', error.message) | ||
// The request was made but no response was received | ||
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | ||
// http.ClientRequest in node.js | ||
console.log('NO RESPONSE:', error.message) | ||
if (error.cause) { | ||
console.log('CAUSE:', error.cause) | ||
} | ||
} | ||
// console.log('ERROR CONFIG', error.config) | ||
const { method, url, data } = error.config | ||
console.log('REQUEST DETAILS', { method, url, data }) | ||
process.exit(1) | ||
} | ||
|
||
module.exports = { | ||
deployBulk | ||
deployColectionHead, | ||
deployColectionFull | ||
} |
This file contains 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 characters