Skip to content

Commit ed0a17b

Browse files
committed
add support for inputs on async queries
1 parent 7ade199 commit ed0a17b

4 files changed

Lines changed: 32 additions & 31 deletions

File tree

examples/run renamed to examples/run-all

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
set -ex
44

5-
ENGINE=sdk-test-xs
65
DATABASE=sdk-test
76
DATABASE_CLONE=$DATABASE-clone
7+
ENGINE=sdk-test-xs
88

99
# setup
1010
python3 ./delete_database.py $DATABASE_CLONE
@@ -13,17 +13,20 @@ python3 ./delete_engine.py $ENGINE
1313

1414
# engines
1515
python3 ./create_engine.py $ENGINE --size=XS
16+
python3 ./get_engine.py $ENGINE
1617
python3 ./list_engines.py
1718
python3 ./list_engines.py --state=PROVISIONED
18-
python3 ./get_engine.py $ENGINE
19+
python3 ./list_engines.py --state=NONESENSE
1920

2021
# databases
2122
python3 ./create_database.py $DATABASE
23+
python3 ./get_database.py $DATABASE
2224
python3 ./list_databases.py
2325
python3 ./list_databases.py --state=CREATED
24-
python3 ./get_database.py $DATABASE
25-
python3 ./list_models.py $DATABASE $ENGINE
26+
python3 ./list_databases.py --state=NONSENSE
2627
python3 ./list_edbs.py $DATABASE $ENGINE
28+
python3 ./list_models.py $DATABASE $ENGINE
29+
python3 ./get_model.py $DATABASE $ENGINE stdlib
2730

2831
# run query
2932
QUERY="x, x^2, x^3, x^4 from x in {1; 2; 3; 4; 5}"
@@ -32,11 +35,12 @@ python3 ./run_query.py $DATABASE $ENGINE "$QUERY" --readonly
3235
python3 ./show_results.py $DATABASE $ENGINE
3336
python3 ./show_problems.py $DATABASE $ENGINE
3437

35-
# install model
38+
# load model
3639
python3 ./install_model.py $DATABASE $ENGINE hello.rel
37-
python3 ./list_models.py $DATABASE $ENGINE
3840
python3 ./get_model.py $DATABASE $ENGINE hello
39-
python3 ./list_edbs.py $DATABASE $ENGINE
41+
python3 ./list_models.py $DATABASE $ENGINE
42+
python3 ./delete_model.py $DATABASE $ENGINE hello
43+
python3 ./list_models.py $DATABASE $ENGINE
4044

4145
# load_csv
4246
python3 ./load_csv.py $DATABASE $ENGINE sample.csv -r sample_csv
@@ -53,12 +57,16 @@ python3 ./run_query.py $DATABASE $ENGINE sample_json
5357
python3 ./list_edbs.py $DATABASE $ENGINE
5458

5559
# clone database
60+
python3 ./delete_database.py $DATABASE
61+
python3 ./create_database.py $DATABASE
62+
python3 ./load_json.py $DATABASE $ENGINE sample.json -r sample_json
63+
python3 ./install_model.py $DATABASE $ENGINE hello.rel
5664
python3 ./clone_database.py $DATABASE_CLONE $DATABASE
65+
python3 ./get_database.py $DATABASE_CLONE
5766
python3 ./list_databases.py
5867
python3 ./list_databases.py --state=CREATED
59-
python3 ./get_database.py $DATABASE_CLONE
60-
python3 ./list_models.py $DATABASE_CLONE $ENGINE
6168
python3 ./list_edbs.py $DATABASE_CLONE $ENGINE
69+
python3 ./list_models.py $DATABASE_CLONE $ENGINE
6270
python3 ./get_model.py $DATABASE_CLONE $ENGINE hello
6371

6472
# delete model
@@ -86,7 +94,6 @@ python3 update_user.py $USERID --status=INACTIVE
8694
python3 update_user.py $USERID --status=ACTIVE
8795
python3 update_user.py $USERID --roles=admin --roles=user
8896
python3 update_user.py $USERID --status=INACTIVE --roles=user
89-
python3 update_user.py $USERID
9097

9198
# cleanup
9299
python3 ./delete_database.py $DATABASE_CLONE

examples/run_query_async.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License
14+
1415
from argparse import ArgumentParser
1516
from urllib.request import HTTPError
1617
from railib import api, config, show
@@ -20,7 +21,7 @@ def run(database: str, engine: str, command: str, readonly: bool, profile: str):
2021
cfg = config.read(profile=profile)
2122
ctx = api.Context(**cfg)
2223
rsp = api.query_async(ctx, database, engine, command, readonly=readonly)
23-
show.results(rsp, "multipart" if isinstance(rsp, list) else "wire")
24+
show.results(rsp)
2425

2526

2627
if __name__ == "__main__":

railib/api.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,6 @@ def update_user(ctx: Context, userid: str, status: str = None, roles=None):
348348
return json.loads(rsp.read())
349349

350350

351-
#
352-
# Transaction endpoint
353-
#
354351
class Transaction(object):
355352
def __init__(self, database: str, engine: str, abort=False,
356353
mode: Mode = Mode.OPEN, nowait_durable=False, readonly=False,
@@ -406,23 +403,15 @@ def run(self, ctx: Context, *args) -> dict:
406403
return json.loads(rsp.read())
407404

408405

409-
#
410-
# /transactions endpoint
411-
#
412406
class TransactionAsync(object):
413-
def __init__(self, database: str, engine: str, command: str, nowait_durable=False, readonly=False,
414-
inputs: dict = None):
407+
def __init__(self, database: str, engine: str, nowait_durable=False, readonly=False):
415408
self.database = database
416409
self.engine = engine
417-
self.command = command
418410
self.nowait_durable = nowait_durable
419411
self.readonly = readonly
420-
self.inputs = inputs
421412

422413
@property
423414
def data(self):
424-
inputs = self.inputs or {}
425-
inputs = [_query_action_input(k, v) for k, v in inputs.items()]
426415
result = {
427416
"dbname": self.database,
428417
"nowait_durable": self.nowait_durable,
@@ -431,16 +420,18 @@ def data(self):
431420
}
432421
if self.engine is not None:
433422
result["engine_name"] = self.engine
434-
result["query"] = self.command
435-
result["inputs"] = inputs
436423
return result
437424

438-
def run(self, ctx: Context) -> Union[dict, list]:
425+
def run(self, ctx: Context, command: str, inputs: dict = None) -> Union[dict, list]:
439426
data = self.data
440-
url = _mkurl(ctx, PATH_TRANSACTIONS)
441-
rsp = rest.post(ctx, url, data)
427+
data["query"] = command
428+
if not inputs is None:
429+
inputs = [_query_action_input(k, v) for k, v in inputs.items()]
430+
data["v1_inputs"] = inputs
431+
rsp = rest.post(ctx, _mkurl(ctx, PATH_TRANSACTIONS), data)
442432
content_type = rsp.headers.get('content-type', None)
443433
content = rsp.read()
434+
# todo: response model should be based on status code (200 v. 201)
444435
# async mode
445436
if content_type.lower() == "application/json":
446437
return json.loads(content)
@@ -668,8 +659,8 @@ def query(ctx: Context, database: str, engine: str, command: str,
668659

669660
def query_async(ctx: Context, database: str, engine: str, command: str,
670661
readonly: bool = True, inputs: dict = None) -> Union[dict, list]:
671-
tx = TransactionAsync(database, engine, command, readonly=readonly, inputs=inputs)
672-
return tx.run(ctx)
662+
tx = TransactionAsync(database, engine, readonly=readonly)
663+
return tx.run(ctx, command, inputs=inputs)
673664

674665

675666
create_compute = create_engine # deprecated, use create_engine

railib/show.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ def problems(rsp: dict) -> None:
122122

123123

124124
# Print the results contained in the given response dict.
125-
def results(rsp: Union[dict, list], format="physical") -> None:
125+
def results(rsp: Union[dict, list], format=None) -> None:
126126
if rsp is None:
127127
return
128+
if format is None:
129+
format = "multipart" if isinstance(rsp, list) else "wire"
128130
if format == "wire":
129131
json.dump(rsp, sys.stdout, indent=2)
130132
elif format == "physical":

0 commit comments

Comments
 (0)