Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

api docs: Document all /bot_storage endpoint methods.

Agnish Bauri197a5939f578
diff --git a/api_docs/include/rest-endpoints.md b/api_docs/include/rest-endpoints.md
index 073cf4ceb21..c6c5730da85 100644
--- a/api_docs/include/rest-endpoints.md
+++ b/api_docs/include/rest-endpoints.md
@@ -166,6 +166,12 @@
* [Get events from an event queue](/api/get-events)
* [Delete an event queue](/api/delete-queue)
+## Interactive bots
+
+* [Get a bot's stored data](/api/get-bot-storage)
+* [Update a bot's stored data](/api/update-bot-storage)
+* [Remove a bot's stored data](/api/remove-bot-storage)
+
## Specialty endpoints
* [Fetch an API key (production)](/api/fetch-api-key)
diff --git a/tools/test-api b/tools/test-api
index 38de41aff8e..52189292e7e 100755
--- a/tools/test-api
+++ b/tools/test-api
@@ -110,7 +110,15 @@ with test_server_running(
site=site,
)
- test_the_api(client, nonadmin_client, owner_client)
+ # Prepare the bot client using the pre-existing default bot in the dev realm.
+ bot_user = get_user("[email protected]", get_realm("zulip"))
+ bot_client = Client(
+ api_key=bot_user.api_key,
+ site=site,
+ )
+
+ test_the_api(client, nonadmin_client, owner_client, bot_client)
test_generated_curl_examples_for_success(client)
test_js_bindings(client)
diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py
index 9f91ac0b321..ab9ffb92f1c 100644
--- a/zerver/openapi/curl_param_value_generators.py
+++ b/zerver/openapi/curl_param_value_generators.py
@@ -19,6 +19,7 @@ from zerver.actions.reactions import do_add_reaction
from zerver.actions.realm_domains import do_add_realm_domain
from zerver.actions.realm_linkifiers import do_add_linkifier
from zerver.actions.realm_playgrounds import check_add_realm_playground
+from zerver.lib.bot_storage import set_bot_storage
from zerver.lib.events import do_events_register
from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
@@ -499,3 +500,26 @@ def delete_realm_domain_owner_auth() -> dict[str, object]:
do_add_realm_domain(owner.realm, "delete-domain-example.com", False, acting_user=None)
return {"domain": "delete-domain-example.com"}
+
+
+@openapi_param_value_generator(["/bot_storage:put"])
+def update_bot_storage_bot_auth() -> dict[str, object]:
+ bot = helpers.example_user("default_bot")
+ AUTHENTICATION_LINE[0] = f"{bot.email}:{bot.api_key}"
+ return {}
+
+
+@openapi_param_value_generator(["/bot_storage:get"])
+def get_bot_storage_bot_auth() -> dict[str, object]:
+ bot = helpers.example_user("default_bot")
+ set_bot_storage(bot, [("foo", "bar")])
+ AUTHENTICATION_LINE[0] = f"{bot.email}:{bot.api_key}"
+ return {}
+
+
+@openapi_param_value_generator(["/bot_storage:delete"])
+def remove_bot_storage_bot_auth() -> dict[str, object]:
+ bot = helpers.example_user("default_bot")
+ set_bot_storage(bot, [("foo", "bar")])
+ AUTHENTICATION_LINE[0] = f"{bot.email}:{bot.api_key}"
+ return {}
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index c15b0b31817..5916dcfa4ac 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -837,6 +837,39 @@ def regenerate_bot_api_key(client: Client) -> None:
validate_against_openapi_schema(result, "/bots/{bot_id}/api_key/regenerate", "post", "200")
+@openapi_test_function("/bot_storage:put")
+def update_bot_storage(client: Client) -> None:
+ # {code_example|start}
+ # Store value of "bar" for the key "foo" for a bot user.
+ result = client.update_storage({"storage": {"foo": "bar"}})
+ # {code_example|end}
+ assert_success_response(result)
+ validate_against_openapi_schema(result, "/bot_storage", "put", "200")
+
+
+@openapi_test_function("/bot_storage:get")
+def get_bot_storage(client: Client) -> None:
+ # {code_example|start}
+ # Retrieve all data stored for a bot user.
+ result = client.get_storage()
+ # {code_example|end}
+ assert_success_response(result)
+ validate_against_openapi_schema(result, "/bot_storage", "get", "200")
+
+
+@openapi_test_function("/bot_storage:delete")
+def remove_bot_storage(client: Client) -> None:
+ # {code_example|start}
+ # Remove all data stored for a bot user.
+ result = client.call_endpoint(
+ url="/bot_storage",
+ method="DELETE",
+ )
+ # {code_example|end}
+ assert_success_response(result)
+ validate_against_openapi_schema(result, "/bot_storage", "delete", "200")
+
+
@openapi_test_function("/get_stream_id:get")
def get_stream_id(client: Client) -> int:
name = "python-test"
@@ -2265,8 +2298,19 @@ def test_api_key_endpoints(client: Client) -> None:
regenerate_api_key(client)
-def test_the_api(client: Client, nonadmin_client: Client, owner_client: Client) -> None:
+def test_bot_storage(bot_client: Client) -> None:
+ update_bot_storage(bot_client)
+ get_bot_storage(bot_client)
+ remove_bot_storage(bot_client)
+
+
+def test_the_api(
+ client: Client, nonadmin_client: Client, owner_client: Client, bot_client: Client
+) -> None:
get_user_agent(client)
+ # test_bot_storage authenticates as default-bot, whose API key is
+ # regenerated in test_api_key_endpoints.
+ test_bot_storage(bot_client)
# Run `test_api_key_endpoints` before `test_users` so Device records created by
# `register_push_device` & `register_device` are not bulk-deleted by
# `regenerate_api_key`, since they are needed for curl tests.
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index c8b4d471021..304b8b9d421 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -7122,6 +7122,185 @@ paths:
}
description: |
A typical failed JSON response for when the user is not logged in:
+ /bot_storage:
+ get:
+ operationId: get-bot-storage
+ summary: Get a bot's stored data
+ tags: ["bots"]
+ description: |
+ Retrieve [data stored](/help/interactive-bots-api#bot_handlerstorage)
+ for a bot user.
+ x-curl-examples-parameters:
+ oneOf:
+ - type: exclude
+ parameters:
+ enum:
+ - keys
+ parameters:
+ - name: keys
+ in: query
+ description: |
+ A JSON-encoded list of keys for data in the bot's storage.
+
+ If not provided, then all data that's stored for the bot is
+ returned.
+ content:
+ application/json:
+ schema:
+ type: array
+ items:
+ type: string
+ example: ["foo"]
+ required: false
+ responses:
+ "200":
+ description: Success.
+ content:
+ application/json:
+ schema:
+ allOf:
+ - $ref: "#/components/schemas/JsonSuccessBase"
+ - additionalProperties: false
+ properties:
+ result: {}
+ msg: {}
+ ignored_parameters_unsupported: {}
+ storage:
+ type: object
+ description: |
+ A dictionary with string key-value pairs of
+ data stored for the bot user.
+
+ If the `keys` parameter was passed, then only
+ the key-value pairs matching the list of keys
+ will be returned.
+ additionalProperties:
+ description: |
+ `{key}`: The stored string value for this key.
+ type: string
+ example:
+ {
+ "result": "success",
+ "msg": "",
+ "storage": {"foo": "bar"},
+ }
+ "400":
+ description: Bad request.
+ content:
+ application/json:
+ schema:
+ oneOf:
+ - allOf:
+ - $ref: "#/components/schemas/CodedError"
+ - example:
+ {
+ "code": "BAD_REQUEST",
+ "msg": "Key does not exist.",
+ "result": "error",
+ }
+ description: |
+ A typical failed JSON response when a requested key does not exist
+ in the bot's storage:
+ put:
+ operationId: update-bot-storage
+ summary: Update a bot's stored data
+ tags: ["bots"]
+ description: |
+ Add or update [data stored](/help/interactive-bots-api#bot_handlerstorage)
+ for a bot user.
+
+ Each bot has a limited storage set by the server, which normally is a
+ default of 10,000,000 characters.
+ requestBody:
+ required: true
+ content:
+ application/x-www-form-urlencoded:
+ schema:
+ type: object
+ properties:
+ storage:
+ description: |
+ A JSON-encoded dictionary mapping string keys to string values
+ that will be added to the bot's storage.
+
+ If the bot's storage already has a specific key, then the value
+ stored for that key will be updated for the new value.
+ type: object
+ additionalProperties:
+ type: string
+ example: {"foo": "bar"}
+ required:
+ - storage
+ encoding:
+ storage:
+ contentType: application/json
+ responses:
+ "200":
+ $ref: "#/components/responses/SimpleSuccess"
+ "400":
+ description: Bad request.
+ content:
+ application/json:
+ schema:
+ oneOf:
+ - allOf:
+ - $ref: "#/components/schemas/CodedError"
+ - example:
+ {
+ "code": "BAD_REQUEST",
+ "msg": "Request exceeds storage limit by 42 characters. The limit is 10000000 characters.",
+ "result": "error",
+ }
+ description: |
+ A typical failed JSON response when the request would exceed the
+ server's storage size limit:
+ delete:
+ operationId: remove-bot-storage
+ summary: Remove a bot's stored data
+ tags: ["bots"]
+ description: |
+ Delete [data stored](/help/interactive-bots-api#bot_handlerstorage)
+ for a bot user.
+ requestBody:
+ required: false
+ content:
+ application/x-www-form-urlencoded:
+ schema:
+ type: object
+ properties:
+ keys:
+ description: |
+ A JSON-encoded list of keys to delete from the bot's storage.
+
+ If not provided, then all data that's stored for the bot is
+ deleted.
+ type: array
+ items:
+ type: string
+ example: ["foo"]
+ encoding:
+ keys:
+ contentType: application/json
+ responses:
+ "200":
+ $ref: "#/components/responses/SimpleSuccess"
+ "400":
+ description: Bad request.
+ content:
+ application/json:
+ schema:
+ oneOf:
+ - allOf:
+ - $ref: "#/components/schemas/CodedError"
+ - example:
+ {
+ "code": "BAD_REQUEST",
+ "msg": "Key does not exist.",
+ "result": "error",
+ }
+ description: |
+ A typical failed JSON response when a key to be deleted does not exist
+ in the bot's storage:
/drafts:
get:
operationId: get-drafts
diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py
index 0a1887ff670..08a00205fa4 100644
--- a/zerver/tests/test_openapi.py
+++ b/zerver/tests/test_openapi.py
@@ -225,7 +225,6 @@ class OpenAPIArgumentsTest(ZulipTestCase):
#### These personal settings endpoints have modest value to document:
"/users/me/avatar",
#### Should be documented as part of interactive bots documentation
- "/bot_storage",
"/submessage",
"/zcommand",
#### These "organization settings" endpoint have modest value to document:
@@ -930,6 +929,7 @@ class OpenAPIAttributesTest(ZulipTestCase):
"invites",
"reminders",
"navigation_views",
+ "bots",
]
paths = OpenAPISpec(OPENAPI_SPEC_PATH).openapi()["paths"]
for path, path_item in paths.items():