From a23ee8cb6a9046fb0a2f679a441b24a8e70c5a18 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Wed, 16 Jan 2019 20:09:14 -0800 Subject: [PATCH] Use oslo_serialization instead of the json module directly * Always use oslo jsonutils. * Consistently import jsonutils as-is. * Use dump_as_bytes instead of dumps. https://wiki.openstack.org/wiki/Python3#Serialization:_base64.2C_JSON.2C_etc. Change-Id: Ia58b2a6a8740fa7f0796b05e14e812d5240c7acc --- designate/api/middleware.py | 8 ++++---- designate/backend/impl_dynect.py | 4 ++-- designate/backend/impl_infoblox/connector.py | 11 +++++------ designate/tests/test_backend/test_dynect.py | 4 ++-- designate/tests/test_notification_handler/__init__.py | 9 ++++++--- designate/utils.py | 4 ++-- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/designate/api/middleware.py b/designate/api/middleware.py index 33259a3cd..2325ec951 100644 --- a/designate/api/middleware.py +++ b/designate/api/middleware.py @@ -21,7 +21,7 @@ import oslo_messaging as messaging from oslo_log import log as logging from oslo_middleware import base from oslo_middleware import request_id -from oslo_serialization import jsonutils as json +from oslo_serialization import jsonutils from oslo_utils import strutils from designate import exceptions @@ -130,7 +130,7 @@ class KeystoneContextMiddleware(ContextMiddleware): return flask.Response(status=401) if headers.get('X-Service-Catalog'): - catalog = json.loads(headers.get('X-Service-Catalog')) + catalog = jsonutils.loads(headers.get('X-Service-Catalog')) else: catalog = None @@ -301,7 +301,7 @@ class FaultWrapperMiddleware(base.Middleware): LOG.error('Missing context in request, please check.') return flask.Response(status=status, headers=headers, - response=json.dumps(response)) + response=jsonutils.dump_as_bytes(response)) class ValidationErrorMiddleware(base.Middleware): @@ -352,7 +352,7 @@ class ValidationErrorMiddleware(base.Middleware): LOG.error('Missing context in request, please check.') return flask.Response(status=exception.error_code, headers=headers, - response=json.dumps(response)) + response=jsonutils.dump_as_bytes(response)) class APIv2ValidationErrorMiddleware(ValidationErrorMiddleware): diff --git a/designate/backend/impl_dynect.py b/designate/backend/impl_dynect.py index 9f43dd82a..8c178f60b 100755 --- a/designate/backend/impl_dynect.py +++ b/designate/backend/impl_dynect.py @@ -13,12 +13,12 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import json import time from eventlet import Timeout from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils import requests from requests.adapters import HTTPAdapter @@ -197,7 +197,7 @@ class DynClient(object): self._http_log_req(method, url, kwargs) # NOTE: Set it back to the original data and serialize it. - kwargs['data'] = json.dumps(data) + kwargs['data'] = jsonutils.dump_as_bytes(data) else: self._http_log_req(method, url, kwargs) diff --git a/designate/backend/impl_infoblox/connector.py b/designate/backend/impl_infoblox/connector.py index fb9db7b31..2f51f0a68 100644 --- a/designate/backend/impl_infoblox/connector.py +++ b/designate/backend/impl_infoblox/connector.py @@ -13,9 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -import json as jsonutils - from oslo_log import log +from oslo_serialization import jsonutils from oslo_utils import strutils from six.moves.urllib import parse import requests @@ -132,7 +131,7 @@ class Infoblox(object): headers = {'Content-type': 'application/json'} - data = jsonutils.dumps(payload) + data = jsonutils.dump_as_bytes(payload) url = self._construct_url(objtype, query_params, extattrs) r = self.session.get(url, @@ -175,7 +174,7 @@ class Infoblox(object): headers = {'Content-type': 'application/json'} r = self.session.post(url, - data=jsonutils.dumps(payload), + data=jsonutils.dump_as_bytes(payload), verify=self.sslverify, headers=headers) @@ -203,7 +202,7 @@ class Infoblox(object): headers = {'Content-type': 'application/json'} r = self.session.post(url, - data=jsonutils.dumps(payload), + data=jsonutils.dump_as_bytes(payload), verify=self.sslverify, headers=headers) @@ -232,7 +231,7 @@ class Infoblox(object): headers = {'Content-type': 'application/json'} r = self.session.put(self._construct_url(ref), - data=jsonutils.dumps(payload), + data=jsonutils.dump_as_bytes(payload), verify=self.sslverify, headers=headers) diff --git a/designate/tests/test_backend/test_dynect.py b/designate/tests/test_backend/test_dynect.py index fccd251ba..01adfd4d3 100644 --- a/designate/tests/test_backend/test_dynect.py +++ b/designate/tests/test_backend/test_dynect.py @@ -13,8 +13,8 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import json as json_ +from oslo_serialization import jsonutils from requests_mock.contrib import fixture as req_fixture import testtools @@ -136,7 +136,7 @@ class DynECTTestsCase(BackendTestCase): base_url = 'https://api.dynect.net:443/REST' if json: - kwargs['text'] = json_.dumps(json) + kwargs['text'] = jsonutils.dumps(json) headers = kwargs.setdefault('headers', {}) headers['Content-Type'] = 'application/json' diff --git a/designate/tests/test_notification_handler/__init__.py b/designate/tests/test_notification_handler/__init__.py index d33888284..752bff862 100644 --- a/designate/tests/test_notification_handler/__init__.py +++ b/designate/tests/test_notification_handler/__init__.py @@ -13,9 +13,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import json import os +from oslo_serialization import jsonutils + from designate.tests import resources @@ -29,5 +30,7 @@ class NotificationHandlerMixin(object): if not os.path.exists(filename): raise Exception('Invalid notification fixture requested') - with open(filename, 'r') as fh: - return json.load(fh) + with open(filename, 'r') as fp: + fixture = fp.read() + + return jsonutils.loads(fixture) diff --git a/designate/utils.py b/designate/utils.py index 677731465..8b8c56d8b 100644 --- a/designate/utils.py +++ b/designate/utils.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. import copy -import json import functools import inspect import os @@ -26,6 +25,7 @@ from jinja2 import Template from oslo_config import cfg from oslo_concurrency import processutils from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import timeutils from oslo_utils import uuidutils from oslo_utils.netutils import is_valid_ipv6 @@ -150,7 +150,7 @@ def resource_string(*args): def load_schema(version, name): schema_string = resource_string('schemas', version, '%s.json' % name) - return json.loads(schema_string.decode('utf-8')) + return jsonutils.loads(schema_string.decode('utf-8')) def load_template(template_name):