(core) External requests

Summary:
Adds a Python function `REQUEST` which makes an HTTP GET request. Behind the scenes it:

- Raises a special exception to stop trying to evaluate the current cell and just keep the existing value.
- Notes the request arguments which will be returned by `apply_user_actions`.
- Makes the actual request in NodeJS, which sends back the raw response data in a new action `RespondToRequests` which reevaluates the cell(s) that made the request.
- Wraps the response data in a class which mimics the `Response` class of the `requests` library.

In certain cases, this asynchronous flow doesn't work and the sandbox will instead synchronously call an exported JS method:

- When reevaluating a single cell to get a formula error, the request is made synchronously.
- When a formula makes multiple requests, the earlier responses are retrieved synchronously from files which store responses as long as needed to complete evaluating formulas. See https://grist.slack.com/archives/CL1LQ8AT0/p1653399747810139

Test Plan: Added Python and nbrowser tests.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Subscribers: paulfitz, dsagal

Differential Revision: https://phab.getgrist.com/D3429
This commit is contained in:
Alex Hall
2022-06-17 20:49:18 +02:00
parent abebe812db
commit 9fffb491f9
17 changed files with 582 additions and 24 deletions

View File

@@ -9,6 +9,7 @@ import six
from six.moves import xrange
import acl
import depend
import gencode
from acl_formula import parse_acl_formula_json
import actions
@@ -331,6 +332,25 @@ class UserActions(object):
"""
self._engine.update_current_time()
@useraction
def RespondToRequests(self, responses, cached_keys):
"""
Reevaluate formulas which called the REQUEST function using the now available responses.
"""
engine = self._engine
# The actual raw responses which will be returned to the REQUEST function
engine._request_responses = responses
# Keys for older requests which are stored in files and can be retrieved synchronously
engine._cached_request_keys = set(cached_keys)
# Invalidate the exact cells which made the exact requests which are being responded to here.
for response in six.itervalues(responses):
for table_id, table_deps in six.iteritems(response.pop("deps")):
for col_id, row_ids in six.iteritems(table_deps):
node = depend.Node(table_id, col_id)
engine.dep_graph.invalidate_deps(node, row_ids, engine.recompute_map)
#----------------------------------------
# User actions on records.
#----------------------------------------