|
| 1 | +from typing import Any, Dict |
| 2 | + |
| 3 | +from .constants.errors import ERRORS |
| 4 | +from .file import File |
| 5 | +from .resource import ImageKitRequest |
| 6 | +from .url import Url |
| 7 | +from .utils.calculation import get_authenticated_params, hamming_distance |
| 8 | + |
| 9 | + |
| 10 | +class ImageKit(object): |
| 11 | + """ |
| 12 | + Main Class What user will use by creating |
| 13 | + instance |
| 14 | + """ |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + public_key=None, |
| 18 | + private_key=None, |
| 19 | + url_endpoint=None, |
| 20 | + transformation_position=None, |
| 21 | + options=None, |
| 22 | + ): |
| 23 | + self.ik_request = ImageKitRequest( |
| 24 | + private_key, public_key, url_endpoint, transformation_position, options |
| 25 | + ) |
| 26 | + self.file = File(self.ik_request) |
| 27 | + self.url_obj = Url(self.ik_request) |
| 28 | + |
| 29 | + def upload(self, file=None, file_name=None, options=None) -> Dict[str, Any]: |
| 30 | + """Provides upload functionality |
| 31 | + """ |
| 32 | + return self.file.upload(file, file_name, options) |
| 33 | + |
| 34 | + def list_files(self, options: Dict) -> Dict: |
| 35 | + """Get list(filtered if given param) of images of client |
| 36 | + """ |
| 37 | + return self.file.list(options) |
| 38 | + |
| 39 | + def get_file_details(self, file_identifier: str = None) -> Dict: |
| 40 | + """Get file_detail by file_id or file_url |
| 41 | + """ |
| 42 | + return self.file.details(file_identifier) |
| 43 | + |
| 44 | + def update_file_details(self, file_id: str, options: dict = None) -> Dict: |
| 45 | + """Update file detail by file id and options |
| 46 | + """ |
| 47 | + return self.file.update_file_details(file_id, options) |
| 48 | + |
| 49 | + def delete_file(self, file_id: str = None) -> Dict[str, Any]: |
| 50 | + """Delete file by file_id |
| 51 | + """ |
| 52 | + return self.file.delete(file_id) |
| 53 | + |
| 54 | + def purge_cache(self, file_url: str = None) -> Dict[str, Any]: |
| 55 | + """Purge Cache from server by file url |
| 56 | + """ |
| 57 | + return self.file.purge_cache(file_url) |
| 58 | + |
| 59 | + def get_purge_cache_status(self, purge_cache_id: str = "") -> Dict[str, Any]: |
| 60 | + """Get Purge Cache status by purge cache request_id |
| 61 | + """ |
| 62 | + return self.file.get_purge_cache_status(str(purge_cache_id)) |
| 63 | + |
| 64 | + def get_metadata(self, file_id: str = None) -> Dict[str, Any]: |
| 65 | + """Get Meta Data of a file by file id |
| 66 | + """ |
| 67 | + return self.file.get_metadata(str(file_id)) |
| 68 | + |
| 69 | + def url(self, options: Dict[str, Any]) -> str: |
| 70 | + """Get generated Url from options parameter |
| 71 | + """ |
| 72 | + return self.url_obj.generate_url(options) |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def phash_distance(first, second): |
| 76 | + """Get hamming distance between two phash(to check similarity) |
| 77 | + """ |
| 78 | + if not (first and second): |
| 79 | + raise TypeError(ERRORS.MISSING_PHASH_VALUE.value) |
| 80 | + return hamming_distance(first, second) |
| 81 | + |
| 82 | + def get_authentication_parameters(self, token="", expire=0): |
| 83 | + """Get Authentication parameters |
| 84 | + """ |
| 85 | + return get_authenticated_params(token, expire, self.ik_request.private_key) |
0 commit comments