/**
* Get access token from exchange using AJAX call.
* Q library is used here for better handling of Async calls
**/
function getAccessTokenFromExchange() {
return Q($.ajax({
type: 'get',
cache: false,
headers: {
'accept':'application/json'
}
}));
}
/**
* Code to search for RAML APIs from exchange based on the search criteria
* Q library is used here for better handling of Async calls
**/
function getExchangeAPIs(searchTerm) {
// Utility function from IZ to display loading icon
Util.loadStart();
return getAccessTokenFromExchange()
.then(function(accessTokenResponse){
// Access token to access Anypoint APIs
var accessToken = 'bearer ' + accessTokenResponse.accessToken;
/**
* Invoke Anypoint API to get the list of RAML APIs based on seach criteria
**/
var exchangeRequest = {
"query": "{ assets(query: {searchTerm: \""+searchTerm+"\", type: \"rest-api\"},latestVersionsOnly: true) { groupId, version, name, assetId, createdBy { firstName, lastName },updatedAt,files { classifier, packaging,externalLink },tags {value},organizationId } }",
"variables": {
"accessToken": accessTokenResponse.accessToken
}
};
/**
* Invoke Anypoint API to get the list of RAML APIs based on seach criteria
**/
return Q($.ajax({
type: 'post',
contentType: "application/json",
data: JSON.stringify(exchangeRequest)
}));
})
.then(function(apisList) {
/**
* Parse the received assets details and populate the html table
* Sample Response Structure
*{
* "data": {
* "assets": [
* {
* "organizationId": "52fe2647-1153-46b7-8725-33430cd480ba",
* "groupId": "52fe2647-1153-46b7-8725-33430cd480ba",
* "assetId": "iz-starter-kit",
* "version": "1.0.0",
* "createdBy": {
* "firstName": "Msch022019",
* "lastName": "Msch022019"
* },
* "files": [
* {
* "classifier": "raml",
* "packaging": "zip",
* "externalLink": "..."
* },
* {
* "classifier": "fat-raml",
* "packaging": "zip",
* "externalLink": "..."
* }
* ],
* "type": "rest-api"
* },
* {
* ...
* }
* ]
* }
*}
**/
var assetHtml = '';
$.each(apisList.data.assets, function(index, asset) {
/**
* Get the asset download link for type fat-raml
* */
var assetUrl = $.grep(asset.files, function (e) {
return e.classifier == 'fat-raml';
})[0];
assetHtml += '<tr>' +
'<td> <input type="radio" name="http_requester_selected_api" asset-name="'+asset.name+'" value="'+assetUrl.externalLink+'" '+(index === 0 ? 'checked': '')+'> '+asset.name+'</td>' +
'<td>'+asset.createdBy.firstName+'</td>' +
'<td>'+asset.version+'</td>' +
'</tr>';
});
console.log(assetHtml);
$('#http_requester_api_list').html(assetHtml);
})
.catch(function (error) {
console.log(error);
return Util.loadEndWithError('Error fetching RAML APIs', error);
})
.done(function() {
// Utility function from IZ to stop displaying loading icon
Util.loadEnd();
});
}
/**
* Listener for API search button
**/
$(document).off("click","#http_requester_search_raml_apis");
$(document).on("click","#http_requester_search_raml_apis",function() {
// Retrieve the enterd search criteria and get the RAML APIs
getExchangeAPIs($('#http_requester_exchange_search_term').val());
});
/**
* Listener listening on final submit click
**/
$(document).off("click",".select_{{nodeId}}");
$(document).on("click",".select_{{nodeId}}",function() {
var componentId = "{{nodeId}}";
var selectedAsset = $('input[name="http_requester_selected_api"]:checked').val();
var assetName = $('input[name="http_requester_selected_api"]:checked').attr('asset-name');
assetName = assetName.trim().replace(new RegExp(' ', 'g'), '_').toLowerCase();
Util.loadStart();
// Utility function from IZ to download binary file
// We are downloading the raml api zip file here to find out the root raml name, which in turn will be used in HTTP connector
Q(Asset.downloadBinaryFile(selectedAsset))
.then(function (zipFile) {
return JSZip.loadAsync(zipFile);
})
.then(function(ramlContents) {
return ramlContents.file("exchange.json").async("string");
})
.then(function(exchangeJson) {
exchangeJson = JSON.parse(exchangeJson);
console.log(exchangeJson);
FlowEditor.componentEditDetails[componentId] = {
rootRaml: exchangeJson.main,
assetUrl: selectedAsset,
apiName: assetName
};
console.log(FlowEditor.componentEditDetails[componentId]);
return '';
})
.catch(function (error) {
console.log(error);
return Util.loadEndWithError('Error fetching root RAML name', error);
})
.done(function() {
// Utility function from IZ to stop displaying loading icon
Util.loadEnd();
});
});