users.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import json
  2. import asyncclick as click
  3. from asyncclick import pass_context
  4. from cli.utils.timer import timer
  5. from r2r import R2RAsyncClient
  6. @click.group()
  7. def users():
  8. """Users commands."""
  9. pass
  10. @users.command()
  11. @click.argument("email", required=True, type=str)
  12. @click.argument("password", required=True, type=str)
  13. @pass_context
  14. async def create(ctx, email, password):
  15. """Create a new user."""
  16. client: R2RAsyncClient = ctx.obj
  17. with timer():
  18. response = await client.users.create(email=email, password=password)
  19. click.echo(json.dumps(response, indent=2))
  20. @users.command()
  21. @click.option("--ids", multiple=True, help="Document IDs to fetch")
  22. @click.option(
  23. "--offset",
  24. default=0,
  25. help="The offset to start from. Defaults to 0.",
  26. )
  27. @click.option(
  28. "--limit",
  29. default=100,
  30. help="The maximum number of nodes to return. Defaults to 100.",
  31. )
  32. @pass_context
  33. async def list(ctx, ids, offset, limit):
  34. """Get an overview of users."""
  35. client: R2RAsyncClient = ctx.obj
  36. ids = list(ids) if ids else None
  37. with timer():
  38. response = await client.users.list(
  39. ids=ids,
  40. offset=offset,
  41. limit=limit,
  42. )
  43. for user in response["results"]:
  44. click.echo(json.dumps(user, indent=2))
  45. @users.command()
  46. @click.argument("id", required=True, type=str)
  47. @pass_context
  48. async def retrieve(ctx, id):
  49. """Retrieve a user by ID."""
  50. client: R2RAsyncClient = ctx.obj
  51. with timer():
  52. response = await client.users.retrieve(id=id)
  53. click.echo(json.dumps(response, indent=2))
  54. @users.command()
  55. @pass_context
  56. async def me(ctx):
  57. """Retrieve the current user."""
  58. client: R2RAsyncClient = ctx.obj
  59. with timer():
  60. response = await client.users.me()
  61. click.echo(json.dumps(response, indent=2))
  62. @users.command()
  63. @click.argument("id", required=True, type=str)
  64. @click.option(
  65. "--offset",
  66. default=0,
  67. help="The offset to start from. Defaults to 0.",
  68. )
  69. @click.option(
  70. "--limit",
  71. default=100,
  72. help="The maximum number of nodes to return. Defaults to 100.",
  73. )
  74. @pass_context
  75. async def list_collections(ctx, id, offset, limit):
  76. """List collections for a specific user."""
  77. client: R2RAsyncClient = ctx.obj
  78. with timer():
  79. response = await client.users.list_collections(
  80. id=id,
  81. offset=offset,
  82. limit=limit,
  83. )
  84. for collection in response["results"]:
  85. click.echo(json.dumps(collection, indent=2))
  86. @users.command()
  87. @click.argument("id", required=True, type=str)
  88. @click.argument("collection_id", required=True, type=str)
  89. @pass_context
  90. async def add_to_collection(ctx, id, collection_id):
  91. """Retrieve a user by ID."""
  92. client: R2RAsyncClient = ctx.obj
  93. with timer():
  94. response = await client.users.add_to_collection(
  95. id=id,
  96. collection_id=collection_id,
  97. )
  98. click.echo(json.dumps(response, indent=2))
  99. @users.command()
  100. @click.argument("id", required=True, type=str)
  101. @click.argument("collection_id", required=True, type=str)
  102. @pass_context
  103. async def remove_from_collection(ctx, id, collection_id):
  104. """Retrieve a user by ID."""
  105. client: R2RAsyncClient = ctx.obj
  106. with timer():
  107. response = await client.users.remove_from_collection(
  108. id=id,
  109. collection_id=collection_id,
  110. )
  111. click.echo(json.dumps(response, indent=2))