Source code for q2_sdk.models.adapters.statement_image

from __future__ import annotations
from base64 import b64encode
from dataclasses import dataclass
from enum import Enum
from typing import Optional
from pathlib import Path
from q2_sdk.core.exceptions import BadParameterError

HERE = Path(__file__).absolute().parent


[docs] class StatementImageRequestType(Enum): StatementPeriodInfo = 10 StatementImage = 11
[docs] @dataclass class StatementImageRequest: internal_account_number: str transaction_id: int transaction_type: StatementImageRequestType external_account_number: Optional[str] = None cif_internal: Optional[str] = None cif_external: Optional[str] = None user_data: Optional[str] = None host_account_id: Optional[int] = None period_name: Optional[str] = None period_year: Optional[str] = None period_file_id: Optional[str] = None period_image_type: Optional[str] = None
[docs] @staticmethod def from_hq_request(inp: dict) -> StatementImageRequest: host_account = inp["HostAccount_Req"][0] internal_account_number = host_account["AccountNumberInternal"] external_account_number = host_account["AccountNumberExternal"] cif_internal = host_account["CifInternal"] cif_external = host_account["CifExternal"] transaction_type = host_account["TransactionType"] transaction_id = host_account["TransactionID"] user_data = inp["RequestorData"][0]["UserData"] host_account_id = host_account["HostAccountID"] statement_image_req = inp["StatementImageCycle_Req"] if statement_image_req: period_name = statement_image_req[0]["Period"] period_year = statement_image_req[0]["Year"] period_file_id = statement_image_req[0]["FileId"] period_image_type = statement_image_req[0]["ImageType"] else: period_name = None period_year = None period_file_id = None period_image_type = None if not internal_account_number: raise BadParameterError("AccountNumberInternal must be provided") elif not transaction_id: raise BadParameterError("TransactionID must be provided") if ( StatementImageRequestType(transaction_type) == StatementImageRequestType.StatementImage ): if not period_name: raise BadParameterError("Period must be provided") elif not period_year: raise BadParameterError("Year must be provided") return StatementImageRequest( internal_account_number, transaction_id, StatementImageRequestType(transaction_type), external_account_number, cif_internal, cif_external, user_data, host_account_id, period_name, period_year, period_file_id, period_image_type, )
[docs] class ImageType(Enum): GIF = 0 JPG = 1 BMP = 2 PNG = 3 PDF = 4 TIFF = 5
[docs] @dataclass class StatementImage: raw: bytes image_type: ImageType
[docs] def as_base64(self) -> str: return b64encode(self.raw).decode()
[docs] def as_adapter_response(self, transaction_id) -> dict[str, str]: return { "ImageData": self.as_base64(), "ImageType": self.image_type.value, "TransactionID": transaction_id, }
[docs] @dataclass class StatementPeriodInfo: name: str year: int file_id: str
[docs] def as_adapter_response(self) -> dict[str, str]: return {"Period": self.name, "Year": str(self.year), "FileId": self.file_id}
[docs] class MockImage:
[docs] @staticmethod def get() -> bytes: return Path.read_bytes(HERE / "statement_image.pdf")