Skip to content

Commit b8068e9

Browse files
committed
SDK-765: Remove redundant image_uri_based_on_content_type Image.base64_image
1 parent b844bff commit b8068e9

File tree

5 files changed

+12
-43
lines changed

5 files changed

+12
-43
lines changed

yoti_python_sdk/activity_details.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, receipt, decrypted_profile=None):
2626
)
2727

2828
if field.name == config.ATTRIBUTE_SELFIE:
29-
self.try_parse_selfie_field(field)
30-
value = field.value
29+
self.base64_selfie_uri = value.base64_content()
30+
value = field.value # set value to be byte content, for backwards compatibility
3131

3232
self.user_profile[field.name] = value
3333

@@ -60,12 +60,6 @@ def __init__(self, receipt, decrypted_profile=None):
6060
if timestamp is not None:
6161
self.timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
6262

63-
def try_parse_selfie_field(self, field):
64-
self.base64_selfie_uri = attribute_parser.image_uri_based_on_content_type(
65-
field.value,
66-
field.content_type
67-
)
68-
6963
def try_parse_age_verified_field(self, field):
7064
if field is not None:
7165
age_verified = attribute_parser.value_based_on_content_type(

yoti_python_sdk/attribute_parser.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import json
66
import logging
77

8-
from cryptography.fernet import base64
9-
108
from yoti_python_sdk.protobuf.protobuf import Protobuf
119

1210

@@ -33,16 +31,6 @@ def value_based_on_content_type(value, content_type=None):
3331
return value.decode('utf-8')
3432

3533

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-
4634
def convert_to_dict(byte_value):
4735
decoder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict, strict=False)
4836
value_to_decode = byte_value.decode()

yoti_python_sdk/image.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
from yoti_python_sdk import attribute_parser
2+
from cryptography.fernet import base64
3+
34
from yoti_python_sdk.protobuf.protobuf import Protobuf
45

56

@@ -32,7 +33,11 @@ def mime_type(self):
3233
return ""
3334

3435
def base64_content(self):
35-
return attribute_parser.image_uri_based_on_content_type(
36-
# TODO: move image_uri_based_on_content_type method to this class
37-
self.__data,
38-
self.__content_type)
36+
if self.__content_type == Protobuf.CT_JPEG:
37+
data = base64.b64encode(self.__data).decode('utf-8')
38+
return 'data:image/jpeg;base64,{0}'.format(data)
39+
elif self.__content_type == Protobuf.CT_PNG:
40+
data = base64.b64encode(self.__data).decode('utf-8')
41+
return 'data:image/png;base64,{0}'.format(data)
42+
43+
raise TypeError("Content type '{0}' is not a supported image type".format(self.__content_type))

yoti_python_sdk/tests/test_activity_details.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ def create_structured_postal_address_field(activity_details, json_address_value)
8888
activity_details.field.content_type = Protobuf.CT_JSON
8989

9090

91-
def test_try_parse_selfie_field_valid_selfie():
92-
activity_details = ActivityDetails(successful_receipt())
93-
create_selfie_field(activity_details)
94-
95-
ActivityDetails.try_parse_selfie_field(activity_details, activity_details.field)
96-
assert activity_details.base64_selfie_uri is not None
97-
98-
9991
def test_try_parse_age_verified_both_missing_not_parsed():
10092
activity_details = ActivityDetails(successful_receipt())
10193
field = None

yoti_python_sdk/tests/test_attribute_parser.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,3 @@ def test_image_value_based_on_content_type(proto, content_type):
5151
result = attribute_parser.value_based_on_content_type(BYTE_VALUE, content_type)
5252
assert result.data == BYTE_VALUE
5353
assert result.content_type == content_type
54-
55-
56-
def test_attribute_parser_image_uri_based_on_content_type(proto):
57-
value = b'test string'
58-
59-
result = attribute_parser.image_uri_based_on_content_type(value, proto.CT_JPEG)
60-
assert result == 'data:image/jpeg;base64,dGVzdCBzdHJpbmc='
61-
62-
result = attribute_parser.image_uri_based_on_content_type(value, proto.CT_PNG)
63-
assert result == 'data:image/png;base64,dGVzdCBzdHJpbmc='

0 commit comments

Comments
 (0)