Improve application key generating functionality

add_scope_mdm
Dharmakeerthi Lasantha 1 year ago
parent bbcadf19d0
commit d251253948

@ -120,12 +120,13 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} catch (APIServicesException e) { } catch (APIServicesException e) {
String errorMsg = "Error occurred while generating the API application"; String errorMsg = "Error occurred while generating the API application";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new APIManagerException(e); throw new APIManagerException(errorMsg, e);
} }
try { try {
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications = io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications =
consumerRESTAPIServices.getAllApplications(apiApplicationKey, accessTokenInfo, applicationName); consumerRESTAPIServices.getAllApplications(apiApplicationKey, accessTokenInfo, applicationName);
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = null;
List<APIInfo> uniqueApiList = new ArrayList<>(); List<APIInfo> uniqueApiList = new ArrayList<>();
@ -148,11 +149,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
if (applications.length == 0) { if (applications.length == 0) {
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = application = new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application();
new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application();
application.setName(applicationName); application.setName(applicationName);
application = consumerRESTAPIServices.createApplication(apiApplicationKey, accessTokenInfo, application); application = consumerRESTAPIServices.createApplication(apiApplicationKey, accessTokenInfo, application);
List<Subscription> subscriptions = new ArrayList<>(); List<Subscription> subscriptions = new ArrayList<>();
for (APIInfo apiInfo : uniqueApiList) { for (APIInfo apiInfo : uniqueApiList) {
Subscription subscription = new Subscription(); Subscription subscription = new Subscription();
@ -163,10 +163,11 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptions); consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptions);
} else { } else {
if (applications.length == 1) { if (applications.length == 1) {
Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> application = Optional<io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application> applicationOpt =
Arrays.stream(applications).findFirst(); Arrays.stream(applications).findFirst();
application = applicationOpt.get();
Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(apiApplicationKey, accessTokenInfo, Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(apiApplicationKey, accessTokenInfo,
application.get().getApplicationId()); application.getApplicationId());
for (Subscription subscription : subscriptions) { for (Subscription subscription : subscriptions) {
if (uniqueApiList.contains(subscription.getApiInfo())) { if (uniqueApiList.contains(subscription.getApiInfo())) {
uniqueApiList.remove(subscription.getApiInfo()); uniqueApiList.remove(subscription.getApiInfo());
@ -175,13 +176,12 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
} }
//todo duplicate code block -> move to a private method
//duplicate code block
List<Subscription> subscriptionList = new ArrayList<>(); List<Subscription> subscriptionList = new ArrayList<>();
for (APIInfo apiInfo : uniqueApiList) { for (APIInfo apiInfo : uniqueApiList) {
Subscription subscription = new Subscription(); Subscription subscription = new Subscription();
subscription.setApiId(apiInfo.getId()); subscription.setApiId(apiInfo.getId());
subscription.setApplicationId(application.get().getApplicationId()); subscription.setApplicationId(application.getApplicationId());
subscriptionList.add(subscription); subscriptionList.add(subscription);
} }
consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptionList); consumerRESTAPIServices.createSubscriptions(apiApplicationKey, accessTokenInfo, subscriptionList);
@ -190,21 +190,38 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
log.error(msg); log.error(msg);
throw new APIManagerException(msg); throw new APIManagerException(msg);
} }
} }
if (application != null) {
if (!application.getKeys().isEmpty()) {
//todo return Application Keys
return null;
} else{
//todo this method has to br modified and return different object, this is not mapped with the
// response.
io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIKey apiKey =
consumerRESTAPIServices.generateApplicationKeys(apiApplicationKey, accessTokenInfo,
application.getApplicationId());
return null;
}
} else{
String msg = "Application retrieval process failed.";
log.error(msg);
throw new APIManagerException(msg);
}
} catch (APIServicesException e) { } catch (APIServicesException e) {
e.printStackTrace(); String msg = "Error occurred wile processing the response of APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (BadRequestException e) { } catch (BadRequestException e) {
e.printStackTrace(); String msg = "Provided incorrect payload when invoking APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} catch (UnexpectedResponseException e) { } catch (UnexpectedResponseException e) {
e.printStackTrace(); String msg = "Error occurred while invoking APIM REST endpoints.";
log.error(msg, e);
throw new APIManagerException(msg, e);
} }
return null;
} }

Loading…
Cancel
Save