From 2698637dc4118300d7deb402d90c5e410e61d342 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson <eandersson@blizzard.com> Date: Sun, 27 Oct 2019 19:20:00 -0700 Subject: [PATCH] Clean up pecan Request patching * Removed hack for old pecan version. * Cleaned up code. Change-Id: I428ee4f1bf5de7cd369e3f82b059c0517f37e804 --- designate/api/v2/patches.py | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/designate/api/v2/patches.py b/designate/api/v2/patches.py index f15f04a66..09637c1e4 100644 --- a/designate/api/v2/patches.py +++ b/designate/api/v2/patches.py @@ -13,9 +13,6 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -from inspect import ismethod -from inspect import getargspec - import six from oslo_serialization import jsonutils import pecan.core @@ -35,25 +32,18 @@ class Request(pecan.core.Request): We add this method to ease future XML support, so the main code is not hardcoded to call pecans "request.json" method. """ - if self.content_type in JSON_TYPES: - try: - json_dict = jsonutils.load(self.body_file) - if json_dict is None: - # NOTE(kiall): Somehow, json.load(fp) is returning None. - raise exceptions.EmptyRequestBody('Request Body is empty') - return json_dict - except ValueError as valueError: - if len(self.body) == 0 or self.body is None: - raise exceptions.EmptyRequestBody('Request Body is empty') - else: - raise exceptions.InvalidJson(six.text_type(valueError)) - else: + if self.content_type not in JSON_TYPES: raise exceptions.UnsupportedContentType( 'Content-type must be application/json') -__init__ = pecan.core.Pecan.__base__.__init__ -if not ismethod(__init__) or 'request_cls' not in getargspec(__init__).args: - # only attempt to monkey patch `pecan.Request` in older versions of pecan; - # newer versions support specifying a custom request implementation in the - # `pecan.core.Pecan` constructor via the `request_cls` argument - pecan.core.Request = Request + try: + json_dict = jsonutils.load(self.body_file) + if json_dict is None: + # NOTE(kiall): Somehow, json.load(fp) is returning None. + raise exceptions.EmptyRequestBody('Request Body is empty') + return json_dict + except ValueError as e: + if not self.body: + raise exceptions.EmptyRequestBody('Request Body is empty') + else: + raise exceptions.InvalidJson(six.text_type(e))