Skip to content

Commit

Permalink
feat: automatically canonize user address arg in routers
Browse files Browse the repository at this point in the history
  • Loading branch information
pik694 committed Mar 19, 2024
1 parent e1eaee5 commit 888152c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion backend/app/infrastructure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask_restx import Resource

from eth_utils import to_checksum_address

from gql import Client
from gql.transport.requests import RequestsHTTPTransport

Expand All @@ -19,12 +21,29 @@ class OctantResource(Resource):
def __init__(self, *args, **kwargs):
Resource.__init__(self, *args, *kwargs)

@classmethod
def canonize_address(cls, field_name: str = "user_address", force=True):
def _add_address_canonization(handler):
def _decorated(*args, **kwargs):
field_value = kwargs.get(field_name)
if force or field_value is not None:
updated_field = to_checksum_address(field_value)
kwargs.update([(field_name, updated_field)])

return handler(*args, **kwargs)

return _decorated

return _add_address_canonization

def __getattribute__(self, name):
user_address_canonizer = OctantResource.canonize_address(force=False)

attr = object.__getattribute__(self, name)

decorator = default_decorators.get(name)
if decorator is not None:
attr = decorator(attr)
attr = user_address_canonizer(decorator(attr))

return attr

Expand Down

0 comments on commit 888152c

Please sign in to comment.