Commit 2b0a9c6a authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis

astakosclient: Raise BadValue when failing to parse request data

parent d9258b6d
......@@ -38,7 +38,7 @@ import hashlib
from copy import copy
import simplejson
from astakosclient.utils import retry, scheme_to_class
from astakosclient.utils import retry, scheme_to_class, parse_request
from astakosclient.errors import \
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \
NoUserName, NoUUID, BadValue, QuotaLimit, InvalidResponse
......@@ -170,6 +170,8 @@ class AstakosClient():
else:
return ""
except Exception as err:
self.logger.error("Cannot parse response \"%s\" with simplejson: %s"
% (data, str(err)))
raise InvalidResponse(str(err), data)
# ------------------------
......@@ -196,7 +198,7 @@ class AstakosClient():
# with {'uuids': uuids}
def _uuid_catalog(self, token, uuids, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'uuids': uuids})
req_body = parse_request({'uuids': uuids}, self.logger)
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
if "uuid_catalog" in data:
......@@ -255,7 +257,7 @@ class AstakosClient():
# with {'displaynames': display_names}
def _displayname_catalog(self, token, display_names, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'displaynames': display_names})
req_body = parse_request({'displaynames': display_names}, self.logger)
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
if "displayname_catalog" in data:
......@@ -349,7 +351,7 @@ class AstakosClient():
"""
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps(request)
req_body = parse_request(request, self.logger)
try:
response = self._call_astakos(token, "/astakos/api/commissions",
req_headers, req_body, "POST")
......@@ -426,7 +428,7 @@ class AstakosClient():
path = "/astakos/api/commissions/" + str(serial) + "/action"
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({str(action): ""})
req_body = parse_request({str(action): ""}, self.logger)
self._call_astakos(token, path, req_headers, req_body, "POST")
def issue_commission_accept(self, token, serial):
......
......@@ -34,8 +34,9 @@
from httplib import HTTPConnection, HTTPSConnection
from contextlib import closing
import simplejson
from objpool.http import PooledHTTPConnection
from astakosclient.errors import AstakosClientException
from astakosclient.errors import AstakosClientException, BadValue
def retry(func):
......@@ -81,3 +82,14 @@ def scheme_to_class(scheme, use_pool, pool_size):
return _https_connection
else:
return None
def parse_request(request, logger):
"""Parse request with simplejson to convert it to string"""
try:
return simplejson.dumps(request)
except Exception as err:
m = "Cannot parse request \"%s\" with simplejson: %s" \
% (request, str(err))
logger.error(m)
raise BadValue(m)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment