Skip to content

Commit

Permalink
sorting responses + error flow
Browse files Browse the repository at this point in the history
  • Loading branch information
barduinor committed Sep 13, 2023
1 parent 69bad1a commit 68a8d4a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 37 deletions.
75 changes: 61 additions & 14 deletions src/DeployIncremental.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,27 @@ async function mergeFolders (remoteCollection, localCollection) {

for (const folder of newFolders) {
const msg = `\t\t Creating new folder ${folder.id} ${folder.name}`
const resp = await new pmAPI.Folder(remoteCollection.collection.info.uid)
await new pmAPI.Folder(remoteCollection.collection.info.uid)
.create(folder)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}

// delete olds folders
const oldFolders = remoteFolders.filter(remoteFolder => !localFolders.find(localFolder => localFolder.id === remoteFolder.id))
// console.log('oldFolders: \n', oldFolders)
for (const folder of oldFolders) {
const msg = `\t\t Deleting old folder ${folder.id} ${folder.name}`
const resp = await new pmAPI.Folder(remoteCollection.collection.info.uid)
await new pmAPI.Folder(remoteCollection.collection.info.uid)
.delete(folder.id)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}
}

Expand All @@ -78,28 +84,46 @@ async function mergeRequests (remoteCollection, localCollection) {
const localRequests = localFolder.item
console.log('\t\t In Folder: ', localFolder.name)

// create new rewuests
// create new requests
const newRequests = localRequests.filter(localRequest => !remoteRequests.find(remoteRequest => remoteRequest.id === localRequest.id))

for (const request of newRequests) {
const pmRequest = pmConvert.requestFromLocal(request)
const msg = `\t\t\t Creating new request ${request.id} ${request.name}`
// console.log('request: \n', JSON.stringify(request, 2))
const resp = await new pmAPI.Request(remoteCollection.collection.info.uid)
await new pmAPI.Request(remoteCollection.collection.info.uid)
.create(pmRequest, localFolder.id)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}

// delete olds requests
// delete old requests
const oldRequests = remoteRequests.filter(remoteRequest => !localRequests.find(localRequest => localRequest.id === remoteRequest.id))
for (const request of oldRequests) {
const msg = `\t\t\t Deleting old request ${request.id} ${request.name}`
const resp = await new pmAPI.Request(remoteCollection.collection.info.uid)
await new pmAPI.Request(remoteCollection.collection.info.uid)
.delete(request.id)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}

// sort requests in folder
const order = localRequests.map(request => request.id)
// console.log('Local requests id: ', localRequestsId)
const msg = `\t\t\t Sorting requests in folder ${localFolder.name}`
await new pmAPI.Folder(remoteCollection.collection.info.uid)
.update(localFolder.id, { order })
.then(() => '[OK]')
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}
}

Expand Down Expand Up @@ -127,25 +151,48 @@ async function mergeResponses (remoteCollection, localCollection) {
for (const response of newResponses) {
const pmResponse = pmConvert.responseFromLocal(response)
const msg = `\t\t\t\t Creating new response ${response.id} ${response.code} ${response.status}`
const resp = await new pmAPI.Response(remoteCollection.collection.info.uid)
await new pmAPI.Response(remoteCollection.collection.info.uid)
.create(pmResponse, localRequest.id)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}

// delete old responses
const oldResponses = remoteResponses.filter(remoteResponse => !localResponses.find(localResponse => localResponse.id === remoteResponse.id))
for (const response of oldResponses) {
const msg = `\t\t\t\t Deleting old response ${response.id} ${response.code} ${response.status}`
const resp = await new pmAPI.Response(remoteCollection.collection.info.uid)
await new pmAPI.Response(remoteCollection.collection.info.uid)
.delete(response.id)
.then(() => '[OK]')
console.log(msg, resp)
.catch((error) => {
console.log(msg, '[FAIL]')
handlePostmanAPIError(error)
})
}
}
}
}

// log 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('API ERROR:', error.response.data)
} else {
// 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.cause)
}
const { method, url, data } = error.config
console.log('REQUEST DETAILS', { method, url, data })
process.exit(1)
}

module.exports = {
deployIncremental
}
25 changes: 2 additions & 23 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,9 @@ const byPriority = (a, b) => {
return 0
}

// log axios error
const logAxiosError = (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)
} else {
// Something happened in setting up the request that triggered an Error
console.log('ERROR MESSAGE', error.message)
}
console.log('ERROR CONFIG', error.config)
// process.exit(1)
}

module.exports = {
GenID: genID,
ByName: byName,
ByPriority: byPriority,
logAxiosError
ByPriority: byPriority
// logAxiosError
}
32 changes: 32 additions & 0 deletions src/postmanAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class Folder {
})
}

async update (folderId, folder) {
return await this.axios.put(
`https://api.getpostman.com/collections/${this.collectionId}/folders/${folderId}`,
folder
).then(function (response) {
if (response.status !== 200) {
throw new Error(`Error updating folder ${folder.Id}: ${response.status} ${response.statusText}`)
} else {
return response.data
}
})
// .catch(function (error) {
// logAxiosError(error)
// })
}

async delete (folderId) {
return await this.axios.delete(
`https://api.getpostman.com/collections/${this.collectionId}/folders/${folderId}`
Expand Down Expand Up @@ -128,6 +144,22 @@ class Request {
})
}

async update (request, requestId) {
return await this.axios.put(
`https://api.getpostman.com/collections/${this.collectionId}/requests/${requestId}`,
request
).then(function (response) {
if (response.status !== 200) {
throw new Error(`Error updating request ${request.id}: ${response.status} ${response.statusText}`)
} else {
return response.data
}
})
.catch(function (error) {
logAxiosError(error)
})
}

async delete (requestId) {
return await this.axios.delete(
`https://api.getpostman.com/collections/${this.collectionId}/requests/${requestId}`
Expand Down

0 comments on commit 68a8d4a

Please sign in to comment.