Skip to content

Commit

Permalink
set up model classes to ignore unknown dict keys
Browse files Browse the repository at this point in the history
  • Loading branch information
adorton-adobe committed Sep 21, 2022
1 parent 3dcb5de commit fbeb468
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
25 changes: 18 additions & 7 deletions sign_client/sign_client/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ def default(self, o):
return new_dct


def remove_unknown_keys(dct: dict, cls):
known_keys = set()
for field in dataclasses.fields(cls):
known_keys.add(field.name)
new_dct = {}
for k, v in dct.items():
if k in known_keys:
new_dct[k] = v
return new_dct


@dataclass
class PageInfo:
nextCursor: str = None

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand All @@ -62,7 +73,7 @@ class UserInfo:

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand All @@ -83,7 +94,7 @@ class UserStateInfo:

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand All @@ -106,7 +117,7 @@ class DetailedUserInfo:

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand All @@ -118,7 +129,7 @@ class GroupInfo:

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand All @@ -130,7 +141,7 @@ class DetailedGroupInfo:

@classmethod
def from_dict(cls, dct):
return cls(**dct)
return cls(**remove_unknown_keys(dct, cls))


@dataclass
Expand Down Expand Up @@ -167,7 +178,7 @@ def from_dict(cls, dct):
if dct is None:
return None
new_dct = {k: BooleanSettingsInfo.from_dict(v) for k, v in dct.items()}
return cls(**new_dct)
return cls(**remove_unknown_keys(new_dct, cls))


@dataclass
Expand Down
1 change: 1 addition & 0 deletions sign_client/sign_client_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

43 changes: 43 additions & 0 deletions sign_client/sign_client_tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from sign_client.model import UserGroupsInfo
import json

def test_deserialize():
"""Test deserialization of a Sign API response"""
# `widgetCreationVisible` is not part of the UserGroupsInfo
# test will pass if user_groups_info is constructed with no errors

# user permission info
api_resp_json = """
{
"groupInfoList": [
{
"id": "",
"isGroupAdmin": false,
"isPrimaryGroup": false,
"status": "",
"createdDate": "date",
"name": "",
"settings": {
"libaryDocumentCreationVisible": {
"value": false,
"inherited": false
},
"sendRestrictedToWorkflows": {
"value": false,
"inherited": false
},
"userCanSend": {
"value": false,
"inherited": false
},
"widgetCreationVisible": {
"value": false,
"inherited": false
}
}
}
]
}
"""
dct = json.loads(api_resp_json)
UserGroupsInfo.from_dict(dct)

0 comments on commit fbeb468

Please sign in to comment.