Add JSON REST API for MCP integration
Build, Lint, and Test / Build, Lint, and Test (push) Failing after 13m0s

- Add /api/v1 namespace with Bearer token auth (PINCHFLAT_API_TOKEN env var)
- API controllers for sources, media items, media profiles, settings, tasks
- Support for all source actions: create, update, delete, force_download_pending,
  force_redownload, force_index, force_metadata_refresh, sync_files_on_disk
- Media item endpoints: list, show, search, force_download, update, delete
- Media profile CRUD endpoints
- Settings show/update and app_info endpoints
- Task listing endpoints
- Add Jason.Encoder for Task schema
- Comprehensive test suite for all API endpoints
- Gitea Actions CI workflow (runs existing checks + API-specific tests)
- API documentation in docs/API.md
This commit is contained in:
2026-07-03 23:22:24 +00:00
parent 67d8bd5598
commit 172c1ff264
19 changed files with 1122 additions and 1 deletions
@@ -0,0 +1,118 @@
defmodule PinchflatWeb.Api.V1.ApiSourceController do
@moduledoc """
JSON API controller for Sources.
"""
use PinchflatWeb, :controller
alias Pinchflat.Repo
alias Pinchflat.Sources
alias Pinchflat.Sources.Source
alias Pinchflat.Tasks
alias Pinchflat.Downloading.DownloadingHelpers
alias Pinchflat.SlowIndexing.SlowIndexingHelpers
alias Pinchflat.Metadata.SourceMetadataStorageWorker
alias Pinchflat.Media.FileSyncingWorker
alias Pinchflat.Sources.SourceDeletionWorker
def index(conn, _params) do
sources = Sources.list_sources() |> Repo.preload(:media_profile)
json(conn, %{data: sources})
end
def show(conn, %{"id" => id}) do
source = Sources.get_source!(id) |> Repo.preload([:media_profile, :media_items])
pending_tasks =
source
|> Tasks.list_tasks_for(nil, [:executing, :available, :scheduled, :retryable])
|> Repo.preload(:job)
json(conn, %{data: source, pending_tasks: pending_tasks})
end
def create(conn, %{"source" => source_params}) do
case Sources.create_source(source_params) do
{:ok, source} ->
source = Repo.preload(source, :media_profile)
conn
|> put_status(:created)
|> json(%{data: source})
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(:unprocessable_entity)
|> json(%{errors: format_changeset_errors(changeset)})
end
end
def update(conn, %{"id" => id, "source" => source_params}) do
source = Sources.get_source!(id)
case Sources.update_source(source, source_params) do
{:ok, source} ->
source = Repo.preload(source, :media_profile)
json(conn, %{data: source})
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(:unprocessable_entity)
|> json(%{errors: format_changeset_errors(changeset)})
end
end
def delete(conn, %{"id" => id} = params) do
delete_files = Map.get(params, "delete_files", "false") == "true"
source = Sources.get_source!(id)
{:ok, _} = Sources.update_source(source, %{marked_for_deletion_at: DateTime.utc_now()})
SourceDeletionWorker.kickoff(source, %{delete_files: delete_files})
conn
|> put_status(:ok)
|> json(%{data: %{id: source.id, message: "Source deletion started."}})
end
def force_download_pending(conn, %{"source_id" => id}) do
source = Sources.get_source!(id)
DownloadingHelpers.enqueue_pending_download_tasks(source)
json(conn, %{data: %{message: "Forcing download of pending media items."}})
end
def force_redownload(conn, %{"source_id" => id}) do
source = Sources.get_source!(id)
DownloadingHelpers.kickoff_redownload_for_existing_media(source)
json(conn, %{data: %{message: "Forcing re-download of downloaded media items."}})
end
def force_index(conn, %{"source_id" => id}) do
source = Sources.get_source!(id)
SlowIndexingHelpers.kickoff_indexing_task(source, %{force: true})
json(conn, %{data: %{message: "Index enqueued."}})
end
def force_metadata_refresh(conn, %{"source_id" => id}) do
source = Sources.get_source!(id)
SourceMetadataStorageWorker.kickoff_with_task(source)
json(conn, %{data: %{message: "Metadata refresh enqueued."}})
end
def sync_files_on_disk(conn, %{"source_id" => id}) do
source = Sources.get_source!(id)
FileSyncingWorker.kickoff_with_task(source)
json(conn, %{data: %{message: "File sync enqueued."}})
end
defp format_changeset_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", to_string(value))
end)
end)
end
end