Add get command for authenticated GET requests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2026-02-24 14:56:54 -08:00
commit d1dfdbf6db

View file

@ -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")