Files
Scrobbler/src/scrobbler/api/__init__.py
T
mattandClaude Opus 5.5 eebd7b8730
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 47s
CI / python-package (push) Skipped
CI / assets (push) Successful in 8s
CI / images (push) Successful in 31s
CI / notify (push) Successful in 6s
Imports API: upload exports, pull from Last.fm, follow progress
- /api/v1/imports/options lists the available sources; `lastfm` only
  appears when the server has a Last.fm API key
- POST /imports/file (multipart; format detected or forced with
  ?format=), POST /imports/lastfm, GET /imports, GET /imports/{id},
  POST /imports/{id}/cancel
- Upload size limit (IMPORT_MAX_BYTES, default 200 MB) with a 413
- Documented in OpenAPI under the Imports tag

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 15:04:15 +01:00

59 lines
1.9 KiB
Python

"""JSON REST API used by the web UI, mounted at /api/v1."""
from flask import Flask, jsonify
from flask_smorest import Api
from werkzeug.exceptions import HTTPException
from scrobbler.schemas import ErrorSchema
BEARER = [{"bearerAuth": []}]
class ScrobblerApi(Api):
ERROR_SCHEMA = ErrorSchema
def handle_http_exception(self, error: HTTPException):
"""Render every HTTP error as {"error": {"code", "message", "details"?}}."""
data = getattr(error, "data", None) or {}
body = {
"code": data.get("code") or error.name.lower().replace(" ", "_"),
"message": data.get("message") or error.description,
}
details = data.get("errors") or data.get("messages")
if details:
body["details"] = details
response = jsonify({"error": body})
response.status_code = error.code or 500
for name, value in (data.get("headers") or {}).items():
response.headers[name] = value
return response
api = ScrobblerApi()
def init_app(app: Flask) -> None:
api.init_app(app)
api.spec.components.security_scheme(
"bearerAuth",
{
"type": "http",
"scheme": "bearer",
"description": "Token from POST /api/v1/auth/login",
},
)
from scrobbler.api import apps, auth, health, imports, sessions, stats, tokens
api.register_blueprint(auth.blp, url_prefix="/api/v1/auth")
api.register_blueprint(apps.blp, url_prefix="/api/v1/apps")
api.register_blueprint(sessions.blp, url_prefix="/api/v1/sessions")
api.register_blueprint(tokens.blp, url_prefix="/api/v1/tokens")
api.register_blueprint(stats.blp, url_prefix="/api/v1/me")
api.register_blueprint(imports.blp, url_prefix="/api/v1/imports")
api.register_blueprint(health.blp)
from scrobbler.openapi import lastfm
lastfm.register(app, api)