Skip to content

Commit

Permalink
Generate jsonschema for API and CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchrisadams committed Nov 8, 2024
1 parent 3518ebb commit 8e0dda4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/carbon_txt/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import os
import sys

from pathlib import Path

import django
Expand Down Expand Up @@ -106,6 +106,14 @@ def validate_file(
return parsed_result


@app.command()
def schema():
schema = CarbonTxtFile.model_json_schema()

rich.print(json.dumps(schema, indent=2))
return schema


def configure_django(
settings_module: str = "carbon_txt.web.config.settings.development",
):
Expand Down
15 changes: 15 additions & 0 deletions src/carbon_txt/web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ def validate_url(

# Return errors if validation failed
return {"success": False, "errors": result.errors()}


@ninja_api.get(
"/json_schema/",
summary="Retrieve JSON Schema",
description="Get the JSON schema representation of carbon.txt file spec for validation",
)
def get_json_schema(request: HttpRequest) -> HttpResponse:
"""
Endpoint to get the JSON schema for a carbon.txt file.
"""
# Get the JSON schema for a carbon.txt file
schema = CarbonTxtFile.model_json_schema()

return schema
12 changes: 7 additions & 5 deletions tests/test_api_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import httpx
import pytest
import rich


@pytest.mark.parametrize("url_suffix", ["", "/"])
Expand All @@ -12,7 +11,6 @@ def test_hitting_validate_endpoint_ok(
api_url = f"{live_server.url}/api/validate/file{url_suffix}"
data = {"text_contents": shorter_carbon_txt_string}
res = httpx.post(api_url, json=data, follow_redirects=True)
rich.inspect(res)

assert res.status_code == 200

Expand All @@ -27,7 +25,6 @@ def test_hitting_validate_endpoint_fail(live_server, url_suffix):
api_url = f"{live_server.url}/api/validate/file{url_suffix}"
data = {"text_contents": toml_file.read()}
res = httpx.post(api_url, json=data, follow_redirects=True)
rich.inspect(res)

assert res.status_code == 200

Expand All @@ -37,7 +34,6 @@ def test_hitting_validate_url_endpoint_ok(live_server, url_suffix):
api_url = f"{live_server.url}/api/validate/url{url_suffix}"
data = {"url": "https://aremythirdpartiesgreen.com/carbon.txt"}
res = httpx.post(api_url, json=data, follow_redirects=True)
rich.inspect(res)

assert res.status_code == 200

Expand All @@ -47,6 +43,12 @@ def test_hitting_validate_url_endpoint_fail(live_server, url_suffix):
api_url = f"{live_server.url}/api/validate/url{url_suffix}"
data = {"url": "https://aremythirdpartiesgreen.com/carbon.txt"}
res = httpx.post(api_url, json=data, follow_redirects=True)
rich.inspect(res)

assert res.status_code == 200


# TODO do we still need to run this with a full on external server?
def test_hitting_fetch_json_schema(live_server):
api_url = f"{live_server.url}/api/json_schema"
res = httpx.get(api_url, follow_redirects=True)
assert res.status_code == 200
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from typer.testing import CliRunner

from carbon_txt.cli import app
Expand Down Expand Up @@ -33,3 +35,16 @@ def test_lookup_file(self):
)
assert result.exit_code == 0
assert "https://used-in-tests.carbontxt.org" in result.stdout

def test_schema(self):
"""
Run our CLI to `carbontxt schema`, and confirm we
get back the expected JSON Schema representation of our domain objects
"""

result = runner.invoke(app, ["schema"])
parsed_schema = json.loads(result.stdout)

assert result.exit_code == 0
assert "CarbonTxtFile" in parsed_schema.get("title")
assert "$defs" in parsed_schema.keys()

0 comments on commit 8e0dda4

Please sign in to comment.