Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ python run.py

Open http://localhost:5000 in your browser.

## API documentation

- [Public API reference](docs/api/index.html) - authentication, pagination,
structured errors, webhook verification, and the registered endpoints.
- [OpenAPI 3.1 spec](docs/openapi.json) - regenerate both with
`python scripts/render_api_docs.py`.

The reference is served by the running app at `/api/docs` and is linked from
the footer. Only `/api/v1` resources have frozen response schemas; other listed
paths keep their existing web-UI contracts.

## Who it's for

- Developers who want AI help grounded in their actual codebase
Expand Down
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def create_app(config_name: str | None = None) -> Flask:

# Register blueprints.
from app.admin import bp as admin_bp
from app.api.docs import bp as api_docs_bp
from app.api.v1 import bp as api_v1_bp
from app.auth import bp as auth_bp
from app.billing import bp as billing_bp
from app.chat import bp as chat_bp
Expand Down Expand Up @@ -130,6 +132,8 @@ def create_app(config_name: str | None = None) -> Flask:
app.register_blueprint(main_bp)
app.register_blueprint(chat_bp)
app.register_blueprint(chat_api_bp)
app.register_blueprint(api_docs_bp)
app.register_blueprint(api_v1_bp)
app.register_blueprint(prompts_bp)
app.register_blueprint(tools_bp)
app.register_blueprint(github_bp)
Expand Down
16 changes: 16 additions & 0 deletions app/api/docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Serve the generated public API reference (#655)."""

from pathlib import Path

from flask import Blueprint, send_from_directory

bp = Blueprint("api_docs", __name__, url_prefix="/api")

DOCS_DIR = Path(__file__).resolve().parents[2] / "docs" / "api"


@bp.route("/docs")
@bp.route("/docs/")
def api_docs():
"""Return the static reference generated by ``scripts/render_api_docs.py``."""
return send_from_directory(DOCS_DIR, "index.html")
190 changes: 190 additions & 0 deletions app/api/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
"""Stable public API v1 resources (#646).

GET /api/v1/projects list the caller's projects
GET /api/v1/projects/<id> one project
GET /api/v1/reviews list the caller's reviews
GET /api/v1/reviews/<id> one review
GET /api/v1/usage bounded usage-ledger page

Authorization matches the web UI exactly: projects and reviews are scoped to
``current_user.id`` and another user's row is reported as ``404`` (never as a
403 existence oracle). Usage is always scoped to the authenticated user.
Errors use the shared code catalog via :func:`error_response`.
"""

from __future__ import annotations

import functools

from flask import Blueprint, jsonify, request
from flask_login import current_user

from app.api.error_catalog import INVALID_INPUT, NOT_FOUND, UNAUTHORIZED, error_response
from app.models import Project, Review
from app.services import pagination, usage_ledger

bp = Blueprint("api_v1", __name__, url_prefix="/api/v1")


def _login_required(view):
@functools.wraps(view)
def wrapper(*args, **kwargs):
if not current_user.is_authenticated:
return error_response(UNAUTHORIZED)
return view(*args, **kwargs)

return wrapper


def _page_request():
"""Validate page/limit/cursor, returning ``(request, error_response)``."""
try:
return pagination.parse_page_request(request.args), None
except pagination.PaginationError as exc:
return None, error_response(INVALID_INPUT, str(exc))


def _cursor_id(page) -> int | None:
"""Return the opaque cursor's ``id``, or ``None`` when absent."""
if page.cursor is None:
return None
if set(page.cursor) != {"id"}:
raise pagination.PaginationError("Invalid cursor.")
value = page.cursor["id"]
if isinstance(value, bool) or not isinstance(value, int):
raise pagination.PaginationError("Invalid cursor.")
return value


def _paginate(query, model, page):
"""Return ``(rows, total, next_cursor)`` for a stable id-ordered list."""
total = query.count()
cursor_id = _cursor_id(page)
query = query.order_by(model.id.desc())
if cursor_id is not None:
query = query.filter(model.id < cursor_id)
else:
query = query.offset((page.page - 1) * page.limit)
rows = query.limit(page.limit + 1).all()
has_more = len(rows) > page.limit
rows = rows[: page.limit]
next_cursor = pagination.encode_cursor({"id": rows[-1].id}) if has_more and rows else None
return rows, total, next_cursor


def _list_payload(items, total, page, next_cursor):
return {
"items": items,
"total": total,
"page": page.page,
"limit": page.limit,
"next_cursor": next_cursor,
}


def _optional_int(name: str):
"""Parse an optional integer query argument, or return ``(None, error)``."""
raw = request.args.get(name)
if raw in (None, ""):
return None, None
try:
return int(raw), None
except (TypeError, ValueError):
return None, error_response(INVALID_INPUT, f"{name} must be an integer.")


@bp.route("/projects", methods=["GET"])
@_login_required
def list_projects():
"""List the caller's projects, newest id first."""
page, error = _page_request()
if error is not None:
return error

workspace_id, error = _optional_int("workspace_id")
if error is not None:
return error
query = Project.query.filter_by(user_id=current_user.id)
if workspace_id is not None:
query = query.filter_by(workspace_id=workspace_id)
try:
rows, total, next_cursor = _paginate(query, Project, page)
except pagination.PaginationError as exc:
return error_response(INVALID_INPUT, str(exc))
return jsonify(_list_payload([row.to_dict() for row in rows], total, page, next_cursor))


@bp.route("/projects/<int:project_id>", methods=["GET"])
@_login_required
def get_project(project_id: int):
"""Return one project owned by the caller."""
project = Project.query.filter_by(id=project_id, user_id=current_user.id).first()
if project is None:
return error_response(NOT_FOUND)
return jsonify(project.to_dict())


@bp.route("/reviews", methods=["GET"])
@_login_required
def list_reviews():
"""List the caller's reviews, newest id first."""
page, error = _page_request()
if error is not None:
return error

query = Review.query.filter_by(user_id=current_user.id)
for name in ("source", "kind", "status"):
value = request.args.get(name)
if value:
query = query.filter_by(**{name: value})
project_id, error = _optional_int("project_id")
if error is not None:
return error
if project_id is not None:
query = query.filter_by(project_id=project_id)

try:
rows, total, next_cursor = _paginate(query, Review, page)
except pagination.PaginationError as exc:
return error_response(INVALID_INPUT, str(exc))
return jsonify(_list_payload([row.to_dict() for row in rows], total, page, next_cursor))


@bp.route("/reviews/<int:review_id>", methods=["GET"])
@_login_required
def get_review(review_id: int):
"""Return one review owned by the caller."""
review = Review.query.filter_by(id=review_id, user_id=current_user.id).first()
if review is None:
return error_response(NOT_FOUND)
return jsonify(review.to_dict())


@bp.route("/usage", methods=["GET"])
@_login_required
def list_usage():
"""Return a bounded page of the caller's usage-ledger records."""
page, error = _page_request()
if error is not None:
return error
if page.cursor is not None:
return error_response(
INVALID_INPUT, "Cursor pagination is not supported here; use page and limit."
)

workspace_id, error = _optional_int("workspace_id")
if error is not None:
return error
project_id, error = _optional_int("project_id")
if error is not None:
return error

payload = usage_ledger.export_payload(
user_id=current_user.id,
workspace_id=workspace_id,
project_id=project_id,
source=request.args.get("source") or None,
page=page.page,
limit=page.limit,
)
return jsonify(payload)
Loading
Loading