Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EffectResponse to parse single effect #36

Merged
merged 3 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions stellar_model/response/effect_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 the following endpoint(s):
- GET /effects/:effect_id
overcat marked this conversation as resolved.
Show resolved Hide resolved
See `Effects <https://developers.stellar.org/api/resources/effects/>`_ on Stellar API Reference.
"""

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)
60 changes: 60 additions & 0 deletions tests/response/test_effect_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)