# Path: services/mgmt.py
# def get_available_services(component=None, exclude=None, include=None):
#     """
#     Get all available services which implement a particular component (or all services if
#     'component' is None). Allows excluding particular services and restricting those from which to filter.
#     :param component: component (mixin) that should be implemented by returned services
#     :param exclude: exclude services passed through this parameter (should be a list of service names)
#     :param include: consider only services passed through this parameter (should be a list of service names)
#     :return: list of matching services (can be empty)
#     """
#     out_services = list()
#     for service in available_services:
#         if component is not None:
#             if component not in service.implemented_components:
#                 continue
#         if exclude is not None:
#             if service.name in exclude:
#                 continue
#         if include is not None:
#             if service.name not in include:
#                 continue
#         out_services.append(service)
#     return out_services
# 
# def get_service_by_id(service_id):
#     for service in available_services:
#         if service.id == service_id:
#             return service
#     raise ACServiceDoesNotExist('Service with id {0} does not exist'.format(service_id))
# 
# Path: api/response_aggregator.py
# def get_response_aggregator():
#     return response_aggregator


from django.conf import settings
from ac_mediator.exceptions import *
from services.acservice.constants import *
from services.mgmt import get_available_services, get_service_by_id
from api.response_aggregator import get_response_aggregator
from celery import shared_task
        # Aggregate error response in response aggregator and continue with next service
        response_aggregator.aggregate_response(response_id, service.name, e)


class RequestDistributor(object):

    @staticmethod
    def process_request(request, wait_until_complete=False, include=None, exclude=None, acid_domain=None):
        """
        Process incoming request, and propagate it to corresponding services.
        Requests to 3rd party services are made asynchronously. We send requests to all 3rd
        party services and as soon as a response is received it is added to a response object
        which aggregates the responses from all services.
        In the normal functioning mode (wait_until_complete=False) this method will immediately
        return a response right after all requests have been sent. This response will mainly
        include a response_id parameter that can be later used to pull the actual responses
        from the 3rd party services (see ResponseAggregator.collect_response).
        If wait_until_complete is set to False, then this method will wait untill a response is
        received for all requests and only then will return an aggregated response including the
        contents of all 3rd party services individual responses.

        :param request: incoming request object
        :param wait_until_complete: whether to return immediately after all requests are sent or wait untill all responses are received
        :param include: list of service names to include when getting available services
        :param include: list of service names to exclude when getting available services
        :param acid_domain: domain of Audio Commons Unique Identifiers to be considered for matching available services
        :return: dictionary with response (as returned by ResponseAggregator.collect_response)
        """

        # Get available services for the given component (e.g. services that do `text search')

# Path: services/mgmt.py
# def get_available_services(component=None, exclude=None, include=None):
#     """
#     Get all available services which implement a particular component (or all services if
#     'component' is None). Allows excluding particular services and restricting those from which to filter.
#     :param component: component (mixin) that should be implemented by returned services
#     :param exclude: exclude services passed through this parameter (should be a list of service names)
#     :param include: consider only services passed through this parameter (should be a list of service names)
#     :return: list of matching services (can be empty)
#     """
#     out_services = list()
#     for service in available_services:
#         if component is not None:
#             if component not in service.implemented_components:
#                 continue
#         if exclude is not None:
#             if service.name in exclude:
#                 continue
#         if include is not None:
#             if service.name not in include:
#                 continue
#         out_services.append(service)
#     return out_services
# 
# def get_service_by_id(service_id):
#     for service in available_services:
#         if service.id == service_id:
#             return service
#     raise ACServiceDoesNotExist('Service with id {0} does not exist'.format(service_id))
# 
# Path: api/response_aggregator.py
# def get_response_aggregator():
#     return response_aggregator


# Path: api/request_distributor.py
from django.conf import settings
from ac_mediator.exceptions import *
from services.acservice.constants import *
from services.mgmt import get_available_services, get_service_by_id
from api.response_aggregator import get_response_aggregator
from celery import shared_task

        # Aggregate error response in response aggregator and continue with next service
        response_aggregator.aggregate_response(response_id, service.name, e)


class RequestDistributor(object):

    @staticmethod
    def process_request(request, wait_until_complete=False, include=None, exclude=None, acid_domain=None):
        """
        Process incoming request, and propagate it to corresponding services.
        Requests to 3rd party services are made asynchronously. We send requests to all 3rd
        party services and as soon as a response is received it is added to a response object
        which aggregates the responses from all services.
        In the normal functioning mode (wait_until_complete=False) this method will immediately
        return a response right after all requests have been sent. This response will mainly
        include a response_id parameter that can be later used to pull the actual responses
        from the 3rd party services (see ResponseAggregator.collect_response).
        If wait_until_complete is set to False, then this method will wait untill a response is
        received for all requests and only then will return an aggregated response including the
        contents of all 3rd party services individual responses.

        :param request: incoming request object
        :param wait_until_complete: whether to return immediately after all requests are sent or wait untill all responses are received
        :param include: list of service names to include when getting available services
        :param include: list of service names to exclude when getting available services
        :param acid_domain: domain of Audio Commons Unique Identifiers to be considered for matching available services
        :return: dictionary with response (as returned by ResponseAggregator.collect_response)
        """

        # Get available services for the given component (e.g. services that do `text search')

        services = get_available_services(component=request['component'], include=include, exclude=exclude)