From c8e81287b7d6ea6ba47fa85d432a31d38269b75e Mon Sep 17 00:00:00 2001 From: PhilipWafula Date: Wed, 20 Oct 2021 15:49:46 +0300 Subject: [PATCH] Refactors to use metadataPointer enums in cic-types. --- apps/cic-ussd/cic_ussd/account/balance.py | 5 +++-- apps/cic-ussd/cic_ussd/account/statement.py | 3 ++- apps/cic-ussd/cic_ussd/account/tokens.py | 3 ++- apps/cic-ussd/cic_ussd/cache.py | 7 ++++--- apps/cic-ussd/cic_ussd/db/models/account.py | 3 ++- apps/cic-ussd/cic_ussd/processor/menu.py | 5 +++-- .../runnable/daemons/cic_user_ussd_server.py | 3 ++- apps/cic-ussd/cic_ussd/tasks/callback_handler.py | 6 ++++-- apps/cic-ussd/cic_ussd/tasks/processor.py | 5 +++-- .../tests/cic_ussd/account/test_statement.py | 5 ++--- apps/cic-ussd/tests/cic_ussd/processor/test_menu.py | 4 ++-- apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py | 3 ++- .../tests/cic_ussd/tasks/test_callback_handler.py | 8 +++----- .../tests/cic_ussd/tasks/test_metadata_tasks.py | 6 +++--- .../tests/cic_ussd/tasks/test_processor_tasks.py | 3 ++- apps/cic-ussd/tests/cic_ussd/test_cache.py | 7 ++++--- apps/cic-ussd/tests/fixtures/account.py | 12 ++++++------ 17 files changed, 49 insertions(+), 39 deletions(-) diff --git a/apps/cic-ussd/cic_ussd/account/balance.py b/apps/cic-ussd/cic_ussd/account/balance.py index 838f8bbb..ad9e7ba3 100644 --- a/apps/cic-ussd/cic_ussd/account/balance.py +++ b/apps/cic-ussd/cic_ussd/account/balance.py @@ -7,6 +7,7 @@ from typing import Optional # third-party imports from cic_eth.api import Api from cic_eth_aux.erc20_demurrage_token.api import Api as DemurrageApi +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.transaction import from_wei @@ -102,7 +103,7 @@ def get_cached_available_balance(blockchain_address: str) -> float: :rtype: float """ identifier = bytes.fromhex(blockchain_address) - key = cache_data_key(identifier, salt=':cic.balances') + key = cache_data_key(identifier, salt=MetadataPointer.BALANCES) cached_balances = get_cached_data(key=key) if cached_balances: return calculate_available_balance(json.loads(cached_balances)) @@ -117,5 +118,5 @@ def get_cached_adjusted_balance(identifier: bytes): :return: :rtype: """ - key = cache_data_key(identifier, ':cic.adjusted_balance') + key = cache_data_key(identifier, MetadataPointer.BALANCES_ADJUSTED) return get_cached_data(key) diff --git a/apps/cic-ussd/cic_ussd/account/statement.py b/apps/cic-ussd/cic_ussd/account/statement.py index 584f526c..3dff2693 100644 --- a/apps/cic-ussd/cic_ussd/account/statement.py +++ b/apps/cic-ussd/cic_ussd/account/statement.py @@ -7,6 +7,7 @@ from typing import Optional import celery from chainlib.hash import strip_0x from cic_eth.api import Api +from cic_types.condiments import MetadataPointer # local import from cic_ussd.account.chain import Chain @@ -53,7 +54,7 @@ def get_cached_statement(blockchain_address: str) -> bytes: :rtype: str """ identifier = bytes.fromhex(strip_0x(blockchain_address)) - key = cache_data_key(identifier=identifier, salt=':cic.statement') + key = cache_data_key(identifier=identifier, salt=MetadataPointer.STATEMENT) return get_cached_data(key=key) diff --git a/apps/cic-ussd/cic_ussd/account/tokens.py b/apps/cic-ussd/cic_ussd/account/tokens.py index 40685764..f49bd835 100644 --- a/apps/cic-ussd/cic_ussd/account/tokens.py +++ b/apps/cic-ussd/cic_ussd/account/tokens.py @@ -5,6 +5,7 @@ from typing import Dict, Optional # external imports from cic_eth.api import Api +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.chain import Chain @@ -23,7 +24,7 @@ def get_cached_default_token(chain_str: str) -> Optional[str]: :rtype: """ logg.debug(f'Retrieving default token from cache for chain: {chain_str}') - key = cache_data_key(identifier=chain_str.encode('utf-8'), salt=':cic.default_token_data') + key = cache_data_key(identifier=chain_str.encode('utf-8'), salt=MetadataPointer.TOKEN_DEFAULT) return get_cached_data(key=key) diff --git a/apps/cic-ussd/cic_ussd/cache.py b/apps/cic-ussd/cic_ussd/cache.py index 5c45e069..70689826 100644 --- a/apps/cic-ussd/cic_ussd/cache.py +++ b/apps/cic-ussd/cic_ussd/cache.py @@ -2,7 +2,8 @@ import hashlib import logging -# third-party imports +# external imports +from cic_types.condiments import MetadataPointer from redis import Redis logg = logging.getLogger() @@ -38,7 +39,7 @@ def get_cached_data(key: str): return cache.get(name=key) -def cache_data_key(identifier: bytes, salt: str): +def cache_data_key(identifier: bytes, salt: MetadataPointer): """ :param identifier: :type identifier: @@ -49,5 +50,5 @@ def cache_data_key(identifier: bytes, salt: str): """ hash_object = hashlib.new("sha256") hash_object.update(identifier) - hash_object.update(salt.encode(encoding="utf-8")) + hash_object.update(salt.value.encode(encoding="utf-8")) return hash_object.digest().hex() diff --git a/apps/cic-ussd/cic_ussd/db/models/account.py b/apps/cic-ussd/cic_ussd/db/models/account.py index 2eb9291b..fe0fe21c 100644 --- a/apps/cic-ussd/cic_ussd/db/models/account.py +++ b/apps/cic-ussd/cic_ussd/db/models/account.py @@ -3,6 +3,7 @@ import json # external imports from cic_eth.api import Api +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.metadata import get_cached_preferred_language, parse_account_metadata @@ -109,7 +110,7 @@ class Account(SessionBase): :rtype: str """ identifier = bytes.fromhex(self.blockchain_address) - key = cache_data_key(identifier, ':cic.person') + key = cache_data_key(identifier, MetadataPointer.PERSON) account_metadata = get_cached_data(key) if not account_metadata: return self.phone_number diff --git a/apps/cic-ussd/cic_ussd/processor/menu.py b/apps/cic-ussd/cic_ussd/processor/menu.py index 568409c8..24006a6c 100644 --- a/apps/cic-ussd/cic_ussd/processor/menu.py +++ b/apps/cic-ussd/cic_ussd/processor/menu.py @@ -5,6 +5,7 @@ from datetime import datetime, timedelta # external imports import i18n.config +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.balance import (calculate_available_balance, @@ -163,7 +164,7 @@ class MenuProcessor: token_symbol = get_default_token_symbol() blockchain_address = self.account.blockchain_address balances = get_balances(blockchain_address, chain_str, token_symbol, False)[0] - key = cache_data_key(self.identifier, ':cic.balances') + key = cache_data_key(self.identifier, MetadataPointer.BALANCES) cache_data(key, json.dumps(balances)) available_balance = calculate_available_balance(balances) now = datetime.now() @@ -173,7 +174,7 @@ class MenuProcessor: else: timestamp = int((now - timedelta(30)).timestamp()) adjusted_balance = get_adjusted_balance(to_wei(int(available_balance)), chain_str, timestamp, token_symbol) - key = cache_data_key(self.identifier, ':cic.adjusted_balance') + key = cache_data_key(self.identifier, MetadataPointer.BALANCES_ADJUSTED) cache_data(key, json.dumps(adjusted_balance)) query_statement(blockchain_address) diff --git a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py index f54f3d2f..8e3f4268 100644 --- a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py +++ b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py @@ -12,6 +12,7 @@ import i18n import redis from chainlib.chain import ChainSpec from confini import Config +from cic_types.condiments import MetadataPointer from cic_types.ext.metadata import Metadata from cic_types.ext.metadata.signer import Signer @@ -109,7 +110,7 @@ default_token_data = query_default_token(chain_str) # cache default token for re-usability if default_token_data: - cache_key = cache_data_key(chain_str.encode('utf-8'), ':cic.default_token_data') + cache_key = cache_data_key(chain_str.encode('utf-8'), MetadataPointer.TOKEN_DEFAULT) cache_data(key=cache_key, data=json.dumps(default_token_data)) else: raise InitializationError(f'Default token data for: {chain_str} not found.') diff --git a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py index 30bce11f..9a13bf74 100644 --- a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py +++ b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py @@ -3,8 +3,10 @@ import json import logging from datetime import timedelta -# third-party imports +# external imports import celery +from cic_types.condiments import MetadataPointer + # local imports from cic_ussd.account.balance import get_balances, calculate_available_balance @@ -87,7 +89,7 @@ def balances_callback(result: list, param: str, status_code: int): balances = result[0] identifier = bytes.fromhex(param) - key = cache_data_key(identifier, ':cic.balances') + key = cache_data_key(identifier, MetadataPointer.BALANCES) cache_data(key, json.dumps(balances)) diff --git a/apps/cic-ussd/cic_ussd/tasks/processor.py b/apps/cic-ussd/cic_ussd/tasks/processor.py index 04440ee4..599938ab 100644 --- a/apps/cic-ussd/cic_ussd/tasks/processor.py +++ b/apps/cic-ussd/cic_ussd/tasks/processor.py @@ -2,9 +2,10 @@ import json import logging -# third-party imports +# external imports import celery import i18n +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.metadata import get_cached_preferred_language @@ -49,7 +50,7 @@ def cache_statement(parsed_transaction: dict, querying_party: str): statement_transactions.append(parsed_transaction) data = json.dumps(statement_transactions) identifier = bytes.fromhex(querying_party) - key = cache_data_key(identifier, ':cic.statement') + key = cache_data_key(identifier, MetadataPointer.STATEMENT) cache_data(key, data) diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_statement.py b/apps/cic-ussd/tests/cic_ussd/account/test_statement.py index 82468fec..7fb5a7bc 100644 --- a/apps/cic-ussd/tests/cic_ussd/account/test_statement.py +++ b/apps/cic-ussd/tests/cic_ussd/account/test_statement.py @@ -4,8 +4,7 @@ import time # external imports import pytest -import requests_mock -from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.statement import (filter_statement_transactions, @@ -48,7 +47,7 @@ def test_generate(activated_account, generate(querying_party, None, sender_transaction) time.sleep(2) identifier = bytes.fromhex(activated_account.blockchain_address) - key = cache_data_key(identifier, ':cic.statement') + key = cache_data_key(identifier, MetadataPointer.STATEMENT) statement = get_cached_data(key) statement = json.loads(statement) assert len(statement) == 1 diff --git a/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py b/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py index a343647b..f5ec7f19 100644 --- a/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py +++ b/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py @@ -3,7 +3,7 @@ import json import datetime # external imports -from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.balance import get_cached_available_balance @@ -58,7 +58,7 @@ def test_menu_processor(activated_account, token_symbol=token_symbol) identifier = bytes.fromhex(activated_account.blockchain_address) - key = cache_data_key(identifier, ':cic.adjusted_balance') + key = cache_data_key(identifier, MetadataPointer.BALANCES_ADJUSTED) adjusted_balance = 45931650.64654012 cache_data(key, json.dumps(adjusted_balance)) resp = response(activated_account, 'ussd.kenya.account_balances', name, init_database, generic_ussd_session) diff --git a/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py b/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py index 25ccb8e6..e5bbd52f 100644 --- a/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py +++ b/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py @@ -7,6 +7,7 @@ import time import i18n import requests_mock from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.chain import Chain @@ -45,7 +46,7 @@ def test_handle_menu(activated_account, ussd_menu = UssdMenu.find_by_name('initial_language_selection') assert menu_resp.get('name') == ussd_menu.get('name') identifier = bytes.fromhex(strip_0x(pending_account.blockchain_address)) - key = cache_data_key(identifier, ':cic.preferences') + key = cache_data_key(identifier, MetadataPointer.PREFERENCES) cache_data(key, json.dumps(preferences)) time.sleep(2) menu_resp = handle_menu(pending_account, init_database) diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py index fa017add..a0d4676d 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py @@ -1,20 +1,18 @@ # standard imports import json -from decimal import Decimal # external imports import celery import pytest -import requests_mock from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports -from cic_ussd.account.statement import generate, filter_statement_transactions +from cic_ussd.account.statement import filter_statement_transactions from cic_ussd.account.transaction import transaction_actors from cic_ussd.cache import cache_data_key, get_cached_data from cic_ussd.db.models.account import Account from cic_ussd.error import AccountCreationDataNotFound -from cic_ussd.metadata import PreferencesMetadata # test imports @@ -89,7 +87,7 @@ def test_balances_callback(activated_account, balances, celery_session_worker): [balances, activated_account.blockchain_address, status_code]) s_balances_callback.apply_async().get() identifier = bytes.fromhex(strip_0x(activated_account.blockchain_address)) - key = cache_data_key(identifier, ':cic.balances') + key = cache_data_key(identifier, MetadataPointer.BALANCES) cached_balances = get_cached_data(key) cached_balances = json.loads(cached_balances) assert cached_balances == balances[0] diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_metadata_tasks.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_metadata_tasks.py index a00c82c7..5bb2b328 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_metadata_tasks.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_metadata_tasks.py @@ -1,11 +1,11 @@ # standard imports import json -import os # external imports import celery import requests_mock from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.cache import cache_data_key, get_cached_data @@ -27,7 +27,7 @@ def test_query_person_metadata(activated_account, s_query_person_metadata = celery.signature( 'cic_ussd.tasks.metadata.query_person_metadata', [activated_account.blockchain_address]) s_query_person_metadata.apply().get() - key = cache_data_key(identifier, ':cic.person') + key = cache_data_key(identifier, MetadataPointer.PERSON) cached_person_metadata = get_cached_data(key) cached_person_metadata = json.loads(cached_person_metadata) assert cached_person_metadata == person_metadata @@ -46,7 +46,7 @@ def test_query_preferences_metadata(activated_account, query_preferences_metadata = celery.signature( 'cic_ussd.tasks.metadata.query_preferences_metadata', [activated_account.blockchain_address]) query_preferences_metadata.apply().get() - key = cache_data_key(identifier, ':cic.preferences') + key = cache_data_key(identifier, MetadataPointer.PREFERENCES) cached_preferences_metadata = get_cached_data(key) cached_preferences_metadata = json.loads(cached_preferences_metadata) assert cached_preferences_metadata == preferences diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py index 63a5edd7..fcfdef4a 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py @@ -4,6 +4,7 @@ import json # external imports import celery from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.transaction import transaction_actors @@ -38,7 +39,7 @@ def test_cache_statement(activated_account, transaction_result): recipient_transaction, sender_transaction = transaction_actors(transaction_result) identifier = bytes.fromhex(strip_0x(activated_account.blockchain_address)) - key = cache_data_key(identifier, ':cic.statement') + key = cache_data_key(identifier, MetadataPointer.STATEMENT) cached_statement = get_cached_data(key) assert cached_statement is None s_parse_transaction = celery.signature( diff --git a/apps/cic-ussd/tests/cic_ussd/test_cache.py b/apps/cic-ussd/tests/cic_ussd/test_cache.py index 6e7625b4..b3fe68cf 100644 --- a/apps/cic-ussd/tests/cic_ussd/test_cache.py +++ b/apps/cic-ussd/tests/cic_ussd/test_cache.py @@ -3,6 +3,7 @@ import hashlib import json # external imports +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.cache import cache_data, cache_data_key, get_cached_data @@ -12,7 +13,7 @@ from cic_ussd.cache import cache_data, cache_data_key, get_cached_data def test_cache_data(init_cache): identifier = 'some_key'.encode() - key = cache_data_key(identifier, ':testing') + key = cache_data_key(identifier, MetadataPointer.PERSON) assert get_cached_data(key) is None cache_data(key, json.dumps('some_value')) assert get_cached_data(key) is not None @@ -20,10 +21,10 @@ def test_cache_data(init_cache): def test_cache_data_key(): identifier = 'some_key'.encode() - key = cache_data_key(identifier, ':testing') + key = cache_data_key(identifier, MetadataPointer.PERSON) hash_object = hashlib.new("sha256") hash_object.update(identifier) - hash_object.update(':testing'.encode(encoding="utf-8")) + hash_object.update(':cic.person'.encode(encoding="utf-8")) assert hash_object.digest().hex() == key diff --git a/apps/cic-ussd/tests/fixtures/account.py b/apps/cic-ussd/tests/fixtures/account.py index 48034a03..5873e0a4 100644 --- a/apps/cic-ussd/tests/fixtures/account.py +++ b/apps/cic-ussd/tests/fixtures/account.py @@ -4,7 +4,7 @@ import random # external accounts import pytest -from chainlib.hash import strip_0x +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.chain import Chain @@ -56,7 +56,7 @@ def cache_account_creation_data(init_cache, account_creation_data): def cache_balances(activated_account, balances, init_cache): identifier = bytes.fromhex(activated_account.blockchain_address) balances = json.dumps(balances[0]) - key = cache_data_key(identifier, ':cic.balances') + key = cache_data_key(identifier, MetadataPointer.BALANCES) cache_data(key, balances) @@ -64,7 +64,7 @@ def cache_balances(activated_account, balances, init_cache): def cache_default_token_data(default_token_data, init_cache, load_chain_spec): chain_str = Chain.spec.__str__() data = json.dumps(default_token_data) - key = cache_data_key(chain_str.encode('utf-8'), ':cic.default_token_data') + key = cache_data_key(chain_str.encode('utf-8'), MetadataPointer.TOKEN_DEFAULT) cache_data(key, data) @@ -72,7 +72,7 @@ def cache_default_token_data(default_token_data, init_cache, load_chain_spec): def cache_person_metadata(activated_account, init_cache, person_metadata): identifier = bytes.fromhex(activated_account.blockchain_address) person = json.dumps(person_metadata) - key = cache_data_key(identifier, ':cic.person') + key = cache_data_key(identifier, MetadataPointer.PERSON) cache_data(key, person) @@ -80,7 +80,7 @@ def cache_person_metadata(activated_account, init_cache, person_metadata): def cache_preferences(activated_account, init_cache, preferences): identifier = bytes.fromhex(activated_account.blockchain_address) preferences = json.dumps(preferences) - key = cache_data_key(identifier, ':cic.preferences') + key = cache_data_key(identifier, MetadataPointer.PREFERENCES) cache_data(key, preferences) @@ -88,7 +88,7 @@ def cache_preferences(activated_account, init_cache, preferences): def cache_statement(activated_account, init_cache, statement): identifier = bytes.fromhex(activated_account.blockchain_address) statement = json.dumps(statement) - key = cache_data_key(identifier, ':cic.statement') + key = cache_data_key(identifier, MetadataPointer.STATEMENT) cache_data(key, statement)