Skip to content

Commit 1737b71

Browse files
fred-yu-2013stiartsly
authored andcommitted
CU-3ebk1pj - Add an API to get scripts.
1 parent 5e1bb57 commit 1737b71

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

docs/source/index.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Summary
2020
database.get_collections, database.create_collection, database.delete_collection, database.insert_or_count,
2121
database.update, database.delete, database.find, database.query,
2222
files.reading_operation, files.writing_operation, files.move_file, files.delete_file,
23-
scripting.register_script, scripting.call_script, scripting.call_script_url,
23+
scripting.register_script, scripting.get_scripts, scripting.call_script, scripting.call_script_url,
2424
scripting.unregister_script, scripting.upload_file, scripting.download_file,
2525
backup.state, backup.backup_restore, backup.server_promotion,
2626
payment.version, payment.place_order, payment.settle_order, payment.orders, payment.receipts,
@@ -229,6 +229,13 @@ register script
229229
:undoc-static:
230230
:endpoints: scripting.register_script
231231

232+
get scripts
233+
-----------
234+
235+
.. autoflask:: src:get_docs_app()
236+
:undoc-static:
237+
:endpoints: scripting.get_scripts
238+
232239
call script
233240
-----------
234241

src/modules/scripting/scripting.py

+27
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,30 @@ def parse_transaction_id(transaction_id):
446446
raise InvalidParameterException(f"Invalid transaction id '{transaction_id}': {[row_id, target_did, target_app_did]}.")
447447

448448
return row_id, target_did, target_app_did
449+
450+
def get_scripts(self, skip, limit, name):
451+
""" :v2 API: """
452+
self.vault_manager.get_vault(g.usr_did).check_write_permission()
453+
454+
if name:
455+
self.check_internal_script(name)
456+
filter_ = {'name': name}
457+
else:
458+
filter_ = {'name': {'$ne': SCRIPT_ANONYMOUS_FILE}}
459+
460+
options = {}
461+
if skip:
462+
options['skip'] = skip
463+
if limit:
464+
options['limit'] = limit
465+
466+
docs = self.mcli.get_user_collection(g.usr_did, g.app_did, SCRIPTING_SCRIPT_COLLECTION).find_many(filter_, **options)
467+
if not docs:
468+
raise ScriptNotFoundException()
469+
470+
for d in docs:
471+
del d['_id']
472+
fix_dollar_keys_recursively(d, is_save=False)
473+
return {
474+
"scripts": docs
475+
}

src/view/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def init_app(api: Api):
4545
api.add_resource(files.DeleteFile, '/vault/files/<path:path>', endpoint='files.delete_file')
4646

4747
# scripting service
48+
api.add_resource(scripting.GetScripts, '/vault/scripting/scripts', endpoint='scripting.get_scripts')
4849
api.add_resource(scripting.RegisterScript, '/vault/scripting/<script_name>', endpoint='scripting.register_script')
4950
api.add_resource(scripting.CallScript, '/vault/scripting/<script_name>', endpoint='scripting.call_script')
5051
api.add_resource(scripting.CallScriptUrl, '/vault/scripting/<script_name>/<context_str>/<params>', endpoint='scripting.call_script_url')

src/view/scripting.py

+63
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from src.modules.scripting.scripting import Scripting
1111
from src.utils.http_exception import InvalidParameterException
12+
from src.utils.http_request import RV
1213
from src.utils.http_response import response_stream
1314

1415

@@ -179,6 +180,68 @@ def put(self, script_name):
179180
return self.scripting.register_script(script_name)
180181

181182

183+
class GetScripts(Resource):
184+
def __init__(self):
185+
self.scripting = Scripting()
186+
187+
def get(self):
188+
""" Get the scripts which user already registered.
189+
190+
.. :quickref: 05 Scripting; Get Scripts
191+
192+
**Request**:
193+
194+
**URL Parameters**:
195+
196+
.. sourcecode:: http
197+
198+
<skip> # optional, default is 0
199+
<limit> # optional, default is 0 ()
200+
<name> # the script name.
201+
202+
**Response OK**:
203+
204+
.. sourcecode:: http
205+
206+
HTTP/1.1 200 OK
207+
208+
.. code-block:: json
209+
210+
{
211+
"scripts": [{ # the content of every script.
212+
"context": {...},
213+
"executable": {...},
214+
"allowAnonymousUser": false,
215+
"allowAnonymousApp": false
216+
}]
217+
}
218+
219+
**Response Error**:
220+
221+
.. sourcecode:: http
222+
223+
HTTP/1.1 401 Unauthorized
224+
225+
.. sourcecode:: http
226+
227+
HTTP/1.1 400 Bad Request
228+
229+
.. sourcecode:: http
230+
231+
HTTP/1.1 403 Forbidden
232+
233+
.. sourcecode:: http
234+
235+
HTTP/1.1 404 Not Found
236+
237+
"""
238+
skip = RV.get_args().get_opt('skip', int, None)
239+
limit = RV.get_args().get_opt('limit', int, None)
240+
name = RV.get_args().get_opt('name', str, None)
241+
242+
return self.scripting.get_scripts(skip, limit, name)
243+
244+
182245
class UnregisterScript(Resource):
183246
def __init__(self):
184247
self.scripting = Scripting()

tests/scripting_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,38 @@ def execute_once(content, words_count):
263263
execute_once('message6', 60000)
264264
execute_once('message8', 80000)
265265

266+
def test02_get_scripts(self):
267+
script_name, executable_name = 'ipfs_database_count', 'database_count'
268+
script_body = {'condition': {
269+
'name': 'verify_user_permission',
270+
'type': 'queryHasResults',
271+
'body': {
272+
'collection': self.collection_name,
273+
'filter': {'author': '$params.condition_author'}
274+
}
275+
}, 'executable': {
276+
'name': executable_name,
277+
'type': 'count',
278+
'body': {
279+
'collection': self.collection_name,
280+
'filter': {'author': '$params.author'}
281+
}
282+
}}
283+
self.__register_script(script_name, script_body)
284+
285+
# the check for 'get scripts'.
286+
response = self.cli.get(f'/scripting/scripts')
287+
RA(response).assert_status(200)
288+
scripts = RA(response).body().get('scripts', list)
289+
self.assertEqual(len(scripts), 1)
290+
script = DictAsserter(**scripts[0])
291+
script.get('executable', dict).assert_equal('name', executable_name)
292+
script.get('executable', dict).assert_equal('type', 'count')
293+
script.get('executable', dict).get('body').assert_equal('collection', self.collection_name)
294+
script.get('executable', dict).get('body', dict).get('filter', dict).assert_equal('author', '$params.author')
295+
296+
self.delete_script(script_name)
297+
266298
def test02_count(self):
267299
script_name, executable_name = 'ipfs_database_count', 'database_count'
268300
script_body = {'executable': {

0 commit comments

Comments
 (0)