mirror of
https://github.com/simonw/dclient.git
synced 2026-07-23 09:24:31 +02:00
Add get command for authenticated GET requests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1b4dc85974
commit
d1dfdbf6db
1 changed files with 35 additions and 0 deletions
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue