Skip to content

Commit a506660

Browse files
committed
SDK-594: new Image type
1 parent d95537d commit a506660

File tree

4 files changed

+94
-16
lines changed

4 files changed

+94
-16
lines changed

yoti_python_sdk/activity_details.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import collections
33
import json
4+
import logging
45
from datetime import datetime
56

67
from yoti_python_sdk import config
@@ -19,22 +20,34 @@ def __init__(self, receipt, decrypted_profile=None):
1920
self.profile = Profile(decrypted_profile_attributes)
2021

2122
for field in decrypted_profile_attributes: # will be removed in v3.0.0
22-
value = Protobuf().value_based_on_content_type(
23-
field.value,
24-
field.content_type
25-
)
26-
27-
self.user_profile[field.name] = value
28-
29-
if field.name == config.ATTRIBUTE_SELFIE:
30-
self.try_parse_selfie_field(field)
31-
32-
if field.name.startswith(config.ATTRIBUTE_AGE_OVER) or field.name.startswith(
33-
config.ATTRIBUTE_AGE_UNDER):
34-
self.try_parse_age_verified_field(field)
35-
36-
if field.name == config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS:
37-
self.try_convert_structured_postal_address_to_dict(field)
23+
try:
24+
value = Protobuf().value_based_on_content_type(
25+
field.value,
26+
field.content_type
27+
)
28+
29+
self.user_profile[field.name] = value
30+
31+
if field.name == config.ATTRIBUTE_SELFIE:
32+
self.try_parse_selfie_field(field)
33+
34+
if field.name.startswith(config.ATTRIBUTE_AGE_OVER) or field.name.startswith(
35+
config.ATTRIBUTE_AGE_UNDER):
36+
self.try_parse_age_verified_field(field)
37+
38+
if field.name == config.ATTRIBUTE_STRUCTURED_POSTAL_ADDRESS:
39+
self.try_convert_structured_postal_address_to_dict(field)
40+
41+
except ValueError as ve:
42+
if logging.getLogger().propagate:
43+
logging.warning(ve)
44+
except Exception as exc:
45+
if logging.getLogger().propagate:
46+
logging.warning(
47+
'Error parsing profile attribute:{0}, exception: {1} - {2}'.format(
48+
field.name,
49+
type(exc).__name__,
50+
exc))
3851

3952
self.ensure_postal_address()
4053

yoti_python_sdk/image.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from yoti_python_sdk.protobuf.protobuf import Protobuf
2+
3+
4+
class Image:
5+
def __init__(self, image_bytes, image_content_type):
6+
if image_content_type == Protobuf.CT_JPEG or image_content_type == Protobuf.CT_PNG:
7+
self.__data = image_bytes
8+
self.__content_type = image_content_type
9+
else:
10+
raise TypeError("Content type '{0}' is not a supported image type".format(image_content_type))
11+
12+
@property
13+
def data(self):
14+
return self.__data
15+
16+
@property
17+
def content_type(self):
18+
return self.__content_type
19+
20+
def mime_type(self):
21+
if self.__content_type == Protobuf.CT_JPEG:
22+
return "image/jpeg"
23+
elif self.__content_type == Protobuf.CT_PNG:
24+
return "image/png"
25+
else:
26+
return ""
27+
28+
def base64_content(self):
29+
return Protobuf().image_uri_based_on_content_type(
30+
# TODO: move image_uri_based_on_content_type method to this class
31+
self.__data,
32+
self.__content_type)

yoti_python_sdk/profile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from yoti_python_sdk import config
55
from yoti_python_sdk.anchor import Anchor
66
from yoti_python_sdk.attribute import Attribute
7+
from yoti_python_sdk.image import Image
78
from yoti_python_sdk.protobuf.protobuf import Protobuf
89

910

@@ -19,6 +20,12 @@ def __init__(self, profile_attributes):
1920
field.content_type
2021
)
2122

23+
# first condition will be removed in v3.0.0, so selfie also returns an Image object
24+
if field.name != config.ATTRIBUTE_SELFIE and \
25+
(field.content_type == Protobuf.CT_JPEG \
26+
or field.content_type == Protobuf.CT_PNG):
27+
value = Image(field.value, field.content_type)
28+
2229
anchors = Anchor().parse_anchors(field.anchors)
2330

2431
self.attributes[field.name] = Attribute(field.name, value, anchors)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from yoti_python_sdk.image import Image
4+
from yoti_python_sdk.protobuf.protobuf import Protobuf
5+
6+
7+
def test_image_with_unsupported_type():
8+
with pytest.raises(TypeError):
9+
Image(b'', Protobuf.CT_UNDEFINED)
10+
11+
12+
@pytest.mark.parametrize("content_type, expected_mime_type",
13+
[(Protobuf.CT_JPEG, 'image/jpeg'),
14+
(Protobuf.CT_PNG, 'image/png')])
15+
def test_image_mime_type(content_type, expected_mime_type):
16+
image = Image(b'', content_type)
17+
18+
assert image.mime_type() == expected_mime_type
19+
20+
21+
@pytest.mark.parametrize("content_type, expected_base64_content",
22+
[(Protobuf.CT_JPEG, 'data:image/jpeg;base64,dGVzdCBzdHJpbmc='),
23+
(Protobuf.CT_PNG, 'data:image/png;base64,dGVzdCBzdHJpbmc=')])
24+
def test_image_base64_content(content_type, expected_base64_content):
25+
image = Image(b'test string', content_type)
26+
assert image.base64_content() == expected_base64_content

0 commit comments

Comments
 (0)