|
| 1 | +"""The Server Info API.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from django.http import HttpRequest, JsonResponse |
| 6 | +from django.urls import get_resolver |
| 7 | + |
| 8 | +from ansible_dev_tools.server_utils import validate_request |
| 9 | +from ansible_dev_tools.version_builder import version_builder |
| 10 | + |
| 11 | + |
| 12 | +class GetMetadata: |
| 13 | + """The metadata, returns the available tools with their versions and available API endpoints.""" |
| 14 | + |
| 15 | + def server_info(self, request: HttpRequest) -> JsonResponse: |
| 16 | + """Return server information including versions and available APIs. |
| 17 | +
|
| 18 | + Args: |
| 19 | + request: HttpRequest Object |
| 20 | + Returns: |
| 21 | + JSON response containing tool versions and available API endpoints. |
| 22 | + """ |
| 23 | + validate_request(request) |
| 24 | + versions = {} |
| 25 | + for line in version_builder().splitlines(): |
| 26 | + tool, version = line.split(maxsplit=1) |
| 27 | + versions[tool] = version |
| 28 | + |
| 29 | + resolver = get_resolver() |
| 30 | + urlpatterns = resolver.url_patterns |
| 31 | + |
| 32 | + endpoints = [str(pattern.pattern) for pattern in urlpatterns] |
| 33 | + |
| 34 | + grouped_endpoints: dict[str, list[str]] = {} |
| 35 | + |
| 36 | + for endpoint in endpoints: |
| 37 | + parts = endpoint.split("/") |
| 38 | + key = parts[0] |
| 39 | + if key not in grouped_endpoints: |
| 40 | + grouped_endpoints[key] = [] |
| 41 | + grouped_endpoints[key].append(f"/{endpoint}") |
| 42 | + |
| 43 | + return JsonResponse({"versions": versions, "apis": grouped_endpoints}, status=200) |
0 commit comments