Skip to content

Commit

Permalink
feat: Add EffectResponse to parse single effect (#36)
Browse files Browse the repository at this point in the history
* Add EffectResponse to parse single effect

* Update docstring

* Run precommit over test file
  • Loading branch information
RohitK89 authored Jun 9, 2022
1 parent dabf5f4 commit d992fa8
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions stellar_model/response/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from stellar_model.response.assets_response import *
from stellar_model.response.claimable_balance_response import *
from stellar_model.response.claimable_balances_response import *
from stellar_model.response.effect_response import *
from stellar_model.response.effects_response import *
from stellar_model.response.error_response import *
from stellar_model.response.fee_stats_response import *
Expand Down
42 changes: 42 additions & 0 deletions stellar_model/response/effect_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pydantic import BaseModel

from stellar_model import __issues__
from stellar_model.model.horizon.effects import _EFFECT_TYPE_I_MAP
from stellar_model.model.horizon.effects import _EFFECT_TYPE_UNION


__all__ = ["EffectResponse"]


class EffectResponse(BaseModel):
"""
Represents single effect response.
Can be used for parsing individual records returned from the following endpoint:
- GET /effects
See `Effects <https://developers.stellar.org/api/resources/effects/>`_ on Stellar API Reference.
The primary intended use-case for this model is to allow parsing individual Effects using the relevant type
parser in situations where EffectsResponse cannot be used over the entire list, like if one or
more items in the list throws a ValidationError.
"""

record: _EFFECT_TYPE_UNION

def __init__(self, **data):
if "type_i" not in data:
raise ValueError(
"Invalid data, `type_i` does not appear in the raw data. "
"Please check the raw data first, if the data is correct, "
"try to upgrade the library or raise an issue at {__issues__}."
)
effect_type = data["type_i"]
if effect_type not in _EFFECT_TYPE_I_MAP:
raise ValueError(
f"The type of effect is {effect_type}, which is not currently supported in the version. "
f"Please try to upgrade the library or raise an issue at {__issues__}."
)
parser = _EFFECT_TYPE_I_MAP[effect_type]
record = parser.parse_obj(data)
super().__init__(record=record)
117 changes: 117 additions & 0 deletions tests/response/test_effect_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from unittest import TestCase

from stellar_model import EffectResponse
from stellar_model.model.horizon.effects import *
from tests.response import load_horizon_file


ops = [
{"filename": "account_created.json", "class": AccountCreatedEffect},
{"filename": "account_removed.json", "class": AccountRemovedEffect},
{"filename": "account_credited.json", "class": AccountCreditedEffect},
{"filename": "account_debited.json", "class": AccountDebitedEffect},
{
"filename": "account_thresholds_updated.json",
"class": AccountThresholdsUpdatedEffect,
},
{
"filename": "account_home_domain_updated.json",
"class": AccountHomeDomainUpdatedEffect,
},
{"filename": "account_flags_updated.json", "class": AccountFlagsUpdatedEffect},
{
"filename": "account_inflation_destination_updated.json",
"class": AccountInflationDestinationUpdatedEffect,
},
{"filename": "signer_created.json", "class": SignerCreatedEffect},
{"filename": "signer_removed.json", "class": SignerRemovedEffect},
{"filename": "signer_updated.json", "class": SignerUpdatedEffect},
{"filename": "trustline_created.json", "class": TrustlineCreatedEffect},
{"filename": "trustline_removed.json", "class": TrustlineRemovedEffect},
{"filename": "trustline_updated.json", "class": TrustlineUpdatedEffect},
{"filename": "trustline_authorized.json", "class": TrustlineAuthorizedEffect},
{"filename": "trustline_deauthorized.json", "class": TrustlineDeauthorizedEffect},
{
"filename": "trustline_authorized_to_maintain_liabilities.json",
"class": TrustlineAuthorizedToMaintainLiabilitiesEffect,
},
{"filename": "trustline_flags_updated.json", "class": TrustlineFlagsUpdatedEffect},
{"filename": "trade.json", "class": TradeEffect},
{"filename": "data_created.json", "class": DataCreatedEffect},
{"filename": "data_removed.json", "class": DataRemovedEffect},
{"filename": "data_updated.json", "class": DataUpdatedEffect},
{"filename": "sequence_bumped.json", "class": SequenceBumpedEffect},
{
"filename": "claimable_balance_created.json",
"class": ClaimableBalanceCreatedEffect,
},
{
"filename": "claimable_balance_claimed.json",
"class": ClaimableBalanceClaimedEffect,
},
{
"filename": "claimable_balance_claimant_created.json",
"class": ClaimableBalanceClaimantCreatedEffect,
},
{
"filename": "claimable_balance_clawed_back.json",
"class": ClaimableBalanceClawedBackEffect,
},
{
"filename": "claimable_balance_sponsorship_created.json",
"class": ClaimableBalanceSponsorshipCreatedEffect,
},
{
"filename": "claimable_balance_sponsorship_removed.json",
"class": ClaimableBalanceSponsorshipRemovedEffect,
},
{
"filename": "account_sponsorship_created.json",
"class": AccountSponsorshipCreatedEffect,
},
{
"filename": "account_sponsorship_removed.json",
"class": AccountSponsorshipRemovedEffect,
},
{
"filename": "trustline_sponsorship_created.json",
"class": TrustlineSponsorshipCreatedEffect,
},
{
"filename": "trustline_sponsorship_removed.json",
"class": TrustlineSponsorshipRemovedEffect,
},
{
"filename": "data_sponsorship_created.json",
"class": DataSponsorshipCreatedEffect,
},
{
"filename": "data_sponsorship_removed.json",
"class": DataSponsorshipRemovedEffect,
},
{
"filename": "signer_sponsorship_created.json",
"class": SignerSponsorshipCreatedEffect,
},
{
"filename": "signer_sponsorship_removed.json",
"class": SignerSponsorshipRemovedEffect,
},
{
"filename": "liquidity_pool_deposited.json",
"class": LiquidityPoolDepositedEffect,
},
{"filename": "liquidity_pool_withdrew.json", "class": LiquidityPoolWithdrewEffect},
{"filename": "liquidity_pool_created.json", "class": LiquidityPoolCreatedEffect},
{"filename": "liquidity_pool_trade.json", "class": LiquidityPoolTradeEffect},
]


class TestEffectResponse(TestCase):
def test_valid(self):
for op in ops:
raw_data = load_horizon_file(f"effects/{op['filename']}")
parsed_data = EffectResponse.parse_obj(raw_data)
self.assertTrue(isinstance(parsed_data, EffectResponse))
self.assertTrue(isinstance(parsed_data.record, op["class"]))
self.assertEqual(raw_data["id"], parsed_data.record.id)

0 comments on commit d992fa8

Please sign in to comment.