forked from p15670423/monkey
Common: Add doctrings for Credential serialization
This commit is contained in:
parent
4d753a808b
commit
6bd0e7dc3a
|
@ -122,6 +122,17 @@ class Credentials:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_mapping(credentials: Mapping) -> Credentials:
|
def from_mapping(credentials: Mapping) -> Credentials:
|
||||||
|
"""
|
||||||
|
Construct a Credentials object from a Mapping
|
||||||
|
|
||||||
|
:param credentials: A mapping that represents a Credentials object
|
||||||
|
:return: A Credentials object
|
||||||
|
:raises InvalidCredentialsError: If the provided Mapping does not represent a valid
|
||||||
|
Credentials object
|
||||||
|
:raises InvalidCredentialComponentError: If any of the contents of `identities` or `secrets`
|
||||||
|
are not a valid ICredentialComponent
|
||||||
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
deserialized_data = CredentialsSchema().load(credentials)
|
deserialized_data = CredentialsSchema().load(credentials)
|
||||||
return Credentials(**deserialized_data)
|
return Credentials(**deserialized_data)
|
||||||
|
@ -132,6 +143,17 @@ class Credentials:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json(credentials: str) -> Credentials:
|
def from_json(credentials: str) -> Credentials:
|
||||||
|
"""
|
||||||
|
Construct a Credentials object from a JSON string
|
||||||
|
|
||||||
|
:param credentials: A JSON string that represents a Credentials object
|
||||||
|
:return: A Credentials object
|
||||||
|
:raises InvalidCredentialsError: If the provided JSON does not represent a valid
|
||||||
|
Credentials object
|
||||||
|
:raises InvalidCredentialComponentError: If any of the contents of `identities` or `secrets`
|
||||||
|
are not a valid ICredentialComponent
|
||||||
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
deserialized_data = CredentialsSchema().loads(credentials)
|
deserialized_data = CredentialsSchema().loads(credentials)
|
||||||
return Credentials(**deserialized_data)
|
return Credentials(**deserialized_data)
|
||||||
|
@ -140,11 +162,31 @@ class Credentials:
|
||||||
except MarshmallowError as err:
|
except MarshmallowError as err:
|
||||||
raise InvalidCredentialsError(str(err))
|
raise InvalidCredentialsError(str(err))
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def to_json(credentials: Credentials) -> str:
|
|
||||||
return CredentialsSchema().dumps(credentials)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json_array(credentials_array_json: str) -> Sequence[Credentials]:
|
def from_json_array(credentials_array_json: str) -> Sequence[Credentials]:
|
||||||
|
"""
|
||||||
|
Construct a sequence of Credentials object from a JSON string
|
||||||
|
|
||||||
|
:param credentials: A JSON string that represents an array of Credentials objects
|
||||||
|
:return: A Sequence of Credentials objects
|
||||||
|
:raises InvalidCredentialsError: If the provided JSON does not represent a valid
|
||||||
|
Credentials object
|
||||||
|
:raises InvalidCredentialComponentError: If any of the contents of `identities` or `secrets`
|
||||||
|
are not a valid ICredentialComponent
|
||||||
|
:raises JSONDecodeError: If the provided string is not valid JSON
|
||||||
|
:raises TypeError: If the provided JSON does not represent an array
|
||||||
|
"""
|
||||||
|
|
||||||
credentials_list = json.loads(credentials_array_json)
|
credentials_list = json.loads(credentials_array_json)
|
||||||
return [Credentials.from_mapping(c) for c in credentials_list]
|
return [Credentials.from_mapping(c) for c in credentials_list]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def to_json(credentials: Credentials) -> str:
|
||||||
|
"""
|
||||||
|
Serialize a Credentials object to JSON
|
||||||
|
|
||||||
|
:param credentials: A Credentials objcet
|
||||||
|
:return: A JSON string representing a Credentials object
|
||||||
|
"""
|
||||||
|
|
||||||
|
return CredentialsSchema().dumps(credentials)
|
||||||
|
|
Loading…
Reference in New Issue