Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/api/decorators/flask_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import json
import time
import subprocess
from flask import Response
from flask import Response, request
from functools import wraps
from ovs_extensions.api.exceptions import HttpBadRequestException
from ovs_extensions.dal.base import ObjectNotFoundException

Expand All @@ -34,6 +35,40 @@ class HTTPRequestFlaskDecorators(object):
logger = None
version = None

@classmethod
def provide_request_data(cls, f):
# type: (callable) -> callable
"""
This decorator feeds in the request data in the function with the 'request_data' keyword
Used for backwards compatibility (transition to application JSON heading with client/server)
- Attempts to read JSON contents
:return: The wrapped function
:rtype: callable
"""
@wraps(f)
def wrap(*args, **kwargs):
# type: (*any, **any) -> any
"""
Wrapper function
:return: Output of the wrapped function
:rtype: any
"""
request_data = request.get_json()
if request_data is None:
# Try the old route. All keys are potentially JSON serialized within the form (it was a mess)
request_data = {}
for key, value in request.form.iteritems():
try:
value = json.loads(value)
except ValueError:
# Not a valid JSON, could be string (who can tell at this point...)
pass
request_data[key] = value
if 'request_data' in kwargs:
raise ValueError('request_data is a reserved argument for the decorator')
return f(*args, request_data=request_data, **kwargs)
return wrap

@classmethod
def get(cls, route, authenticate=True):
"""
Expand Down