示例#1
0
    def _decode(cls: Type[T], entity: Entity, data: Any) -> T:

        if not isinstance(data, list):
            raise UnexpectedResponseType(data, list)

        if len(data) < 1:
            raise ApiError('Response unexpectedly empty')

        tx_list_data = data[0]

        try:

            if tx_list_data['versions'] is None:
                tx_list_data['versions'] = list()

            tx_list = cls(
                entity=entity,
                transaction_id=tx_list_data['transaction_id'],
                versions=Transaction.decode_many(
                    entity,
                    tx_list_data['versions']
                )
            )

        except KeyError as error:
            raise MissingKey(error.args[0])

        return tx_list
    def decode(
        cls: Type[T],
        entity: Entity,
        data: Any
    ) -> T:

        if not isinstance(data, dict):
            raise UnexpectedResponseType(data, dict)

        try:

            if data['assets'] is None:
                data['assets'] = list()
            if data['liabilities'] is None:
                data['liabilities'] = list()
            if data['equities'] is None:
                data['equities'] = list()

            position = cls(
                entity=entity,
                balance_time=AmatinoTime.decode(data['balance_time']),
                generated_time=AmatinoTime.decode(data['generated_time']),
                global_unit_id=data['global_unit_denomination'],
                custom_unit_id=data['custom_unit_denomination'],
                assets=TreeNode.decode_many(entity, data['assets']),
                liabilities=TreeNode.decode_many(entity, data['liabilities']),
                equities=TreeNode.decode_many(entity, data['equities']),
                depth=data['depth']
            )

        except KeyError as error:
            raise MissingKey(error.args[0])

        return position
    def decode(cls: Type[T], entity: Entity, data: Any) -> T:

        if not isinstance(data, dict):
            raise UnexpectedResponseType(data, dict)

        try:
            children = None
            if data['children'] is not None:
                children = [cls.decode(entity, d) for d in data['children']]
            node = cls(
                entity=entity,
                account_id=data['account_id'],
                depth=data['depth'],
                account_balance=AmatinoAmount.decode(
                    data['account_balance']
                ),
                recursive_balance=AmatinoAmount.decode(
                    data['recursive_balance']
                ),
                name=data['name'],
                am_type=AMType(data['type']),
                children=children
            )
        except KeyError as error:
            raise MissingKey(error.args[0])

        return node
    def decode(
        cls: Type[T],
        entity: Entity,
        data: Any
    ) -> T:

        if not isinstance(data, dict):
            raise UnexpectedResponseType(data, dict)

        try:

            income = None
            if data['income'] is not None:
                income = TreeNode.decode_many(entity, data['income'])

            expenses = None
            if data['expenses'] is not None:
                expenses = TreeNode.decode_many(entity, data['expenses'])

            performance = cls(
                entity=entity,
                start_time=AmatinoTime.decode(data['start_time']),
                end_time=AmatinoTime.decode(data['end_time']),
                generated_time=AmatinoTime.decode(data['generated_time']),
                custom_unit_id=data['custom_unit_denomination'],
                global_unit_id=data['global_unit_denomination'],
                income=income,
                expenses=expenses,
                depth=data['depth']
            )
        except KeyError as error:
            raise MissingKey(error.args[0])

        return performance
    def _decode_many(cls: Type[T], entity: Entity, data: Any) -> List[T]:

        if not isinstance(data, list):
            raise UnexpectedResponseType(data, list)

        for balance in data:
            if not isinstance(balance, dict):
                raise UnexpectedResponseType(balance, dict)

        balances = list()

        for balance in data:
            balances.append(
                cls(entity, AmatinoTime.decode(balance['balance_time']),
                    AmatinoTime.decode(balance['generated_time']),
                    balance['recursive'], balance['global_unit_denomination'],
                    balance['custom_unit_denomination'], balance['account_id'],
                    Decimal(balance['balance'])))

        return balances
        def decode(obj: Any) -> T:
            if not isinstance(obj, dict):
                raise UnexpectedResponseType(obj, dict)

            user = cls(
                session=session,
                id_=obj['user_id'],
                email=obj['email'],
                name=obj['name'],
                handle=obj['handle'],
                avatar_url=obj['avatar_url']
            )

            return user
        def decode(data) -> LedgerRow:

            if not isinstance(data, list):
                raise UnexpectedResponseType(data, list)

            row = LedgerRow(transaction_id=data[0],
                            transaction_time=AmatinoTime.decode(data[1]),
                            description=data[2],
                            opposing_account_id=data[3],
                            opposing_account_name=data[4],
                            debit=AmatinoAmount.decode(data[5]),
                            credit=AmatinoAmount.decode(data[6]),
                            balance=AmatinoAmount.decode(data[7]))

            return row
        def decode(unit_data: Any) -> T:
            if not isinstance(unit_data, dict):
                raise UnexpectedResponseType(unit_data, dict)

            try:
                unit = cls(code=unit_data['code'],
                           id_=unit_data['global_unit_id'],
                           name=unit_data['name'],
                           priority=unit_data['priority'],
                           description=unit_data['description'],
                           exponent=unit_data['exponent'])
            except KeyError as error:
                message = 'Expected key "{key}" missing from response data'
                message.format(key=error.args[0])
                raise ApiError(message)

            return unit
示例#9
0
    def decode(cls: Type[T], entity: Entity, data: Any) -> T:

        if not isinstance(data, dict):
            raise UnexpectedResponseType(data, dict)

        try:

            tree = cls(
                entity=entity,
                balance_time=AmatinoTime.decode(data['balance_time']),
                generated_time=AmatinoTime.decode(data['generated_time']),
                global_unit_denomination=data['global_unit_denomination'],
                custom_unit_denomination=data['custom_unit_denomination'],
                tree=TreeNode.decode_many(entity, data['tree'])
            )

        except KeyError as error:
            raise MissingKey(error.args[0])

        return tree
    def _decode_rows(cls: Type[T], rows: List[Any]) -> List[LedgerRow]:
        """Return LedgerRows decoded from raw API response data"""
        if not isinstance(rows, list):
            raise UnexpectedResponseType(rows, list)

        def decode(data) -> LedgerRow:

            if not isinstance(data, list):
                raise UnexpectedResponseType(data, list)

            row = LedgerRow(transaction_id=data[0],
                            transaction_time=AmatinoTime.decode(data[1]),
                            description=data[2],
                            opposing_account_id=data[3],
                            opposing_account_name=data[4],
                            debit=AmatinoAmount.decode(data[5]),
                            credit=AmatinoAmount.decode(data[6]),
                            balance=AmatinoAmount.decode(data[7]))

            return row

        return [decode(r) for r in rows]
    def decode_many(cls: Type[T], session: Session, data: Any) -> List[T]:
        """Return a list of Users decoded from API response data"""

        if not isinstance(data, list):
            raise UnexpectedResponseType(data, list)

        def decode(obj: Any) -> T:
            if not isinstance(obj, dict):
                raise UnexpectedResponseType(obj, dict)

            user = cls(
                session=session,
                id_=obj['user_id'],
                email=obj['email'],
                name=obj['name'],
                handle=obj['handle'],
                avatar_url=obj['avatar_url']
            )

            return user

        return [decode(u) for u in data]
    def _decode(cls: Type[T], entity: Entity, data: Any) -> T:

        if not isinstance(data, dict):
            raise UnexpectedResponseType(type(data), dict)

        try:
            ledger = cls(entity=entity,
                         account_id=data['account_id'],
                         start_time=AmatinoTime.decode(data['start_time']),
                         end_time=AmatinoTime.decode(data['end_time']),
                         recursive=data['recursive'],
                         generated_time=AmatinoTime.decode(
                             data['generated_time']),
                         global_unit_id=data['global_unit_denomination'],
                         custom_unit_id=data['custom_unit_denomination'],
                         ledger_rows=Ledger._decode_rows(data['ledger_rows']),
                         page=data['page'],
                         number_of_pages=data['number_of_pages'],
                         order=LedgerOrder(data['ordered_oldest_first']))
        except KeyError as error:
            raise MissingKey(error.args[0])

        return ledger
 def decode_many(cls: Type[T], entity: Entity, data: Any) -> List[T]:
     if not isinstance(data, list):
         raise UnexpectedResponseType(data, list)
     return [cls.decode(entity, o) for o in data]