Search in sources :

Example 1 with APIMonetizationUsageDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationUsageDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method getSubscriptionUsage.

/**
 * Get monetization usage data for a subscription
 *
 * @param subscriptionId subscription Id
 * @param messageContext message context
 * @return monetization usage data for a subscription
 */
@Override
public Response getSubscriptionUsage(String subscriptionId, MessageContext messageContext) {
    if (StringUtils.isBlank(subscriptionId)) {
        String errorMessage = "Subscription ID cannot be empty or null when getting monetization usage.";
        RestApiUtil.handleBadRequest(errorMessage, log);
    }
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
        Map<String, String> billingEngineUsageData = monetizationImplementation.getCurrentUsageForSubscription(subscriptionId, apiProvider);
        if (MapUtils.isEmpty(billingEngineUsageData)) {
            String errorMessage = "Billing engine usage data was not found for subscription ID : " + subscriptionId;
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        APIMonetizationUsageDTO apiMonetizationUsageDTO = new APIMonetizationUsageDTO();
        apiMonetizationUsageDTO.setProperties(billingEngineUsageData);
        return Response.ok().entity(apiMonetizationUsageDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Failed to retrieve billing engine usage data for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (MonetizationException e) {
        String errorMessage = "Failed to get current usage for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIMonetizationUsageDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationUsageDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 2 with APIMonetizationUsageDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationUsageDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdUsageGet.

@Override
public Response subscriptionsSubscriptionIdUsageGet(String subscriptionId, MessageContext messageContext) {
    if (StringUtils.isBlank(subscriptionId)) {
        String errorMessage = "Subscription ID cannot be empty or null when getting monetization usage.";
        RestApiUtil.handleBadRequest(errorMessage, log);
    }
    try {
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        Monetization monetizationImplementation = apiConsumer.getMonetizationImplClass();
        Map<String, String> billingEngineUsageData = monetizationImplementation.getCurrentUsageForSubscription(subscriptionId, RestApiCommonUtil.getLoggedInUserProvider());
        if (MapUtils.isEmpty(billingEngineUsageData)) {
            String errorMessage = "Billing engine usage data was not found for subscription ID : " + subscriptionId;
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        APIMonetizationUsageDTO apiMonetizationUsageDTO = new APIMonetizationUsageDTO();
        apiMonetizationUsageDTO.setProperties(billingEngineUsageData);
        return Response.ok().entity(apiMonetizationUsageDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Failed to retrieve billing engine usage data for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (MonetizationException e) {
        String errorMessage = "Failed to get current usage for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) APIMonetizationUsageDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIMonetizationUsageDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)2 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 APIMonetizationUsageDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationUsageDTO)1 APIMonetizationUsageDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIMonetizationUsageDTO)1