|
| 1 | +# ===----------------------------------------------------------------------===// |
| 2 | +# |
| 3 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +# See https://llvm.org/LICENSE.txt for license information. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +# |
| 7 | +# ===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +import json |
| 10 | +from abc import ABC, abstractmethod |
| 11 | +from typing import Dict, Any, Optional |
| 12 | + |
| 13 | + |
| 14 | +class BaseEndpoint(ABC): |
| 15 | + def __init__(self, data_dir: str, collector): |
| 16 | + self.data_dir = data_dir |
| 17 | + self.collector = collector |
| 18 | + self._cache = {} |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def handle(self, path_parts: list, query_params: Dict[str, list]) -> Dict[str, Any]: |
| 22 | + pass |
| 23 | + |
| 24 | + def get_compilation_units(self): |
| 25 | + if "units" not in self._cache: |
| 26 | + self._cache["units"] = self.collector.discover_compilation_units( |
| 27 | + self.data_dir |
| 28 | + ) |
| 29 | + return self._cache["units"] |
| 30 | + |
| 31 | + def get_parsed_data(self): |
| 32 | + if "parsed_data" not in self._cache: |
| 33 | + self._cache["parsed_data"] = self.collector.parse_all_units(self.data_dir) |
| 34 | + return self._cache["parsed_data"] |
| 35 | + |
| 36 | + def clear_cache(self): |
| 37 | + self._cache.clear() |
| 38 | + |
| 39 | + |
| 40 | +class APIResponse: |
| 41 | + @staticmethod |
| 42 | + def success(data: Any, status: int = 200) -> Dict[str, Any]: |
| 43 | + return {"success": True, "data": data, "status": status} |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def error(message: str, status: int = 400) -> Dict[str, Any]: |
| 47 | + return {"success": False, "error": message, "status": status} |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def not_found(resource: str) -> Dict[str, Any]: |
| 51 | + return APIResponse.error(f"{resource} not found", 404) |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def invalid_request(message: str) -> Dict[str, Any]: |
| 55 | + return APIResponse.error(f"Invalid request: {message}", 400) |
0 commit comments