Skip to content

Commit

Permalink
REST API: make the profile configurable as request parameter
Browse files Browse the repository at this point in the history
To make this possible, after parsing the query string but before
performing the request, the desired profile needs to be loaded. A new
method `load_profile` is added to the `BaseResource` class. All methods
that access the storage, such as the `get` methods, need to invoke this
method before handing the request.

The `load_profile` method will call `load_profile` with `allow_switch`
set to True, in order to allow changing the profile if another had
already been loaded. The profile that is loaded is determined from the
`profile` query parameter specified in the request. If not specified,
the profile will be taken that was specified in the `kwargs` of the
resources constructor.

Note that the parsing of the request path and query parameters had to be
refactored a bit to prevent the parsing having to be performed twice,
which would result in a performance regression.

When the REST API is invoked through the `verdi` CLI, the profile
specified by the `-p` option, or the default profile if not specified,
is passed to the API, which will be passed to the resource constructors.
This guarantees that if `profile` is not specified in the query
parameters the profile with which `verdi restapi` was invoked will be
loaded.
  • Loading branch information
sphuber committed Jul 15, 2022
1 parent 15f4bae commit 2f87d74
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 60 deletions.
4 changes: 3 additions & 1 deletion aiida/cmdline/commands/cmd_restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
help='Enable POST endpoints (currently only /querybuilder).',
hidden=True,
)
def restapi(hostname, port, config_dir, debug, wsgi_profile, posting):
@click.pass_context
def restapi(ctx, hostname, port, config_dir, debug, wsgi_profile, posting):
"""
Run the AiiDA REST API server.
Expand All @@ -58,6 +59,7 @@ def restapi(hostname, port, config_dir, debug, wsgi_profile, posting):
# Invoke the runner
try:
run_api(
profile=ctx.obj['profile'].name,
hostname=hostname,
port=port,
config=config_dir,
Expand Down
14 changes: 11 additions & 3 deletions aiida/restapi/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def build_translator_parameters(self, field_list):
extras = None
extras_filter = None
full_type = None
profile = None

# io tree limit parameters
tree_in_limit = None
Expand Down Expand Up @@ -539,10 +540,17 @@ def build_translator_parameters(self, field_list):
raise RestInputValidationError('You cannot specify extras_filter more than once')
if 'full_type' in field_counts and field_counts['full_type'] > 1:
raise RestInputValidationError('You cannot specify full_type more than once')
if 'profile' in field_counts and field_counts['profile'] > 1:
raise RestInputValidationError('You cannot specify profile more than once')

## Extract results
for field in field_list:
if field[0] == 'limit':
if field[0] == 'profile':
if field[1] == '=':
profile = field[2]
else:
raise RestInputValidationError("only assignment operator '=' is permitted after 'profile'")
elif field[0] == 'limit':
if field[1] == '=':
limit = field[2]
else:
Expand Down Expand Up @@ -658,7 +666,7 @@ def build_translator_parameters(self, field_list):

return (
limit, offset, perpage, orderby, filters, download_format, download, filename, tree_in_limit,
tree_out_limit, attributes, attributes_filter, extras, extras_filter, full_type
tree_out_limit, attributes, attributes_filter, extras, extras_filter, full_type, profile
)

def parse_query_string(self, query_string):
Expand All @@ -680,7 +688,7 @@ def parse_query_string(self, query_string):

## Define grammar
# key types
key = Word(f'{alphas}_', f'{alphanums}_')
key = Word(f'{alphas}_', f'{alphanums}_-')
# operators
operator = (
Literal('=like=') | Literal('=ilike=') | Literal('=in=') | Literal('=notin=') | Literal('=') |
Expand Down
Loading

0 comments on commit 2f87d74

Please sign in to comment.