123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import json
- import asyncclick as click
- from asyncclick import pass_context
- from cli.utils.timer import timer
- from r2r import R2RAsyncClient
- @click.group()
- def users():
- """Users commands."""
- pass
- @users.command()
- @click.argument("email", required=True, type=str)
- @click.argument("password", required=True, type=str)
- @pass_context
- async def create(ctx, email, password):
- """Create a new user."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.create(email=email, password=password)
- click.echo(json.dumps(response, indent=2))
- @users.command()
- @click.option("--ids", multiple=True, help="Document IDs to fetch")
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of nodes to return. Defaults to 100.",
- )
- @pass_context
- async def list(ctx, ids, offset, limit):
- """Get an overview of users."""
- client: R2RAsyncClient = ctx.obj
- ids = list(ids) if ids else None
- with timer():
- response = await client.users.list(
- ids=ids,
- offset=offset,
- limit=limit,
- )
- for user in response["results"]:
- click.echo(json.dumps(user, indent=2))
- @users.command()
- @click.argument("id", required=True, type=str)
- @pass_context
- async def retrieve(ctx, id):
- """Retrieve a user by ID."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.retrieve(id=id)
- click.echo(json.dumps(response, indent=2))
- @users.command()
- @pass_context
- async def me(ctx):
- """Retrieve the current user."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.me()
- click.echo(json.dumps(response, indent=2))
- @users.command()
- @click.argument("id", required=True, type=str)
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of nodes to return. Defaults to 100.",
- )
- @pass_context
- async def list_collections(ctx, id, offset, limit):
- """List collections for a specific user."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.list_collections(
- id=id,
- offset=offset,
- limit=limit,
- )
- for collection in response["results"]:
- click.echo(json.dumps(collection, indent=2))
- @users.command()
- @click.argument("id", required=True, type=str)
- @click.argument("collection_id", required=True, type=str)
- @pass_context
- async def add_to_collection(ctx, id, collection_id):
- """Retrieve a user by ID."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.add_to_collection(
- id=id,
- collection_id=collection_id,
- )
- click.echo(json.dumps(response, indent=2))
- @users.command()
- @click.argument("id", required=True, type=str)
- @click.argument("collection_id", required=True, type=str)
- @pass_context
- async def remove_from_collection(ctx, id, collection_id):
- """Retrieve a user by ID."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.users.remove_from_collection(
- id=id,
- collection_id=collection_id,
- )
- click.echo(json.dumps(response, indent=2))
|