Search in sources :

Example 1 with APIRevenueDTO

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;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIRevenueDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevenueDTO) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)1 API (org.wso2.carbon.apimgt.api.model.API)1 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1 APIRevenueDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevenueDTO)1