8000 new: `post /_admin/execute` (#309) · arangodb/python-arango@1265881 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1265881

Browse files
authored
new: post /_admin/execute (#309)
* new: `Database.execute()` * fix lint * fix end of file * fix: lint
1 parent 70548d9 commit 1265881

8 files changed

+54
-7
lines changed

arango/database.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
ServerEchoError,
4646
ServerEncryptionError,
4747
ServerEngineError,
48+
ServerExecuteError,
4849
ServerLicenseGetError,
4950
ServerLicenseSetError,
5051
ServerLogLevelError,
@@ -229,6 +230,36 @@ def response_handler(resp: Response) -> Json:
229230

230231
return self._execute(request, response_handler)
231232

233+
def execute(self, command: str) -> Result[Any]:
234+
"""Execute raw Javascript command on the server.
235+
236+
Executes the JavaScript code in the body on the server as
237+
the body of a function with no arguments. If you have a
238+
return statement then the return value you produce will be returned
239+
as 'application/json'.
240+
241+
NOTE: this method endpoint will only be usable if the server
242+
was started with the option `--javascript.allow-admin-execute true`.
243+
The default value of this option is false, which disables the execution
244+
of user-defined code and disables this API endpoint entirely.
245+
This is also the recommended setting for production.
246+
247+
:param command: Javascript command to execute.
248+
:type command: str
249+
:return: Return value of **command**, if any.
250+
:rtype: Any
251+
:raise arango.exceptions.ServerExecuteError: If execution fails.
252+
"""
253+
request = Request(method="post", endpoint="/_admin/execute", data=command)
254+
255+
def response_handler(resp: Response) -> Any:
256+
if not resp.is_success:
257+
raise ServerExecuteError(resp, request)
258+
259+
return resp.body
260+
261+
return self._execute(request, response_handler)
262+
232263
def execute_transaction(
233264
self,
234265
command: str,

arango/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ class ServerEncryptionError(ArangoServerError):
718718
"""Failed to reload user-defined encryption keys."""
719719

720720

721+
class ServerExecuteError(ArangoServerError):
722+
"""Failed to execute raw JavaScript command."""
723+
724+
721725
#####################
722726
# Task Exceptions #
723727
#####################

arango/replication.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,15 @@ def response_handler(resp: Response) -> Json:
180180
if resp.is_success:
181181
result = format_replication_header(resp.headers)
182182
result["content"] = [
183-
[
184-
self._conn.deserialize(line)
185-
for line in resp.body.split("\n")
186-
if line
187-
]
188-
if deserialize
189-
else resp.body
183+
(
184+
[
185+
self._conn.deserialize(line)
186+
for line in resp.body.split("\n")
187+
if line
188+
]
189+
if deserialize
190+
else resp.body
191+
)
190192
]
191193
return result
192194

tests/static/cluster-3.10.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ jwt-secret = /tests/static/keyfile
1010
[args]
1111
all.database.password = passwd
1212
all.log.api-enabled = true
13+
all.javascript.allow-admin-execute = true

tests/static/cluster.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ jwt-secret = /tests/static/keyfile
1111
all.database.password = passwd
1212
all.database.extended-names = true
1313
all.log.api-enabled = true
14+
all.javascript.allow-admin-execute = true

tests/static/single-3.10.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ jwt-secret = /tests/static/keyfile
88

99
[args]
1010
all.database.password = passwd
11+
all.javascript.allow-admin-execute = true

tests/static/single.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ jwt-secret = /tests/static/keyfile
99
[args]
1010
all.database.password = passwd
1111
all.database.extended-names = true
12+
all.javascript.allow-admin-execute = true

tests/test_database.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ def test_database_misc_methods(client, sys_db, db, bad_db, cluster, secret):
305305
bad_db.engine()
306306
assert err.value.error_code in {11, 1228}
307307

308+
# Test execute JavaScript code
309+
assert db.execute(1) is None
310+
assert db.execute(None) == {"error": False, "code": 200}
311+
assert db.execute("") == {"error": False, "code": 200}
312+
assert db.execute("return 1") == 1
313+
308314
# Test database compact
309315
with assert_raises(DatabaseCompactError) as err:
310316
db.compact()

0 commit comments

Comments
 (0)
0