import asyncio from aiohttp import web from aiohttp_apispec import docs from http import HTTPStatus from apollo_utils.service.utils.isrc import normalize_isrc from server.constants.core import Service from server.legacy.core.deserializers import HealthCheckParams async def health(request: web.Request) -> web.Response: data = HealthCheckParams().validate(request.query, raise_exception=True) include = [i.strip().lower() for i in data["include"]] points_to_check = {Service.DELPHI: request.app["delphi_java_api"].check_health} errors = [] tasks = {} for service_name, check_func in points_to_check.items(): if service_name not in include and Service.ALL not in include: continue tasks[service_name] = check_func() responses = await asyncio.gather(*tasks.values(), return_exceptions=True) for service_name, response in zip(tasks.keys(), responses): if isinstance(response, Exception): errors.append(dict(name=service_name, details=str(response))) elif not response: errors.append(dict(name=service_name)) if errors: return web.json_response(errors, status=HTTPStatus.BAD_GATEWAY) return web.Response(content_type="application/json") @docs(tags=["core"], summary="Make any request to Delphi API.") async def handle_other_requests(request: web.Request) -> web.Response: url = request.match_info.get("endpoint") params = {key: ",".join(request.query.getall(key)) for key in request.query.keys()} if params.get("isrc"): params["isrc"] = normalize_isrc(params["isrc"]) delphi_api = request.app["delphi_java_api"] result = await delphi_api.get(url, params) return web.json_response(result)