use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevenueDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIRevenue.
/**
* Get total revenue for a given API from all its' subscriptions
*
* @param apiId API ID
* @param messageContext message context
* @return revenue data for a given API
*/
@Override
public Response getAPIRevenue(String apiId, MessageContext messageContext) {
if (StringUtils.isBlank(apiId)) {
String errorMessage = "API ID cannot be empty or null when getting revenue details.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
API api = apiProvider.getAPIbyUUID(apiId, organization);
if (!APIConstants.PUBLISHED.equalsIgnoreCase(api.getStatus())) {
String errorMessage = "API " + api.getId().getName() + " should be in published state to get total revenue.";
RestApiUtil.handleBadRequest(errorMessage, log);
}
Map<String, String> revenueUsageData = monetizationImplementation.getTotalRevenue(api, apiProvider);
APIRevenueDTO apiRevenueDTO = new APIRevenueDTO();
apiRevenueDTO.setProperties(revenueUsageData);
return Response.ok().entity(apiRevenueDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Failed to retrieve revenue data for API ID : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (MonetizationException e) {
String errorMessage = "Failed to get current revenue data for API ID : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
Aggregations