|
| 1 | +from io import BytesIO |
| 2 | +from typing import ClassVar, Optional, cast |
| 3 | + |
| 4 | +from cuenca_validations.types import ( |
| 5 | + FileFormat, |
| 6 | + FileQuery, |
| 7 | + FileUploadRequest, |
| 8 | + KYCFileType, |
| 9 | +) |
| 10 | +from pydantic import HttpUrl |
| 11 | +from pydantic.dataclasses import dataclass |
| 12 | + |
| 13 | +from ..http import Session, session as global_session |
| 14 | +from .base import Downloadable, Queryable, Uploadable |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class File(Downloadable, Queryable, Uploadable): |
| 19 | + _resource: ClassVar = 'files' |
| 20 | + _query_params: ClassVar = FileQuery |
| 21 | + |
| 22 | + extension: str |
| 23 | + type: KYCFileType |
| 24 | + url: HttpUrl |
| 25 | + user_id: str |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def upload( |
| 29 | + cls, |
| 30 | + file: BytesIO, |
| 31 | + file_type: KYCFileType, |
| 32 | + extension: Optional[str], |
| 33 | + is_back: bool = False, |
| 34 | + user_id: str = 'me', |
| 35 | + *, |
| 36 | + session: Session = global_session, |
| 37 | + ) -> 'File': |
| 38 | + """ |
| 39 | + Stores an encrypted version of the file, |
| 40 | + only users with permissions can access it. |
| 41 | +
|
| 42 | + :param file: |
| 43 | + :param user_id: |
| 44 | + :param session: |
| 45 | + :return: New encrypted file object |
| 46 | + """ |
| 47 | + req = FileUploadRequest( |
| 48 | + file=file.read(), |
| 49 | + type=file_type, |
| 50 | + extension=extension, |
| 51 | + is_back=is_back, |
| 52 | + user_id=user_id, |
| 53 | + ) |
| 54 | + return cast( |
| 55 | + 'File', |
| 56 | + cls._upload( |
| 57 | + session=session, |
| 58 | + **req.dict(), |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + @property |
| 63 | + def file(self) -> bytes: |
| 64 | + """ |
| 65 | + Bytes of the decrypted file. |
| 66 | + Format of the file is found on `file_type` property. |
| 67 | + """ |
| 68 | + return self.download(self, file_format=FileFormat.any).read() |
| 69 | + |
| 70 | + @property |
| 71 | + def pdf(self) -> bytes: |
| 72 | + """ |
| 73 | + Override from `Downloadable`, this property does not apply. |
| 74 | + """ |
| 75 | + raise NotImplementedError |
| 76 | + |
| 77 | + @property |
| 78 | + def xml(self) -> bytes: |
| 79 | + """ |
| 80 | + Override from `Downloadable`, this property does not apply. |
| 81 | + """ |
| 82 | + raise NotImplementedError |
0 commit comments