Revision 5a6cee09
b/snf-astakos-app/astakos/api/tokens.py | ||
---|---|---|
91 | 91 |
|
92 | 92 |
d = defaultdict(dict) |
93 | 93 |
if not public_mode: |
94 |
req = utils.get_request_dict(request)
|
|
94 |
req = utils.get_json_body(request)
|
|
95 | 95 |
|
96 | 96 |
uuid = None |
97 | 97 |
try: |
b/snf-astakos-app/astakos/im/tests/api.py | ||
---|---|---|
545 | 545 |
r = client.post(url, "not json", content_type='application/json') |
546 | 546 |
self.assertEqual(r.status_code, 400) |
547 | 547 |
body = json.loads(r.content) |
548 |
self.assertEqual(body['badRequest']['message'], 'Invalid JSON data') |
|
548 |
self.assertEqual(body['badRequest']['message'], |
|
549 |
'Could not decode request body as JSON') |
|
549 | 550 |
|
550 | 551 |
# Check auth with token |
551 | 552 |
post_data = """{"auth":{"token": {"id":"%s"}, |
b/snf-cyclades-app/synnefo/admin/views.py | ||
---|---|---|
61 | 61 |
images = True |
62 | 62 |
backend = None |
63 | 63 |
if request.body: |
64 |
req = utils.get_request_dict(request)
|
|
64 |
req = utils.get_json_body(request)
|
|
65 | 65 |
req_stats = utils.get_attribute(req, "stats", required=True, |
66 | 66 |
attr_type=dict) |
67 | 67 |
# Check backend |
b/snf-cyclades-app/synnefo/api/floating_ips.py | ||
---|---|---|
93 | 93 |
serializations=["json"]) |
94 | 94 |
def floating_ip_action_demux(request, floating_ip_id): |
95 | 95 |
userid = request.user_uniq |
96 |
req = utils.get_request_dict(request)
|
|
96 |
req = utils.get_json_body(request)
|
|
97 | 97 |
log.debug('floating_ip_action %s %s', floating_ip_id, req) |
98 | 98 |
if len(req) != 1: |
99 | 99 |
raise faults.BadRequest('Malformed request.') |
... | ... | |
165 | 165 |
@transaction.commit_on_success |
166 | 166 |
def allocate_floating_ip(request): |
167 | 167 |
"""Allocate a floating IP.""" |
168 |
req = utils.get_request_dict(request)
|
|
168 |
req = utils.get_json_body(request)
|
|
169 | 169 |
floating_ip_dict = api.utils.get_attribute(req, "floatingip", |
170 | 170 |
required=True, attr_type=dict) |
171 | 171 |
log.info('allocate_floating_ip %s', req) |
... | ... | |
226 | 226 |
#userid = request.user_uniq |
227 | 227 |
#log.info("update_floating_ip '%s'. User '%s'.", floating_ip_id, userid) |
228 | 228 |
|
229 |
#req = utils.get_request_dict(request)
|
|
229 |
#req = utils.get_json_body(request)
|
|
230 | 230 |
#info = api.utils.get_attribute(req, "floatingip", required=True) |
231 | 231 |
|
232 | 232 |
#device_id = api.utils.get_attribute(info, "device_id", required=False) |
b/snf-cyclades-app/synnefo/api/images.py | ||
---|---|---|
236 | 236 |
# badMediaType(415), |
237 | 237 |
# overLimit (413) |
238 | 238 |
|
239 |
req = utils.get_request_dict(request)
|
|
239 |
req = utils.get_json_body(request)
|
|
240 | 240 |
log.info('update_image_metadata %s %s', image_id, req) |
241 | 241 |
with image_backend(request.user_uniq) as backend: |
242 | 242 |
image = backend.get_image(image_id) |
... | ... | |
285 | 285 |
# badMediaType(415), |
286 | 286 |
# overLimit (413) |
287 | 287 |
|
288 |
req = utils.get_request_dict(request)
|
|
288 |
req = utils.get_json_body(request)
|
|
289 | 289 |
log.info('create_image_metadata_item %s %s %s', image_id, key, req) |
290 | 290 |
try: |
291 | 291 |
metadict = req['meta'] |
b/snf-cyclades-app/synnefo/api/networks.py | ||
---|---|---|
86 | 86 |
|
87 | 87 |
@api.api_method(http_method='POST', user_required=True, logger=log) |
88 | 88 |
def network_action_demux(request, network_id): |
89 |
req = utils.get_request_dict(request)
|
|
89 |
req = utils.get_json_body(request)
|
|
90 | 90 |
network = util.get_network(network_id, request.user_uniq, for_update=True) |
91 | 91 |
action = req.keys()[0] |
92 | 92 |
try: |
... | ... | |
128 | 128 |
@api.api_method(http_method='POST', user_required=True, logger=log) |
129 | 129 |
def create_network(request): |
130 | 130 |
userid = request.user_uniq |
131 |
req = api.utils.get_request_dict(request)
|
|
131 |
req = api.utils.get_json_body(request)
|
|
132 | 132 |
log.info('create_network %s', req) |
133 | 133 |
|
134 | 134 |
network_dict = api.utils.get_attribute(req, "network", |
... | ... | |
165 | 165 |
|
166 | 166 |
@api.api_method(http_method='PUT', user_required=True, logger=log) |
167 | 167 |
def update_network(request, network_id): |
168 |
info = api.utils.get_request_dict(request)
|
|
168 |
info = api.utils.get_json_body(request)
|
|
169 | 169 |
|
170 | 170 |
network = api.utils.get_attribute(info, "network", attr_type=dict, |
171 | 171 |
required=True) |
b/snf-cyclades-app/synnefo/api/ports.py | ||
---|---|---|
104 | 104 |
@transaction.commit_on_success |
105 | 105 |
def create_port(request): |
106 | 106 |
user_id = request.user_uniq |
107 |
req = api.utils.get_request_dict(request)
|
|
107 |
req = api.utils.get_json_body(request)
|
|
108 | 108 |
log.info('create_port %s', req) |
109 | 109 |
|
110 | 110 |
port_dict = api.utils.get_attribute(req, "port", attr_type=dict) |
... | ... | |
200 | 200 |
You can update only name, security_groups |
201 | 201 |
''' |
202 | 202 |
port = util.get_port(port_id, request.user_uniq, for_update=True) |
203 |
req = api.utils.get_request_dict(request)
|
|
203 |
req = api.utils.get_json_body(request)
|
|
204 | 204 |
|
205 | 205 |
port_info = api.utils.get_attribute(req, "port", required=True, |
206 | 206 |
attr_type=dict) |
b/snf-cyclades-app/synnefo/api/servers.py | ||
---|---|---|
369 | 369 |
# badRequest (400), |
370 | 370 |
# serverCapacityUnavailable (503), |
371 | 371 |
# overLimit (413) |
372 |
req = utils.get_request_dict(request)
|
|
372 |
req = utils.get_json_body(request)
|
|
373 | 373 |
log.info('create_server %s', req) |
374 | 374 |
user_id = request.user_uniq |
375 | 375 |
|
... | ... | |
445 | 445 |
# buildInProgress (409), |
446 | 446 |
# overLimit (413) |
447 | 447 |
|
448 |
req = utils.get_request_dict(request)
|
|
448 |
req = utils.get_json_body(request)
|
|
449 | 449 |
log.info('update_server_name %s %s', server_id, req) |
450 | 450 |
|
451 | 451 |
req = utils.get_attribute(req, "server", attr_type=dict, required=True) |
... | ... | |
497 | 497 |
@api.api_method(http_method='POST', user_required=True, logger=log) |
498 | 498 |
@transaction.commit_on_success |
499 | 499 |
def demux_server_action(request, server_id): |
500 |
req = utils.get_request_dict(request)
|
|
500 |
req = utils.get_json_body(request)
|
|
501 | 501 |
log.debug('server_action %s %s', server_id, req) |
502 | 502 |
|
503 | 503 |
if not isinstance(req, dict) and len(req) != 1: |
... | ... | |
596 | 596 |
# badMediaType(415), |
597 | 597 |
# overLimit (413) |
598 | 598 |
|
599 |
req = utils.get_request_dict(request)
|
|
599 |
req = utils.get_json_body(request)
|
|
600 | 600 |
log.info('update_server_metadata %s %s', server_id, req) |
601 | 601 |
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True) |
602 | 602 |
metadata = utils.get_attribute(req, "metadata", required=True, |
... | ... | |
645 | 645 |
# badMediaType(415), |
646 | 646 |
# overLimit (413) |
647 | 647 |
|
648 |
req = utils.get_request_dict(request)
|
|
648 |
req = utils.get_json_body(request)
|
|
649 | 649 |
log.info('create_server_metadata_item %s %s %s', server_id, key, req) |
650 | 650 |
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True) |
651 | 651 |
try: |
b/snf-cyclades-app/synnefo/api/subnets.py | ||
---|---|---|
101 | 101 |
network_id and the desired cidr are mandatory, everything else is optional |
102 | 102 |
|
103 | 103 |
""" |
104 |
dictionary = utils.get_request_dict(request)
|
|
104 |
dictionary = utils.get_json_body(request)
|
|
105 | 105 |
log.info('create subnet %s', dictionary) |
106 | 106 |
|
107 | 107 |
try: |
... | ... | |
187 | 187 |
|
188 | 188 |
""" |
189 | 189 |
|
190 |
dictionary = utils.get_request_dict(request)
|
|
190 |
dictionary = utils.get_json_body(request)
|
|
191 | 191 |
user_id = request.user_uniq |
192 | 192 |
|
193 | 193 |
try: |
b/snf-cyclades-app/synnefo/plankton/views.py | ||
---|---|---|
403 | 403 |
""" |
404 | 404 |
|
405 | 405 |
log.debug('update_image_members %s', image_id) |
406 |
data = api.utils.get_json_body(request) |
|
406 | 407 |
members = [] |
407 |
try: |
|
408 |
data = json.loads(request.body) |
|
409 |
for member in data['memberships']: |
|
410 |
members.append(member['member_id']) |
|
411 |
except (ValueError, KeyError, TypeError): |
|
412 |
return HttpResponse(status=400) |
|
408 |
|
|
409 |
memberships = api.utils.get_attribute(data, "memberships", attr_type=list) |
|
410 |
for member in memberships: |
|
411 |
if not isinstance(member, dict): |
|
412 |
raise faults.BadRequest("Invalid 'memberships' field") |
|
413 |
member = api.utils.get_attribute(member, "member_id") |
|
414 |
members.append(member) |
|
413 | 415 |
|
414 | 416 |
with image_backend(request.user_uniq) as backend: |
415 | 417 |
backend.replace_users(image_id, members) |
b/snf-django-lib/snf_django/lib/api/utils.py | ||
---|---|---|
88 | 88 |
return utc_since |
89 | 89 |
|
90 | 90 |
|
91 |
def get_request_dict(request):
|
|
92 |
"""Return data sent by the client as python dictionary.
|
|
91 |
def get_json_body(request):
|
|
92 |
"""Get the JSON request body as a Python object.
|
|
93 | 93 |
|
94 |
Only JSON format is supported |
|
94 |
Check that that content type is json and deserialize the body of the |
|
95 |
request that contains a JSON document to a Python object. |
|
95 | 96 |
|
96 | 97 |
""" |
97 | 98 |
data = request.body |
... | ... | |
101 | 102 |
if content_type.startswith("application/json"): |
102 | 103 |
try: |
103 | 104 |
return json.loads(data) |
105 |
except UnicodeDecodeError: |
|
106 |
raise faults.BadRequest("Could not decode request as UTF-8 string") |
|
104 | 107 |
except ValueError: |
105 |
raise faults.BadRequest("Invalid JSON data")
|
|
108 |
raise faults.BadRequest("Could not decode request body as JSON")
|
|
106 | 109 |
else: |
107 | 110 |
raise faults.BadRequest("Unsupported Content-type: '%s'" % |
108 | 111 |
content_type) |
Also available in: Unified diff