Skip to content

Commit 97bf9b7

Browse files
added validation to api response
1 parent 23659df commit 97bf9b7

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

uid2_client/identity_map_v3_response.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,19 @@ def __init__(self, status: str, body: Dict[Literal['email_hash', 'phone_hash'],
5757

5858
@classmethod
5959
def from_json(cls, data) -> 'ApiResponse':
60+
if not set(data['body'].keys()).issubset(['email', 'phone', 'email_hash', 'phone_hash']):
61+
raise ValueError("api response body does not contain correct keys")
62+
6063
api_body: Dict[Literal['email_hash', 'phone_hash'], List['ApiIdentity']] = {
61-
'email_hash': [ApiIdentity.from_json(item) for item in data.get('body').get('email_hash', [])] if data.get('body').get('email_hash') else [],
62-
'phone_hash': [ApiIdentity.from_json(item) for item in data.get('body').get('phone_hash', [])] if data.get('body').get('phone_hash') else [],
64+
'email_hash': [ApiIdentity.from_json(item) for item in data['body']['email_hash']] if data['body'].get('email_hash') else [],
65+
'phone_hash': [ApiIdentity.from_json(item) for item in data['body']['phone_hash']] if data['body'].get('phone_hash') else [],
6366
}
6467
return cls(
65-
status=data.get("status"),
68+
status=data['status'],
6669
body=api_body
6770
)
6871

72+
6973
class ApiIdentity:
7074
def __init__(self, current_uid: Optional[str], previous_uid: Optional[str],
7175
refresh_from_seconds: Optional[int], error: Optional[str]):

0 commit comments

Comments
 (0)