collections.py 3.3 KB

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