Add the schulcloud CLI, and document the split
The CLI talks only to the Pi's /api surface and holds no Schulcloud credential — only the same bearer token the Claude connector uses. That is not layering for its own sake: a Schulcloud session dies after two hours idle and a CLI process lives for seconds, so a CLI with its own token would be dead most times you reached for it. Routing through the Pi means one session, one keepalive, one monthly cookie paste. sync is a one-way mirror, which follows from the data rather than from scope-cutting: file records are immutable upstream, so there is no versioning, no conflict resolution and no merge. State is keyed by file record id with the path as derived output, so an upstream rename moves the local file instead of duplicating it — verified against the live server. Verification is size-only because the download endpoint exposes no ETag and Schulcloud publishes no hash; size still catches the failure that happens, a truncated download. Downloads land on a .part neighbour and are renamed, so an interrupted run leaves no half-file that a later run mistakes for complete. Deletions are reported but not propagated — a teacher removing a worksheet is no reason to destroy the student's copy — with --prune to opt in. what_changed now clamps to the oldest stored generation instead of refusing, and says it did: "what's new this week" is a reasonable question to ask a two-day-old index. Two build bugs caught by the checks rather than by luck: the smoke harness constructed the app without services, so the index-backed tools were never exercised; and the Docker build could not see scripts/copy-assets.mjs, so the image would have shipped without migrations and silently degraded to live-only. 67 unit tests (9 needing Postgres), smoke green both ways — 34 checks with an index, 32 without, because graceful degradation is a supported mode and not a fallback nobody runs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
94
src/cli/client.ts
Normal file
94
src/cli/client.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import type { CliConfig } from './config.ts';
|
||||
|
||||
/**
|
||||
* Talks to the schulcloud-mcp server's /api surface.
|
||||
*
|
||||
* Deliberately the only thing in the CLI that knows a network exists, and it
|
||||
* never touches Schulcloud directly — the Pi holds that credential.
|
||||
*/
|
||||
|
||||
export interface ManifestEntry {
|
||||
fileId: string;
|
||||
name: string;
|
||||
path: string;
|
||||
size: number;
|
||||
mimeType: string;
|
||||
courseId: string | null;
|
||||
courseTitle: string;
|
||||
status: 'added' | 'unchanged' | 'removed';
|
||||
}
|
||||
|
||||
export interface Manifest {
|
||||
crawlId: number;
|
||||
cursor: string;
|
||||
crawledAt?: string;
|
||||
count: number;
|
||||
entries: ManifestEntry[];
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
readonly status: number;
|
||||
|
||||
constructor(status: number, message: string) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiClient {
|
||||
private readonly config: CliConfig;
|
||||
|
||||
constructor(config: CliConfig) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private async request(path: string, init: RequestInit = {}): Promise<Response> {
|
||||
const response = await fetch(`${this.config.server}${path}`, {
|
||||
...init,
|
||||
headers: { ...(init.headers ?? {}), Authorization: `Bearer ${this.config.token}` },
|
||||
});
|
||||
if (!response.ok) {
|
||||
let detail = '';
|
||||
try {
|
||||
const body = (await response.json()) as { message?: string; error?: string };
|
||||
detail = body.message ?? body.error ?? '';
|
||||
} catch {
|
||||
// Non-JSON error bodies are not worth surfacing verbatim.
|
||||
}
|
||||
throw new ApiError(response.status, describe(response.status, detail, this.config.server));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
async status(): Promise<Record<string, unknown>> {
|
||||
return (await (await this.request('/api/status')).json()) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
async manifest(since?: string): Promise<Manifest> {
|
||||
const query = since ? `?since=${encodeURIComponent(since)}` : '';
|
||||
return (await (await this.request(`/api/manifest${query}`)).json()) as Manifest;
|
||||
}
|
||||
|
||||
async refresh(courseId?: string, force = false): Promise<Record<string, unknown>> {
|
||||
const response = await this.request('/api/refresh', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ courseId, force }),
|
||||
});
|
||||
return (await response.json()) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
/** Streams one file's bytes. */
|
||||
async file(fileId: string): Promise<Response> {
|
||||
return this.request(`/api/files/${encodeURIComponent(fileId)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function describe(status: number, detail: string, server: string): string {
|
||||
if (status === 401) return `Unauthorized — the token is wrong or expired. Re-run: schulcloud login --server ${server} --token <token>`;
|
||||
if (status === 503) return 'The server is running without an index, so this command is unavailable. Set DATABASE_URL on the server.';
|
||||
if (status === 409) return detail || 'The sync cursor is unknown to the server. Run a full sync with --full.';
|
||||
if (status === 429) return detail || 'Refreshed too recently — wait a moment, or pass --force.';
|
||||
return detail ? `HTTP ${status}: ${detail}` : `HTTP ${status}`;
|
||||
}
|
||||
Reference in New Issue
Block a user