Skip to content

Commit

Permalink
Allow endpoints to specify explicit http methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Apr 22, 2021
1 parent 609bd46 commit 649ee8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/middlewared/middlewared/restful.py
Expand Up @@ -142,6 +142,10 @@ async def register_resources(self):
res_kwargs['post'] = methodname
else:
res_kwargs['get'] = methodname
for rest_method in (method['explicit_methods'] or []):
assert rest_method in ('post', 'get')
res_kwargs[rest_method] = methodname

Resource(self, self.middleware, short_methodname, service['config'], parent=parent, **res_kwargs)
await asyncio.sleep(0) # Force context switch

Expand Down
19 changes: 19 additions & 0 deletions src/middlewared/middlewared/service.py
Expand Up @@ -181,6 +181,23 @@ def wrapper(fn):
return wrapper


def rest_api_metadata(explicit_methods=None):
"""
Allow having endpoints specify explicit rest methods.
Explicit methods should be a list which specifies what methods the function should be available
at other then the default one it is already going to be. This is useful when we want to maintain
backwards compatibility with endpoints which were not expecting payload before but are now and users
still would like to consume them with previous method which would be GET whereas it's POST now.
"""
def wrapper(fn):
fn._rest_api_metadata = {
'explicit_methods': explicit_methods,
}
return fn
return wrapper


def periodic(interval, run_on_start=True):
def wrapper(fn):
fn._periodic = PeriodicTaskDescriptor(interval, run_on_start)
Expand Down Expand Up @@ -1070,6 +1087,8 @@ def get_methods(self, service, cli):
'filterable': hasattr(method, '_filterable'),
'filterable_schema': None,
'pass_application': hasattr(method, '_pass_app'),
'explicit_methods': method._rest_api_metadata['explicit_methods'] if hasattr(
method, '_rest_api_metadata') else None,
'require_websocket': hasattr(method, '_pass_app') and not method._pass_app['rest'],
'job': hasattr(method, '_job'),
'downloadable': hasattr(method, '_job') and 'output' in method._job['pipes'],
Expand Down

0 comments on commit 649ee8e

Please sign in to comment.