Refactors to use metadataPointer enums in cic-types.

This commit is contained in:
PhilipWafula 2021-10-20 15:49:46 +03:00
parent b0fc3f866c
commit c8e81287b7
Signed by untrusted user: mango-habanero
GPG Key ID: B00CE9034DA19FB7
17 changed files with 49 additions and 39 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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.')

View File

@ -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))

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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]

View File

@ -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

View File

@ -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(

View File

@ -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

View File

@ -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)