Skip to content

Commit b844bff

Browse files
committed
SDK-765: Refactor attribute parsing: extract to attribute_parser
1 parent 770d80e commit b844bff

File tree

8 files changed

+74
-63
lines changed

8 files changed

+74
-63
lines changed

yoti_python_sdk/activity_details.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import logging
55
from datetime import datetime
66

7-
from yoti_python_sdk import config
7+
from yoti_python_sdk import attribute_parser, config
88
from yoti_python_sdk.profile import Profile
9-
from yoti_python_sdk.protobuf.protobuf import Protobuf
109

1110

1211
class ActivityDetails:
@@ -21,7 +20,7 @@ def __init__(self, receipt, decrypted_profile=None):
2120

2221
for field in decrypted_profile_attributes: # will be removed in v3.0.0
2322
try:
24-
value = Protobuf().value_based_on_content_type(
23+
value = attribute_parser.value_based_on_content_type(
2524
field.value,
2625
field.content_type
2726
)
@@ -62,14 +61,14 @@ def __init__(self, receipt, decrypted_profile=None):
6261
self.timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
6362

6463
def try_parse_selfie_field(self, field):
65-
self.base64_selfie_uri = Protobuf().image_uri_based_on_content_type(
64+
self.base64_selfie_uri = attribute_parser.image_uri_based_on_content_type(
6665
field.value,
6766
field.content_type
6867
)
6968

7069
def try_parse_age_verified_field(self, field):
7170
if field is not None:
72-
age_verified = Protobuf().value_based_on_content_type(
71+
age_verified = attribute_parser.value_based_on_content_type(
7372
field.value,
7473
field.content_type
7574
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
import collections
5+
import json
6+
import logging
7+
8+
from cryptography.fernet import base64
9+
10+
from yoti_python_sdk.protobuf.protobuf import Protobuf
11+
12+
13+
def value_based_on_content_type(value, content_type=None):
14+
from yoti_python_sdk.image import Image
15+
if content_type == Protobuf.CT_STRING:
16+
return value.decode('utf-8')
17+
elif value == b'':
18+
raise ValueError("Content type: '{0}' should not have an empty value".format(content_type))
19+
elif content_type == Protobuf.CT_DATE:
20+
return value.decode('utf-8')
21+
elif content_type in Image.allowed_types():
22+
return Image(value, content_type)
23+
elif content_type == Protobuf.CT_JSON:
24+
return convert_to_dict(value)
25+
elif content_type == Protobuf.CT_INT:
26+
string_value = value.decode('utf-8')
27+
int_value = int(string_value)
28+
return int_value
29+
30+
if logging.getLogger().propagate:
31+
logging.warning("Unknown type '{0}', attempting to parse it as a String".format(content_type))
32+
33+
return value.decode('utf-8')
34+
35+
36+
def image_uri_based_on_content_type(value, content_type=None):
37+
if content_type == Protobuf.CT_JPEG:
38+
data = base64.b64encode(value).decode('utf-8')
39+
return 'data:image/jpeg;base64,{0}'.format(data)
40+
elif content_type == Protobuf.CT_PNG:
41+
data = base64.b64encode(value).decode('utf-8')
42+
return 'data:image/png;base64,{0}'.format(data)
43+
return value
44+
45+
46+
def convert_to_dict(byte_value):
47+
decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict, strict=False)
48+
value_to_decode = byte_value.decode()
49+
50+
return decoder.decode(value_to_decode)

yoti_python_sdk/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ def __read_pem_file(key_file_path, error_source):
5555
raise RuntimeError('{0}: {1}'.format(error, exception))
5656

5757
def get_activity_details(self, encrypted_request_token):
58+
proto = protobuf.Protobuf()
5859
http_method = 'GET'
5960
content = None
6061
response = self.__make_activity_details_request(encrypted_request_token, http_method, content)
6162
receipt = json.loads(response.text).get('receipt')
6263

63-
encrypted_data = protobuf.Protobuf().current_user(receipt)
64+
encrypted_data = proto.current_user(receipt)
6465

6566
if not encrypted_data:
6667
return ActivityDetails(receipt)
@@ -71,7 +72,7 @@ def get_activity_details(self, encrypted_request_token):
7172
encrypted_data.iv,
7273
encrypted_data.cipher_text
7374
)
74-
attribute_list = protobuf.Protobuf().attribute_list(decrypted_data)
75+
attribute_list = proto.attribute_list(decrypted_data)
7576
return ActivityDetails(receipt, attribute_list)
7677

7778
def perform_aml_check(self, aml_profile):

yoti_python_sdk/image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
from yoti_python_sdk import attribute_parser
13
from yoti_python_sdk.protobuf.protobuf import Protobuf
24

35

@@ -30,7 +32,7 @@ def mime_type(self):
3032
return ""
3133

3234
def base64_content(self):
33-
return Protobuf().image_uri_based_on_content_type(
35+
return attribute_parser.image_uri_based_on_content_type(
3436
# TODO: move image_uri_based_on_content_type method to this class
3537
self.__data,
3638
self.__content_type)

yoti_python_sdk/profile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import logging
33

4-
from yoti_python_sdk import config
4+
from yoti_python_sdk import attribute_parser, config
55
from yoti_python_sdk.anchor import Anchor
66
from yoti_python_sdk.attribute import Attribute
77
from yoti_python_sdk.image import Image
8-
from yoti_python_sdk.protobuf.protobuf import Protobuf
98

109

1110
class Profile:
@@ -15,7 +14,7 @@ def __init__(self, profile_attributes):
1514
if profile_attributes:
1615
for field in profile_attributes:
1716
try:
18-
value = Protobuf().value_based_on_content_type(
17+
value = attribute_parser.value_based_on_content_type(
1918
field.value,
2019
field.content_type
2120
)
Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
import collections
5-
import json
6-
import logging
7-
84
from cryptography.fernet import base64
95
from yoti_python_sdk.protobuf.attribute_public_api import Attribute_pb2, List_pb2
106
from yoti_python_sdk.protobuf.common_public_api import EncryptedData_pb2
@@ -43,41 +39,3 @@ def anchor(data):
4339
anchor = Attribute_pb2.Anchor()
4440
anchor.MergeFromString(data)
4541
return anchor
46-
47-
def value_based_on_content_type(self, value, content_type=None):
48-
from yoti_python_sdk.image import Image
49-
if content_type == self.CT_STRING:
50-
return value.decode('utf-8')
51-
elif value == b'':
52-
raise ValueError("Content type: '{0}' should not have an empty value".format(content_type))
53-
elif content_type == self.CT_DATE:
54-
return value.decode('utf-8')
55-
elif content_type in Image.allowed_types():
56-
return Image(value, content_type)
57-
elif content_type == self.CT_JSON:
58-
return self.convert_to_dict(value)
59-
elif content_type == self.CT_INT:
60-
string_value = value.decode('utf-8')
61-
int_value = int(string_value)
62-
return int_value
63-
64-
if logging.getLogger().propagate:
65-
logging.warning("Unknown type '{0}', attempting to parse it as a String".format(content_type))
66-
67-
return value.decode('utf-8')
68-
69-
def image_uri_based_on_content_type(self, value, content_type=None):
70-
if content_type == self.CT_JPEG:
71-
data = base64.b64encode(value).decode('utf-8')
72-
return 'data:image/jpeg;base64,{0}'.format(data)
73-
elif content_type == self.CT_PNG:
74-
data = base64.b64encode(value).decode('utf-8')
75-
return 'data:image/png;base64,{0}'.format(data)
76-
return value
77-
78-
@staticmethod
79-
def convert_to_dict(byte_value):
80-
decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict, strict=False)
81-
value_to_decode = byte_value.decode()
82-
83-
return decoder.decode(value_to_decode)

yoti_python_sdk/tests/test_protobuf.py renamed to yoti_python_sdk/tests/test_attribute_parser.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from yoti_python_sdk import attribute_parser
67
from yoti_python_sdk.protobuf import protobuf
78

89
STRING_VALUE = "123"
@@ -20,23 +21,23 @@ def proto():
2021
[(proto().CT_STRING, STRING_VALUE),
2122
(proto().CT_DATE, STRING_VALUE),
2223
(proto().CT_INT, INT_VALUE)])
23-
def test_protobuf_values_based_on_content_type(content_type, expected_value):
24-
result = proto().value_based_on_content_type(BYTE_VALUE, content_type)
24+
def test_attribute_parser_values_based_on_content_type(content_type, expected_value):
25+
result = attribute_parser.value_based_on_content_type(BYTE_VALUE, content_type)
2526
assert result == expected_value
2627

2728

28-
def test_protobuf_values_based_on_other_content_types(proto):
29+
def test_attribute_parser_values_based_on_other_content_types(proto):
2930
# disable logging for the below types: warning shown as type is not recognized
3031
logger = logging.getLogger()
3132
logger.propagate = False
3233

33-
result = proto.value_based_on_content_type(BYTE_VALUE, proto.CT_UNDEFINED)
34+
result = attribute_parser.value_based_on_content_type(BYTE_VALUE, proto.CT_UNDEFINED)
3435
assert result == STRING_VALUE
3536

36-
result = proto.value_based_on_content_type(BYTE_VALUE)
37+
result = attribute_parser.value_based_on_content_type(BYTE_VALUE)
3738
assert result == STRING_VALUE
3839

39-
result = proto.value_based_on_content_type(BYTE_VALUE, 100)
40+
result = attribute_parser.value_based_on_content_type(BYTE_VALUE, 100)
4041
assert result == STRING_VALUE
4142

4243
logger.propagate = True
@@ -47,16 +48,16 @@ def test_protobuf_values_based_on_other_content_types(proto):
4748
(proto().CT_JPEG,
4849
proto().CT_PNG))
4950
def test_image_value_based_on_content_type(proto, content_type):
50-
result = proto.value_based_on_content_type(BYTE_VALUE, content_type)
51+
result = attribute_parser.value_based_on_content_type(BYTE_VALUE, content_type)
5152
assert result.data == BYTE_VALUE
5253
assert result.content_type == content_type
5354

5455

55-
def test_protobuf_image_uri_based_on_content_type(proto):
56+
def test_attribute_parser_image_uri_based_on_content_type(proto):
5657
value = b'test string'
5758

58-
result = proto.image_uri_based_on_content_type(value, proto.CT_JPEG)
59+
result = attribute_parser.image_uri_based_on_content_type(value, proto.CT_JPEG)
5960
assert result == 'data:image/jpeg;base64,dGVzdCBzdHJpbmc='
6061

61-
result = proto.image_uri_based_on_content_type(value, proto.CT_PNG)
62+
result = attribute_parser.image_uri_based_on_content_type(value, proto.CT_PNG)
6263
assert result == 'data:image/png;base64,dGVzdCBzdHJpbmc='

yoti_python_sdk/tests/test_image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
import pytest
23

34
from yoti_python_sdk.image import Image

0 commit comments

Comments
 (0)