From cf08f14bedce7859534857bcba6d159e34082646 Mon Sep 17 00:00:00 2001 From: Tao Zou Date: Wed, 29 Jun 2022 14:30:08 +0800 Subject: [PATCH] Support two json decoder exception If simplejson installed, requests will use simplejson to handle the deserialization of the JSON. if not installed, requests will use json package Change-Id: I64a54d105d6d533c2d75174bbf265b1d372b9b88 --- vmware_nsxlib/v3/client.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vmware_nsxlib/v3/client.py b/vmware_nsxlib/v3/client.py index fedddeed..2e3fb4d8 100644 --- a/vmware_nsxlib/v3/client.py +++ b/vmware_nsxlib/v3/client.py @@ -13,11 +13,15 @@ # License for the specific language governing permissions and limitations # under the License. # -import json import re import time from urllib import parse as urlparse +try: + from simplejson.errors import JSONDecodeError +except ImportError: + from json.decoder import JSONDecodeError + from oslo_log import log from oslo_serialization import jsonutils import requests @@ -26,7 +30,6 @@ from vmware_nsxlib._i18n import _ from vmware_nsxlib.v3 import constants from vmware_nsxlib.v3 import exceptions from vmware_nsxlib.v3 import utils - LOG = log.getLogger(__name__) NULL_CURSOR_PREFIX = '0000' @@ -35,7 +38,7 @@ NULL_CURSOR_PREFIX = '0000' def get_http_error_details(response): try: msg = response.json() if response.content else '' - except json.decoder.JSONDecodeError as e: + except JSONDecodeError as e: LOG.debug("decode response %s error %s", response.content, e) # error_code can't be None else it can't be casted to an exception return {'status_code': response.status_code,