Skip to content

Commit

Permalink
Generate specs from local proxy bundle (#10)
Browse files Browse the repository at this point in the history
* Generate specs from local proxy bundle

Added support for local proxy bundle i.e. you don't have to download bundle from Edge instance.

* Generate specs from local proxy bundle

Added support for local proxy bundle i.e. you don't have to download bundle from Edge instance.

* Generate specs from local proxy bundle

Added support for local proxy bundle i.e. you don't have to download bundle from Edge instance.
  • Loading branch information
mjsebai committed Aug 30, 2016
1 parent f4f4963 commit 52089ea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 36 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ $ sudo npm install -g apigee2openapi


#### Examples

1. Download proxy bundle from Edge instance and generate OAI
```bash
$ apigee2openapi -d /Users/Anil/Desktop/
```
2. Use local proxy bundle (zip) to generate specs
```bash
$ apigee2openapi -d ~/Documents/GlobalWeather -l ~/Documents/GlobalWeather.zip -n GlobalWeather -e https://msebai-test.apigee.net
```

#### Articles

Expand Down
9 changes: 8 additions & 1 deletion bin/apigee2openapi
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ program
program
.usage('<options>')
.option('-d, --destination <file>', 'API Bundle destination location....')
.option('-l, --local <file>', 'Use local API bundle')
.option('-n, --name <API name>', 'API proxy name. Required if local bundle is used')
.option('-e, --endpoint <API proxy endpoint>', 'API proxy endpoint e.g. https://{ORGNAME}-{ENV}.apigee.net. Required if local bundle is used.')
.description('Generates openapi 2.0 Spec from Apigee Proxy');

program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ apigee2openapi --help');
console.log(' $ apigee2openapi -d /Users/Anil/Desktop/');
console.log(' $ apigee2openapi -d ~/Desktop/weather-api -l ~/Desktop/WeatherAPI.zip -n WeatherAPI -e https://msebai-test.apigee.net');
console.log('');
});

program.parse(process.argv);

var options = {};
options.destination = program.destination;
options.file = program.local;
options.api = program.name;
options.proxyEndPoint = program.endpoint;

fetch.fetchProxy(options, function(err, reply) {
if(err) {
Expand All @@ -35,4 +42,4 @@ fetch.fetchProxy(options, function(err, reply) {
else {
//nothing for now..
}
});
});
93 changes: 59 additions & 34 deletions lib/fetchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ var questions = [
];

function fetchProxy(options, cb) {
if (options.file && options.api && options.proxyEndPoint) {
// process local proxy bundle to generate openapi spec
fetchProxyLocal(options, cb)
} else {
// download bundle from Edge and then generate openapi spec
fetchProxyPrompt(options, cb)
}
}

function fetchProxyLocal(options, cb) {
if (!options.destination) {
options.destination = pathLib.join(__dirname, '../api_bundles') + "/" + options.api;
}
generateOpenapi(options, cb)
}

function fetchProxyPrompt(options, cb) {
inquirer.prompt( questions, function( answers ) {
var destination = options.destination || pathLib.join(__dirname, '../api_bundles');
var destination = options.destination || pathLib.join(__dirname, '../api_bundles');
destination = destination + "/" + answers.api;
answers.file = destination + "/" + answers.api + ".zip";
for (answer in answers) {
Expand All @@ -44,42 +60,51 @@ function fetchProxy(options, cb) {
if (err) {
return cb(err, {});
}
// Unzip folder.....
var stream = fs.createReadStream(answers.file).pipe(unzip.Extract({ path: destination }));
var had_error = false;
stream.on('error', function(err){
had_error = true;
return cb(err, {});
});
stream.on('close', function(){
if (!had_error) {
// generate openapi...
delete answers['password'];
// Generate multiple openapi files based on number of files in proxies.
// Read through proxy files..
delete answers['password']
options.destination = destination
options.file = answers.file
options.api = answers.api
options.proxyEndPoint = answers.proxyEndPoint
generateOpenapi(options, cb)
});
});
});
}

function generateOpenapi(options, cb) {
// Unzip folder.....
var stream = fs.createReadStream(options.file).pipe(unzip.Extract({ path: options.destination }));
var had_error = false;
stream.on('error', function(err){
had_error = true;
return cb(err, {});
});
stream.on('close', function(){
if (!had_error) {
if (options.password)
delete options['password'];

glob(destination + "/apiproxy/proxies"+ "/*.xml", options, function (er, files) {
async.each(Object.keys(files), function (i, callback) {
proxy.genopenapi(destination, answers, files[i], function (err, reply) {
if (err) {
callback(err, {});
}
callback(null, {});
});
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
cb(err, {})
}
else {
cb(null, {});
}
});
});
// generate openapi...
// Generate multiple openapi files based on number of files in proxies.
// Read through proxy files..
glob(options.destination + "/apiproxy/proxies" + "/*.xml", options, function (er, files) {
async.each(Object.keys(files), function (i, callback) {
proxy.genopenapi(options.destination, options, files[i], function (err, reply) {
if (err) {
callback(err, {});
}
callback(null, {});
});
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
cb(err, {})
}
else {
cb(null, {});
}
});
});
});
}
});
}

0 comments on commit 52089ea

Please sign in to comment.