From d3982ba59b7c5891c5cb5a3b47521d95370efb98 Mon Sep 17 00:00:00 2001 From: Luke Van Seters Date: Mon, 12 Jul 2021 15:20:53 -0400 Subject: [PATCH] Include json_encoders in the CamelModel --- schemas/.utils.py.swp | Bin 0 -> 12288 bytes schemas/blocks.py | 12 ++---------- schemas/utils.py | 22 +++++++++++++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 schemas/.utils.py.swp diff --git a/schemas/.utils.py.swp b/schemas/.utils.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..fb8d39c29657a8a310307626e14e0823c45a2af4 GIT binary patch literal 12288 zcmeI2zi$&U6vtggL}>+df22b$QgbP-gixebqGf=gq60t1!*}N-ZhiLY>}#7MU;`Fl zV&EU(4`4uIWoG3sU?@mP9biS``FcqyD(Vb8OW*inKfmYi$6lgbJb&lL8e9n%7`AD~ z_FsOz_xi(dVrF-Osd7{J;gi}LPgacCY_AuleK}Mn;p*5W4}HQ;UYLF-<5G9}(hbTu zOib4CGh4QkiLJKOHW!I&=fjRo1|s9OQ#z?^m=BN5N`pjz2%JcubiMYKNw&CfY2L@q zHZH>XbGJ`iMgc^C2oM1xKm>>Y5g-CYfC&6M1YADB9-*C6wWUVgPyE)`U#TMkM1Tko z0U|&IhyW2F0z`la5CI}U1pYz-JY{U=3}f$5{{R2c@Bi;pjD16WM7>2lMLj`1My05A z)GTWMEMs3#pHX|LH>eDyP@AZmsA<#`>c=EwpHS~nuTU>g&r#1%IjVySQKwOR^`0)^ zGc_VW1c(3;AOb{y2oM1xKm`7C0#G-ul-Z7Qlb4D+X>=41qn;EhjWo{$EWxd!6znis zavSx976o^vz-ecq1OrtSk|=I1tQy^u{ot;M7uQTGRCqAZ>ar@R(P*r2E2=4Q1Ma{T zTw5$JEc6PKL6vY7V=y~d%{K+sumTsdx`MA*B?!i;eERV|AB>*>+jX(CGQ{j%P&|uM z4udWX!hzTcT60GNuR2%AxOC#0Ok5XI{5%ZUut-Pf1T7U7o!_sP{%ZM;1_xZH3f*lZ zlBR`HTTw`^&AVODOJ{GrW( z*hy5GipbY<)mW>=Qql8=W(wJtnyaX?<((cR>cSyWXfIa`Mr2!KSUo&@IS1RONI_~4 zy3G8I5P?O2x*ytHNf$JuCKiOc`QR|&xpdyS2wEU}AYmEiK`1gXnyia_wX?3K{!8n8xc7 R`%5#L9$g~F>XaTF`w1v4Q5gUL literal 0 HcmV?d00001 diff --git a/schemas/blocks.py b/schemas/blocks.py index fb71d97..18e41b1 100644 --- a/schemas/blocks.py +++ b/schemas/blocks.py @@ -2,11 +2,9 @@ import json from enum import Enum from typing import Dict, List, Optional -from hexbytes import HexBytes from pydantic import BaseModel -from web3.datastructures import AttributeDict -from .utils import CamelModel +from .utils import CamelModel, Web3Model class BlockCallType(Enum): @@ -30,7 +28,7 @@ class BlockCall(CamelModel): error: Optional[str] -class Block(BaseModel): +class Block(Web3Model): block_number: int calls: List[BlockCall] data: dict @@ -39,12 +37,6 @@ class Block(BaseModel): transaction_hashes: List[str] txs_gas_data: Dict[str, dict] - class Config: - json_encoders = { - AttributeDict: dict, - HexBytes: lambda h: h.hex(), - } - def get_filtered_calls(self, hash: str) -> List[BlockCall]: return [ call for call in self.calls diff --git a/schemas/utils.py b/schemas/utils.py index 8a57059..a3cb04b 100644 --- a/schemas/utils.py +++ b/schemas/utils.py @@ -1,6 +1,8 @@ import json +from hexbytes import HexBytes from pydantic import BaseModel +from web3.datastructures import AttributeDict def to_camel(string: str) -> str: @@ -10,13 +12,23 @@ def to_camel(string: str) -> str: ) +def to_original_json_dict(model: BaseModel) -> dict: + return json.loads(model.json(by_alias=True, exclude_unset=True)) + + +class Web3Model(BaseModel): + """BaseModel that handles web3's unserializable objects""" + + class Config: + json_encoders = { + AttributeDict: dict, + HexBytes: lambda h: h.hex(), + } + + class CamelModel(BaseModel): """BaseModel that translates from camelCase to snake_case""" - class Config: + class Config(Web3Model.Config): alias_generator = to_camel allow_population_by_field_name = True - - -def to_original_json_dict(model: BaseModel) -> dict: - return json.loads(model.json(by_alias=True, exclude_unset=True))