-
Notifications
You must be signed in to change notification settings - Fork 150
/
__init__.py
executable file
·67 lines (55 loc) · 2.05 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from webob.multidict import MultiDict
from cornice.validators._colander import (
validator as colander_validator,
body_validator as colander_body_validator)
__all__ = ['colander_validator',
'colander_body_validator',
'extract_cstruct',
'DEFAULT_VALIDATORS',
'DEFAULT_FILTERS']
DEFAULT_VALIDATORS = []
DEFAULT_FILTERS = []
def extract_cstruct(request):
"""
Extract attributes from the specified `request` such as body, url, path,
method, querystring, headers, cookies, and returns them in a single dict
object.
:param request: Current request
:type request: :class:`~pyramid:pyramid.request.Request`
:returns: A mapping containing most request attributes.
:rtype: dict
"""
if request.content_type == 'application/x-www-form-urlencoded':
body = request.POST.mixed()
elif request.content_type and request.content_type != 'application/json':
body = request.body
else:
if request.body:
try:
body = request.json_body
except ValueError as e:
request.errors.add('body', '', 'Invalid JSON: %s' % e)
return {}
else:
if not hasattr(body, 'items'):
request.errors.add('body', '', 'Should be a JSON object')
return {}
else:
body = {}
cstruct = {'method': request.method,
'url': request.url,
'path': request.path,
'body': body}
for sub, attr in (('querystring', 'GET'),
('header', 'headers'),
('cookies', 'cookies')):
data = getattr(request, attr)
if isinstance(data, MultiDict):
data = data.mixed()
else:
data = dict(data)
cstruct[sub] = data
return cstruct