users.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import json
  2. from builtins import list as _list
  3. from uuid import UUID
  4. import asyncclick as click
  5. from asyncclick import pass_context
  6. from cli.utils.timer import timer
  7. from r2r import R2RAsyncClient, R2RException
  8. @click.group()
  9. def users():
  10. """Users commands."""
  11. pass
  12. @users.command()
  13. @click.argument("email", required=True, type=str)
  14. @click.argument("password", required=True, type=str)
  15. @pass_context
  16. async def create(ctx: click.Context, email: str, password: str):
  17. """Create a new user."""
  18. client: R2RAsyncClient = ctx.obj
  19. try:
  20. with timer():
  21. response = await client.users.create(
  22. email=email,
  23. password=password,
  24. )
  25. click.echo(json.dumps(response, indent=2))
  26. except R2RException as e:
  27. click.echo(str(e), err=True)
  28. except Exception as e:
  29. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  30. @users.command()
  31. @click.option("--ids", multiple=True, help="Document IDs to fetch", type=str)
  32. @click.option(
  33. "--offset",
  34. default=0,
  35. help="The offset to start from. Defaults to 0.",
  36. type=int,
  37. )
  38. @click.option(
  39. "--limit",
  40. default=100,
  41. help="The maximum number of nodes to return. Defaults to 100.",
  42. type=int,
  43. )
  44. @pass_context
  45. async def list(
  46. ctx: click.Context,
  47. ids: tuple[str, ...],
  48. offset: int,
  49. limit: int,
  50. ):
  51. """Get an overview of users."""
  52. uuids: _list[str | UUID] | None = (
  53. [UUID(id_) for id_ in ids] if ids else None
  54. )
  55. client: R2RAsyncClient = ctx.obj
  56. try:
  57. with timer():
  58. response = await client.users.list(
  59. ids=uuids,
  60. offset=offset,
  61. limit=limit,
  62. )
  63. for user in response["results"]: # type: ignore
  64. click.echo(json.dumps(user, indent=2))
  65. except R2RException as e:
  66. click.echo(str(e), err=True)
  67. except Exception as e:
  68. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  69. @users.command()
  70. @click.argument("id", required=True, type=str)
  71. @pass_context
  72. async def retrieve(ctx: click.Context, id):
  73. """Retrieve a user by ID."""
  74. client: R2RAsyncClient = ctx.obj
  75. try:
  76. with timer():
  77. response = await client.users.retrieve(id=id)
  78. click.echo(json.dumps(response, indent=2))
  79. except R2RException as e:
  80. click.echo(str(e), err=True)
  81. except Exception as e:
  82. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  83. @users.command()
  84. @pass_context
  85. async def me(ctx: click.Context):
  86. """Retrieve the current user."""
  87. client: R2RAsyncClient = ctx.obj
  88. try:
  89. with timer():
  90. response = await client.users.me()
  91. click.echo(json.dumps(response, indent=2))
  92. except R2RException as e:
  93. click.echo(str(e), err=True)
  94. except Exception as e:
  95. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  96. @users.command()
  97. @click.argument("id", required=True, type=str)
  98. @click.option(
  99. "--offset",
  100. default=0,
  101. help="The offset to start from. Defaults to 0.",
  102. )
  103. @click.option(
  104. "--limit",
  105. default=100,
  106. help="The maximum number of nodes to return. Defaults to 100.",
  107. )
  108. @pass_context
  109. async def list_collections(ctx: click.Context, id, offset, limit):
  110. """List collections for a specific user."""
  111. client: R2RAsyncClient = ctx.obj
  112. try:
  113. with timer():
  114. response = await client.users.list_collections(
  115. id=id,
  116. offset=offset,
  117. limit=limit,
  118. )
  119. for collection in response["results"]: # type: ignore
  120. click.echo(json.dumps(collection, indent=2))
  121. except R2RException as e:
  122. click.echo(str(e), err=True)
  123. except Exception as e:
  124. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  125. @users.command()
  126. @click.argument("id", required=True, type=str)
  127. @click.argument("collection_id", required=True, type=str)
  128. @pass_context
  129. async def add_to_collection(ctx: click.Context, id, collection_id):
  130. """Retrieve a user by ID."""
  131. client: R2RAsyncClient = ctx.obj
  132. try:
  133. with timer():
  134. response = await client.users.add_to_collection(
  135. id=id,
  136. collection_id=collection_id,
  137. )
  138. click.echo(json.dumps(response, indent=2))
  139. except R2RException as e:
  140. click.echo(str(e), err=True)
  141. except Exception as e:
  142. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  143. @users.command()
  144. @click.argument("id", required=True, type=str)
  145. @click.argument("collection_id", required=True, type=str)
  146. @pass_context
  147. async def remove_from_collection(ctx: click.Context, id, collection_id):
  148. """Retrieve a user by ID."""
  149. client: R2RAsyncClient = ctx.obj
  150. try:
  151. with timer():
  152. response = await client.users.remove_from_collection(
  153. id=id,
  154. collection_id=collection_id,
  155. )
  156. click.echo(json.dumps(response, indent=2))
  157. except R2RException as e:
  158. click.echo(str(e), err=True)
  159. except Exception as e:
  160. click.echo(str(f"An unexpected error occurred: {e}"), err=True)