-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/devel
- Loading branch information
Showing
12 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""The Server Info API.""" | ||
|
||
from __future__ import annotations | ||
|
||
from django.http import HttpRequest, JsonResponse | ||
from django.urls import get_resolver | ||
|
||
from ansible_dev_tools.server_utils import validate_request | ||
from ansible_dev_tools.version_builder import version_builder | ||
|
||
|
||
class GetMetadata: | ||
"""The metadata, returns the available tools with their versions and available API endpoints.""" | ||
|
||
def server_info(self, request: HttpRequest) -> JsonResponse: | ||
"""Return server information including versions and available APIs. | ||
Args: | ||
request: HttpRequest Object | ||
Returns: | ||
JSON response containing tool versions and available API endpoints. | ||
""" | ||
validate_request(request) | ||
versions = {} | ||
for line in version_builder().splitlines(): | ||
tool, version = line.split(maxsplit=1) | ||
versions[tool] = version | ||
|
||
resolver = get_resolver() | ||
urlpatterns = resolver.url_patterns | ||
|
||
endpoints = [str(pattern.pattern) for pattern in urlpatterns] | ||
|
||
grouped_endpoints: dict[str, list[str]] = {} | ||
|
||
for endpoint in endpoints: | ||
parts = endpoint.split("/") | ||
key = parts[0] | ||
if key not in grouped_endpoints: | ||
grouped_endpoints[key] = [] | ||
grouped_endpoints[key].append(f"/{endpoint}") | ||
|
||
return JsonResponse({"versions": versions, "apis": grouped_endpoints}, status=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Test the dev tools server for metadata.""" | ||
|
||
from __future__ import annotations | ||
|
||
import requests | ||
|
||
|
||
def test_metadata(server_url: str) -> None: | ||
"""Test the server info endpoint. | ||
Args: | ||
server_url: The server URL. | ||
""" | ||
endpoint = f"{server_url}/metadata" | ||
|
||
response = requests.get(endpoint, timeout=10) | ||
|
||
expected_response_code = 200 | ||
assert ( | ||
response.status_code == expected_response_code | ||
), f"Expected status code 200 but got {response.status_code}" | ||
|
||
assert response.headers["Content-Type"] == "application/json" | ||
|
||
data = response.json() | ||
|
||
assert "versions" in data, "Response is missing 'versions' key" | ||
assert "apis" in data, "Response is missing 'apis' key" | ||
|
||
assert len(data["versions"]) > 0, "Versions should contain at least one package" | ||
|
||
assert len(data["apis"]) > 0, "APIs should contain at least one endpoint" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters