Skip to content

Commit 2bae20d

Browse files
committed
SDK-2530-python-create-helper-methods-for-the-idv-sdk
1 parent aa55696 commit 2bae20d

4 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎yoti_python_sdk/doc_scan/session/retrieve/get_session_result.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,21 @@ def biometric_consent_timestamp(self):
249249
:rtype: datetime.datetime or None
250250
"""
251251
return self.__biometric_consent_timestamp
252+
253+
def get_resources_for_check(self, check_id):
254+
"""
255+
Returns the resources used by the check with the given check ID
256+
257+
:param check_id: the ID of the check to find resources for
258+
:type check_id: str
259+
:return: a ResourceContainer containing only the resources used by the check
260+
:rtype: ResourceContainer
261+
:raises ValueError: if no check exists with the given check_id
262+
"""
263+
check = next(
264+
(check for check in self.__checks if check.id == check_id), None
265+
)
266+
if check is None:
267+
raise ValueError("Check not found for check_id: {0}".format(check_id))
268+
269+
return self.__resources.filter_for_check(check)

‎yoti_python_sdk/doc_scan/session/retrieve/resource_container.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,34 @@ def share_codes(self):
140140
:rtype: list[ShareCodeResourceResponse]
141141
"""
142142
return self.__share_codes
143+
144+
def filter_for_check(self, check_response):
145+
"""
146+
Returns a new ResourceContainer containing only the resources
147+
referenced by the given check's resources_used
148+
149+
:param check_response: the check to filter resources for
150+
:type check_response: yoti_python_sdk.doc_scan.session.retrieve.check_response.CheckResponse
151+
:return: a ResourceContainer containing only the resources used by the check
152+
:rtype: ResourceContainer
153+
"""
154+
resource_ids = set(check_response.resources_used)
155+
156+
filtered = ResourceContainer()
157+
filtered.__id_documents = self.__filter_resources(
158+
self.__id_documents, resource_ids
159+
)
160+
filtered.__supplementary_documents = self.__filter_resources(
161+
self.__supplementary_documents, resource_ids
162+
)
163+
filtered.__liveness_capture = self.__filter_resources(
164+
self.__liveness_capture, resource_ids
165+
)
166+
filtered.__share_codes = self.__filter_resources(
167+
self.__share_codes, resource_ids
168+
)
169+
return filtered
170+
171+
@staticmethod
172+
def __filter_resources(resources, resource_ids):
173+
return [resource for resource in resources if resource.id in resource_ids]

‎yoti_python_sdk/tests/doc_scan/session/retrieve/test_get_session_result.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from datetime import datetime
44

5+
import pytest
56
import pytz
67

78
from yoti_python_sdk.doc_scan.session.retrieve.check_response import (
@@ -116,6 +117,49 @@ def test_should_filter_checks(self):
116117
SupplementaryDocumentTextDataCheckResponse,
117118
)
118119

120+
def test_get_resources_for_check_should_return_only_resources_used_by_check(self):
121+
data = {
122+
"checks": [
123+
{
124+
"id": "check-1",
125+
"type": "ID_DOCUMENT_AUTHENTICITY",
126+
"resources_used": ["id-doc-1"],
127+
},
128+
{
129+
"id": "check-2",
130+
"type": "LIVENESS",
131+
"resources_used": ["liveness-1"],
132+
},
133+
],
134+
"resources": {
135+
"id_documents": [{"id": "id-doc-1"}, {"id": "id-doc-2"}],
136+
"liveness_capture": [
137+
{"id": "liveness-1", "liveness_type": "ZOOM"},
138+
{"id": "liveness-2", "liveness_type": "ZOOM"},
139+
],
140+
},
141+
}
142+
143+
result = GetSessionResult(data)
144+
145+
resources_for_check = result.get_resources_for_check("check-1")
146+
147+
assert isinstance(resources_for_check, ResourceContainer)
148+
assert [d.id for d in resources_for_check.id_documents] == ["id-doc-1"]
149+
assert resources_for_check.liveness_capture == []
150+
151+
def test_get_resources_for_check_should_raise_for_unknown_check_id(self):
152+
data = {
153+
"checks": [{"id": "check-1", "type": "ID_DOCUMENT_AUTHENTICITY"}],
154+
"resources": {},
155+
}
156+
result = GetSessionResult(data)
157+
158+
with pytest.raises(ValueError) as ex:
159+
result.get_resources_for_check("someUnknownCheckId")
160+
161+
assert "someUnknownCheckId" in str(ex.value)
162+
119163

120164
if __name__ == "__main__":
121165
unittest.main()

‎yoti_python_sdk/tests/doc_scan/session/retrieve/test_resource_container.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
from yoti_python_sdk.doc_scan.session.retrieve.check_response import CheckResponse
34
from yoti_python_sdk.doc_scan.session.retrieve.liveness_resource_response import (
45
LivenessResourceResponse,
56
)
@@ -113,6 +114,68 @@ def test_should_parse_with_missing_share_codes(self):
113114

114115
assert len(result.share_codes) == 0
115116

117+
def test_filter_for_check_should_return_only_referenced_resources(self):
118+
data = {
119+
"id_documents": [{"id": "id-doc-1"}, {"id": "id-doc-2"}],
120+
"supplementary_documents": [
121+
{"id": "supp-doc-1"},
122+
{"id": "supp-doc-2"},
123+
],
124+
"liveness_capture": [
125+
{"id": "liveness-1", "liveness_type": "ZOOM"},
126+
{"id": "liveness-2", "liveness_type": "ZOOM"},
127+
],
128+
"share_codes": [
129+
{"id": "share-code-1", "source": "END_USER", "tasks": []},
130+
{"id": "share-code-2", "source": "END_USER", "tasks": []},
131+
],
132+
}
133+
container = ResourceContainer(data)
134+
check = CheckResponse(
135+
{
136+
"resources_used": [
137+
"id-doc-1",
138+
"supp-doc-1",
139+
"liveness-1",
140+
"share-code-1",
141+
]
142+
}
143+
)
144+
145+
result = container.filter_for_check(check)
146+
147+
assert isinstance(result, ResourceContainer)
148+
assert [d.id for d in result.id_documents] == ["id-doc-1"]
149+
assert [d.id for d in result.supplementary_documents] == ["supp-doc-1"]
150+
assert [d.id for d in result.liveness_capture] == ["liveness-1"]
151+
assert [d.id for d in result.share_codes] == ["share-code-1"]
152+
153+
def test_filter_for_check_should_return_empty_lists_when_no_matches(self):
154+
data = {
155+
"id_documents": [{"id": "id-doc-1"}],
156+
"share_codes": [{"id": "share-code-1", "source": "END_USER", "tasks": []}],
157+
}
158+
container = ResourceContainer(data)
159+
check = CheckResponse({"resources_used": ["someOtherResourceId"]})
160+
161+
result = container.filter_for_check(check)
162+
163+
assert result.id_documents == []
164+
assert result.supplementary_documents == []
165+
assert result.liveness_capture == []
166+
assert result.share_codes == []
167+
168+
def test_filter_for_check_should_return_empty_lists_when_check_has_no_resources_used(
169+
self,
170+
):
171+
data = {"id_documents": [{"id": "id-doc-1"}]}
172+
container = ResourceContainer(data)
173+
check = CheckResponse({})
174+
175+
result = container.filter_for_check(check)
176+
177+
assert result.id_documents == []
178+
116179

117180
if __name__ == "__main__":
118181
unittest.main()

0 commit comments

Comments
 (0)