Remove oslo serialization dependency

Oslo.serialization has a number of dependencies include msgpack which we
want to avoid. We maintain a couple of conversions for convenience that
oslo serialization did for us.

Change-Id: Iacd08f1d0d5acf4cb15dfaf46b1296aab007879e
This commit is contained in:
Jamie Lennox 2015-05-26 15:13:21 +10:00
parent 5aee35cf6e
commit 06575d3ca5
6 changed files with 54 additions and 38 deletions

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_serialization import jsonutils
from keystoneauth import utils
@ -205,11 +203,9 @@ class LegacyJsonAdapter(Adapter):
resp = super(LegacyJsonAdapter, self).request(*args, **kwargs)
body = None
if resp.text:
try:
body = jsonutils.loads(resp.text)
except ValueError:
pass
try:
body = resp.json()
except ValueError:
body = None
return resp, body

View File

@ -11,15 +11,17 @@
# under the License.
import argparse
import datetime
import functools
import hashlib
import json
import logging
import os
import socket
import time
import uuid
from oslo_config import cfg
from oslo_serialization import jsonutils
from oslo_utils import importutils
import requests
import six
@ -29,6 +31,11 @@ from keystoneauth import exceptions
from keystoneauth.i18n import _, _LI, _LW
from keystoneauth import utils
try:
import netaddr
except ImportError:
netaddr = None
osprofiler_web = importutils.try_import("osprofiler.web")
USER_AGENT = 'keystoneauth'
@ -54,24 +61,17 @@ def request(url, method='GET', **kwargs):
return Session().request(url, method=method, **kwargs)
def _remove_service_catalog(body):
try:
data = jsonutils.loads(body)
class _JSONEncoder(json.JSONEncoder):
# V3 token
if 'token' in data and 'catalog' in data['token']:
data['token']['catalog'] = '<removed>'
return jsonutils.dumps(data)
def default(self, o):
if isinstance(o, datetime.datetime):
return o.isoformat()
if isinstance(o, uuid.UUID):
return six.text_type(o)
if netaddr and isinstance(o, netaddr.IPAddress):
return six.text_type(o)
# V2 token
if 'serviceCatalog' in data['access']:
data['access']['serviceCatalog'] = '<removed>'
return jsonutils.dumps(data)
except Exception:
# Don't fail trying to clean up the request body.
pass
return body
return super(_JSONEncoder, self).default(o)
class Session(object):
@ -142,6 +142,27 @@ class Session(object):
if user_agent is not None:
self.user_agent = user_agent
self._json = _JSONEncoder()
def _remove_service_catalog(self, body):
try:
data = json.loads(body)
# V3 token
if 'token' in data and 'catalog' in data['token']:
data['token']['catalog'] = '<removed>'
return self._json.encode(data)
# V2 token
if 'serviceCatalog' in data['access']:
data['access']['serviceCatalog'] = '<removed>'
return self._json.encode(data)
except Exception:
# Don't fail trying to clean up the request body.
pass
return body
@staticmethod
def _process_header(header):
"""Redacts the secure headers to be logged."""
@ -182,7 +203,7 @@ class Session(object):
string_parts.append('-H "%s: %s"'
% self._process_header(header))
if json:
data = jsonutils.dumps(json)
data = self._json.encode(json)
if data:
string_parts.append("-d '%s'" % data)
@ -201,9 +222,9 @@ class Session(object):
if not headers:
headers = response.headers
if not text:
text = _remove_service_catalog(response.text)
text = self._remove_service_catalog(response.text)
if json:
text = jsonutils.dumps(json)
text = self._json.encode(json)
string_parts = ['RESP:']
@ -351,7 +372,7 @@ class Session(object):
if json is not None:
headers['Content-Type'] = 'application/json'
kwargs['data'] = jsonutils.dumps(json)
kwargs['data'] = self._json.encode(json)
kwargs.setdefault('verify', self.verify)

View File

@ -10,9 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import re
from oslo_serialization import jsonutils
import six
from keystoneauth import discover
@ -75,7 +75,7 @@ V2_URL = "%sv2.0" % BASE_URL
V2_VERSION = fixture.V2Discovery(V2_URL)
V2_VERSION.updated_str = UPDATED
V2_AUTH_RESPONSE = jsonutils.dumps({
V2_AUTH_RESPONSE = json.dumps({
"access": {
"token": {
"expires": "2020-01-01T00:00:10.000123Z",
@ -97,7 +97,7 @@ V3_MEDIA_TYPES = V3_VERSION.media_types
V3_VERSION.updated_str = UPDATED
V3_TOKEN = six.u('3e2813b7ba0b4006840c3825860b86ed'),
V3_AUTH_RESPONSE = jsonutils.dumps({
V3_AUTH_RESPONSE = json.dumps({
"token": {
"methods": [
"token",
@ -209,11 +209,11 @@ GLANCE_EXAMPLES = {
def _create_version_list(versions):
return jsonutils.dumps({'versions': {'values': versions}})
return json.dumps({'versions': {'values': versions}})
def _create_single_version(version):
return jsonutils.dumps({'version': version})
return json.dumps({'version': version})
V3_VERSION_LIST = _create_version_list([V3_VERSION, V2_VERSION])

View File

@ -12,13 +12,13 @@
import argparse
import itertools
import json
import logging
import uuid
import mock
from oslo_config import cfg
from oslo_config import fixture as config
from oslo_serialization import jsonutils
import requests
import six
from testtools import matchers
@ -675,7 +675,7 @@ class AdapterTest(utils.TestCase):
def test_legacy_binding(self):
key = uuid.uuid4().hex
val = uuid.uuid4().hex
response = jsonutils.dumps({key: val})
response = json.dumps({key: val})
self.stub_url('GET', text=response)

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import json as jsonutils
import logging
import sys
import time
@ -18,7 +19,6 @@ import uuid
import fixtures
import mock
from mox3 import mox
from oslo_serialization import jsonutils
import requests
from requests_mock.contrib import fixture
import six

View File

@ -6,7 +6,6 @@ argparse
Babel>=1.3
oslo.config>=1.9.3 # Apache-2.0
oslo.i18n>=1.5.0 # Apache-2.0
oslo.serialization>=1.4.0 # Apache-2.0
oslo.utils>=1.4.0 # Apache-2.0
requests>=2.2.0,!=2.4.0
six>=1.9.0