Skip to content

Commit

Permalink
merge responses
Browse files Browse the repository at this point in the history
  • Loading branch information
barduinor committed Sep 13, 2023
1 parent 5354e7b commit 95aa218
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/DeployIncremental.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const deployIncremental = async (remoteCollectionID, localCollection) => {
remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
await mergeRequests(remoteCollection, localCollection)

remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
await mergeResponses(remoteCollection, localCollection)

console.log('Incremental deployment of collection ', localCollection.info.name, ' completed')
}

Expand Down Expand Up @@ -100,6 +103,49 @@ async function mergeRequests (remoteCollection, localCollection) {
}
}

async function mergeResponses (remoteCollection, localCollection) {
console.log('\t Requests:')
const remoteFoldersRequest = remoteCollection.collection.item
.map(folder => ({ id: folder.id, name: folder.name, item: folder.item }))

const localFoldersRequest = localCollection.item
.map(folder => ({ id: folder.id, name: folder.name, item: folder.item }))

// loop folders
for (const localFolder of localFoldersRequest) {
const remoteRequests = remoteFoldersRequest.find(remoteFolder => remoteFolder.id === localFolder.id).item
const localRequests = localFolder.item
console.log('\t\t Folder: ', localFolder.name)
// loop requests
for (const localRequest of localRequests) {
const remoteResponses = remoteRequests.find(remoteRequest => remoteRequest.id === localRequest.id).response
const localResponses = localRequest.response
console.log('\t\t\t Request: ', localRequest.name)

// create new responses
const newResponses = localResponses.filter(localResponse => !remoteResponses.find(remoteResponse => remoteResponse.id === localResponse.id))
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)
.create(pmResponse, localRequest.id)
.then(() => '[OK]')
console.log(msg, resp)
}

// 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)
.delete(response.id)
.then(() => '[OK]')
console.log(msg, resp)
}
}
}
}

module.exports = {
deployIncremental
}
34 changes: 33 additions & 1 deletion src/PostmanCovertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ const requestFromLocal = (localRequest) => {
return request
}

const responseFromLocal = (localResponse) => {
// if ()
const response = {
// owner: '8119550',
// lastUpdatedBy: '8119550',
// lastRevision: 32546597265,
// request: '331bbc94-5425-46f3-8c02-31c353d2ced8',

id: localResponse.id, //
name: localResponse.name, //
status: localResponse.status, //
responseCode: {
code: localResponse.code, //
name: localResponse.status, //
detail: ''
},
// time: null,
headers: localResponse.header, //
cookies: [],
mime: null,
text: localResponse.body, //
language: 'Text', //
rawDataType: 'text'//
// requestObject: null,
// createdAt: '2023-09-13T14:53:05.000Z',
// updatedAt: '2023-09-13T14:53:05.000Z'
}

return response
}

const dataFromLocalURLEncode = (localFormData) => {
const data = []
for (const param of localFormData) {
Expand All @@ -106,5 +137,6 @@ const dataFromLocalURLEncode = (localFormData) => {
}

module.exports = {
requestFromLocal
requestFromLocal,
responseFromLocal
}
62 changes: 61 additions & 1 deletion src/postmanAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,68 @@ class Request {
}
} // class Request

class Response {
constructor (collectionId) {
this.collectionId = collectionId
this.apiKey = process.env.POSTMAN_API_KEY
this.axios = axios.create({
timeout: 10000,
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
})
}

async get (responseId) {
return await this.axios.get(
`https://api.getpostman.com/collections/${this.collectionId}/responses/${responseId}`
).then(function (axiosResp) {
if (axiosResp.status !== 200) {
throw new Error(`Error getting response ${responseId}: ${axiosResp.status} ${axiosResp.statusText}`)
} else {
return axiosResp.data
}
})
.catch(function (error) {
logAxiosError(error)
})
}

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

async delete (responseId) {
return await this.axios.delete(
`https://api.getpostman.com/collections/${this.collectionId}/responses/${responseId}`

).then(function (axiosResp) {
if (axiosResp.status !== 200) {
throw new Error(`Error deleting response ${responseId}: ${axiosResp.status} ${axiosResp.statusText}`)
} else {
return axiosResp.data
}
})
.catch(function (error) {
logAxiosError(error)
})
}
} // class Response

module.exports = {
Collection,
Folder,
Request
Request,
Response
}

0 comments on commit 95aa218

Please sign in to comment.