Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get collection from Postman API #21

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
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
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ Say that you import a _huge_ collection in a workspace that has tonnes of [envir

1. Clone this into your local folder and navigate to it.
2. Run `npm install`
2. Export the collection JSON from the Postman app (Steps to do that are [here](https://learning.getpostman.com/docs/postman/collections/data_formats/#exporting-postman-data)). The file encoding must be `utf-8`.
3. Run `node index.js --collection <absolute-path-to-collection-file> --environment <name-that-environment-should-get> --apiKey <postman-api-key/path-to-key> --workspaceId <workspace-id> --apiKeyFile`
### Using exported collection's file
1. Export the collection JSON from the Postman app (Steps to do that are [here](https://learning.getpostman.com/docs/postman/collections/data_formats/#exporting-postman-data)). The file encoding must be `utf-8`.
2. Run `node index.js <absolute-path-to-collection-file> <name-that-environment-should-get> <postman-api-key> <workspace-id>`
### Using uuid
2. Run `node index.js <collection-id> <name-that-environment-should-get> <postman-api-key> <workspace-id>`

> The `collection-id` is a parameter to get collection using the API. To get the collection-id, follow these steps:
> 1. Log into the postman app
> 2. Select any collection
> 3. Click on the right arrow beside the collection name
> 4. Click on 'View in Web'
> 5. The opened browser window contains url of the form: [https://postman.co/collections/**xyz-uuid**?version=latest&workspace=uuid](https://postman.co/collections/xyz-uuid?version=latest&workspace=uuid)
> 6. Copy the xyz-uuid before the question mark.

```
Usage: index [options]
Expand All @@ -41,7 +52,7 @@ Options:
> 2. Select any collection
> 3. Click on the right arrow beside the collection name
> 4. Click on 'View in Web'
> 5. The opened browser window contains url of the form: `https://postman.co/collections/xyz-uuid?version=latest&workspace=uuid`
> 5. The opened browser window contains url of the form: [https://postman.co/collections/xyz-uuid?version=latest&workspace=**uuid**](https://postman.co/collections/xyz-uuid?version=latest&workspace=uuid)
> 6. Copy the workspace Id.

### Example
Expand Down
3 changes: 3 additions & 0 deletions lib/getEnvironmentVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ module.exports = {
catch (e) {
throw new Error(e);
}
if(collectionJSON.hasOwnProperty('collection')){
collectionJSON = collectionJSON.collection
}

let result = removeDuplicates(_.compact(traverseObject(collectionJSON)));

Expand Down
2 changes: 1 addition & 1 deletion lib/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function ({ filePath, environmentName, apiKey, workspaceId }, o
function (cb) {
console.info(chalk.green.bold('Warming up...'));

return readFile({ filePath }, cb);
return readFile({ filePath, apiKey }, cb);
},
function (collection, cb) {
console.log('options:', options);
Expand Down
49 changes: 41 additions & 8 deletions lib/readFromFileAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,45 @@
* This is a wrapper around Node.JS' async read file function.
*/

const fs = require('fs');

module.exports = function ({ filePath }, callback) {
fs.readFile(filePath, 'utf8', function (err, fileContents) {
if (err) { return callback(err); }

return callback(null, fileContents);
});
const fs = require('fs'),
request = require('request');

module.exports = function ({ filePath , apiKey}, callback) {
let options = {
method: 'GET',
url: 'https://api.getpostman.com/collections/' + filePath,
headers: {
'X-Api-Key': apiKey,
'Content-Type': 'application/json'
},
json: true
};


//Check if filePath is a Collection uuid (8-4-4-4-12 RFC 4122)
const uid = filePath.split('-');
if( ( uid.length == 6 && ( uid[1].length == 8 || uid[2].length == 4 || uid[3].length == 4 || uid[4].length == 4 || uid[5].length == 12 ))
|| ( uid.length == 5 && ( uid[0].length == 8 || uid[1].length == 4 || uid[2].length == 4 || uid[3].length == 4 || uid[4].length == 12 ))){

request(options, function (error, response, body) {
if (error) {
return cb(new Error(`Error getting a collection:`, error));
}

if (response.statusCode >= 400) {
return cb(new Error(`Postman API returned HTTP ${response.statusCode} with message:`, response.body));
}

return callback(null, JSON.stringify(body));
});

}
//else read file
else {
fs.readFile(filePath, 'utf8', function (err, fileContents) {
if (err) { return callback(err); }

return callback(null, fileContents);
});
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-environment-generator",
"version": "0.0.0",
"version": "0.0.1",
"description": "An app that creates a Postman environment for all the variable names in your Postman collection ",
"main": "index.js",
"scripts": {
Expand Down