From 3a97cf95e8516acdf95b95c94fc61a9127a54649 Mon Sep 17 00:00:00 2001 From: PhilipWafula Date: Mon, 25 Oct 2021 21:01:00 +0300 Subject: [PATCH] Adds token metadata handler. --- apps/cic-ussd/cic_ussd/metadata/tokens.py | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 apps/cic-ussd/cic_ussd/metadata/tokens.py diff --git a/apps/cic-ussd/cic_ussd/metadata/tokens.py b/apps/cic-ussd/cic_ussd/metadata/tokens.py new file mode 100644 index 00000000..f3b1c26b --- /dev/null +++ b/apps/cic-ussd/cic_ussd/metadata/tokens.py @@ -0,0 +1,54 @@ +# standard imports +from typing import Dict, Optional + +# external imports +import json + +from cic_types.condiments import MetadataPointer + +# local imports +from .base import UssdMetadataHandler +from cic_ussd.cache import cache_data +from cic_ussd.error import MetadataNotFoundError + + +class TokenMetadata(UssdMetadataHandler): + def __init__(self, identifier: bytes, **kwargs): + super(TokenMetadata, self).__init__(identifier=identifier, **kwargs) + + +def token_metadata_handler(metadata_client: TokenMetadata) -> Optional[Dict]: + """ + :param metadata_client: + :type metadata_client: + :return: + :rtype: + """ + result = metadata_client.query() + token_metadata = result.json() + if not token_metadata: + raise MetadataNotFoundError(f'No metadata found at: {metadata_client.metadata_pointer} for: {metadata_client.identifier.decode("utf-8")}') + cache_data(metadata_client.metadata_pointer, json.dumps(token_metadata)) + return token_metadata + + +def query_token_metadata(identifier: bytes): + """ + :param identifier: + :type identifier: + :return: + :rtype: + """ + token_metadata_client = TokenMetadata(identifier=identifier, cic_type=MetadataPointer.TOKEN_META_SYMBOL) + return token_metadata_handler(token_metadata_client) + + +def query_token_info(identifier: bytes): + """ + :param identifier: + :type identifier: + :return: + :rtype: + """ + token_info_client = TokenMetadata(identifier=identifier, cic_type=MetadataPointer.TOKEN_PROOF_SYMBOL) + return token_metadata_handler(token_info_client)