From d1dfdbf6dbbd204855d9df07901784171354e1bf Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 24 Feb 2026 14:56:54 -0800 Subject: [PATCH] Add get command for authenticated GET requests Co-Authored-By: Claude Opus 4.6 --- dclient/cli.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/dclient/cli.py b/dclient/cli.py index 91f92ce..f2e6396 100644 --- a/dclient/cli.py +++ b/dclient/cli.py @@ -21,6 +21,41 @@ def cli(): "A client CLI utility for Datasette instances" +@cli.command() +@click.argument("path") +@click.option("--instance", default=None, help="Datasette URL or alias") +@click.option("--token", help="API token") +def get(path, instance, token): + """ + Make an authenticated GET request to a Datasette instance + + Example usage: + + \b + dclient get /-/plugins.json + dclient get /data/creatures.json --instance https://my.datasette.io + """ + url = _resolve_url(instance) + token = _resolve_token(token, url) + headers = {} + if token: + headers["Authorization"] = f"Bearer {token}" + full_url = url.rstrip("/") + "/" + path.lstrip("/") + response = httpx.get( + full_url, + headers=headers, + follow_redirects=True, + timeout=30.0, + ) + if response.status_code != 200: + raise click.ClickException(f"{response.status_code} error for {full_url}") + # Pretty-print if JSON, otherwise raw + if "json" in response.headers.get("content-type", ""): + click.echo(json.dumps(response.json(), indent=2)) + else: + click.echo(response.text) + + @cli.command() @click.argument("url_or_alias") @click.argument("sql")