Skip to content

Commit 0020afa

Browse files
authored
Merge pull request #90 from cloudblue/python-decode-issue
Added mechanism to avoid decode issue between python 2 and 3
2 parents 176644e + 6bbf700 commit 0020afa

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19.7
1+
19.8

apsconnectcli/hub.py

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ def _get_extension_id(self):
148148
url = 'aps/2/resources?implementing(http://odin.com/servicesSelector/globals/2.0)'
149149
r = self.aps.get(url)
150150
try:
151-
data = json.loads(r.content.decode('utf-8'))
151+
if sys.version_info.major < 3 or (
152+
sys.version_info.major == 3 and sys.version_info.minor < 6
153+
):
154+
data = json.loads(r.content.decode('utf-8'))
155+
else:
156+
data = json.loads(r.content)
152157
except ValueError:
153158
print("APSController provided non-json format")
154159
sys.exit(1)
@@ -161,7 +166,12 @@ def _get_id(self):
161166
r.raise_for_status()
162167

163168
try:
164-
data = json.loads(r.content.decode('utf-8'))
169+
if sys.version_info.major < 3 or (
170+
sys.version_info.major == 3 and sys.version_info.minor < 6
171+
):
172+
data = json.loads(r.content.decode('utf-8'))
173+
else:
174+
data = json.loads(r.content)
165175
except ValueError:
166176
print("APSController provided non-json format")
167177
sys.exit(1)
@@ -188,7 +198,12 @@ def check_package_operation(self, package):
188198
url = '/aps/2/resources/{}'.format(app_instances[0]['application_resource_id'])
189199
r = self.aps.get(url)
190200
try:
191-
data = json.loads(r.content.decode('utf-8'))
201+
if sys.version_info.major < 3 or (
202+
sys.version_info.major == 3 and sys.version_info.minor < 6
203+
):
204+
data = json.loads(r.content.decode('utf-8'))
205+
else:
206+
data = json.loads(r.content)
192207
except ValueError:
193208
print("APSController provided non-json format")
194209
sys.exit(1)
@@ -327,7 +342,12 @@ def _get_item_info_from_local_id(self, product, local_id):
327342
r = self.aps.post('aps/2/resources/{}/itemInfo'.format(self.extension_id), json=payload)
328343
try:
329344
apsapi_raise_for_status(r)
330-
data = json.loads(r.content.decode('utf-8'))
345+
if sys.version_info.major < 3 or (
346+
sys.version_info.major == 3 and sys.version_info.minor < 6
347+
):
348+
data = json.loads(r.content.decode('utf-8'))
349+
else:
350+
data = json.loads(r.content)
331351
except ValueError:
332352
print("Error while decoding item information")
333353
sys.exit(1)
@@ -377,7 +397,12 @@ def _exists_item_profile_resource(self, package):
377397
r = self.aps.get('aps/2/resources?implementing({}/{}/{}.{})'.format(
378398
package.connector_id, "itemProfile", package.version, package.release))
379399
try:
380-
data = json.loads(r.content.decode('utf-8'))
400+
if sys.version_info.major < 3 or (
401+
sys.version_info.major == 3 and sys.version_info.minor < 6
402+
):
403+
data = json.loads(r.content.decode('utf-8'))
404+
else:
405+
data = json.loads(r.content)
381406
if data[0]['aps']['id']:
382407
return True
383408
return False
@@ -392,18 +417,32 @@ def _get_existing_ref_rts(self, app_id):
392417
rts = self.osaapi.getResourceTypesByClass(**payload)
393418
existing_resources = []
394419
for resource in rts['result']:
395-
aps_rt = json.loads(
396-
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
397-
resource['resource_type_id'])).content.decode('utf-8'))
420+
if sys.version_info.major < 3 or (
421+
sys.version_info.major == 3 and sys.version_info.minor < 6
422+
):
423+
aps_rt = json.loads(
424+
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
425+
resource['resource_type_id'])).content.decode('utf-8'))
426+
else:
427+
aps_rt = json.loads(
428+
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
429+
resource['resource_type_id'])).content)
398430
if 'app_id' in aps_rt['activationParameters'] and int(aps_rt['activationParameters'][
399431
'app_id']) == int(app_id):
400432
existing_resources.append(aps_rt['activationParameters']['resource_id'])
401433
return existing_resources
402434

403435
def _find_existing(self, resource, app_id):
404-
aps_rt = json.loads(
405-
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
406-
resource['resource_type_id'])).content.decode('utf-8'))
436+
if sys.version_info.major < 3 or (
437+
sys.version_info.major == 3 and sys.version_info.minor < 6
438+
):
439+
aps_rt = json.loads(
440+
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
441+
resource['resource_type_id'])).content.decode('utf-8'))
442+
else:
443+
aps_rt = json.loads(
444+
self.aps.get('aps/2/services/resource-type-manager/resourceTypes/{}'.format(
445+
resource['resource_type_id'])).content)
407446
if 'app_id' in aps_rt['activationParameters'] and int(
408447
aps_rt['activationParameters'][
409448
'app_id']) == int(app_id):
@@ -589,7 +628,12 @@ def check_connect_hub_app_installed(self):
589628
url = 'aps/2/resources?implementing(http://odin.com/servicesSelector/globals/2.0)'
590629
r = self.aps.get(url)
591630
try:
592-
data = json.loads(r.content.decode('utf-8'))
631+
if sys.version_info.major < 3 or (
632+
sys.version_info.major == 3 and sys.version_info.minor < 6
633+
):
634+
data = json.loads(r.content.decode('utf-8'))
635+
else:
636+
data = json.loads(r.content)
593637
except ValueError:
594638
print("APSController provided non-json format")
595639
sys.exit(1)
@@ -622,7 +666,12 @@ def get_connections(self, product_id):
622666
}
623667
r = self.aps.post(url, json=payload)
624668
try:
625-
data = json.loads(r.content.decode('utf-8'))
669+
if sys.version_info.major < 3 or (
670+
sys.version_info.major == 3 and sys.version_info.minor < 6
671+
):
672+
data = json.loads(r.content.decode('utf-8'))
673+
else:
674+
data = json.loads(r.content)
626675
if data.get('id'):
627676
return data
628677
print("ERROR: Product {} has no connection to this HUB.\n".format(product_id) +

apsconnectcli/package.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ def resources(self):
8484
resources = {}
8585

8686
for key in self.tenant_properties:
87-
if self.tenant_properties[key]['type'] in resource_types and key.decode(
88-
'utf-8') != 'COUNTRY':
89-
resources[key] = self.tenant_properties[key]
87+
if sys.version_info.major < 3 or (
88+
sys.version_info.major == 3 and sys.version_info.minor < 6
89+
):
90+
if self.tenant_properties[key]['type'] in resource_types and key.decode(
91+
'utf-8') != 'COUNTRY':
92+
resources[key] = self.tenant_properties[key]
93+
else:
94+
if self.tenant_properties[key]['type'] in resource_types and key != 'COUNTRY':
95+
resources[key] = self.tenant_properties[key]
9096

9197
return resources
9298

tests/test_hub.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def test_hub_incorrect_id(self):
117117
patch('apsconnectcli.hub.get_config'), \
118118
patch('apsconnectcli.hub.osaapi_raise_for_status'), \
119119
patch('apsconnectcli.hub.sys') as sys_mock:
120+
sys_mock.version_info.major = sys.version_info.major
121+
sys_mock.version_info.minor = sys.version_info.minor
120122
resp_mock = MagicMock()
121123
resp_mock.content = b'["aps": {"id": "12345"}}]'
122124
aps_mock.return_value.get.return_value = resp_mock

0 commit comments

Comments
 (0)