|
| 1 | +import base64 |
| 2 | +from datetime import datetime as dt |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +import requests |
| 6 | +from requests import Response |
| 7 | + |
| 8 | +from .constants.defaults import Default |
| 9 | +from .constants.errors import ERRORS |
| 10 | + |
| 11 | + |
| 12 | +class ImageKitRequest(object): |
| 13 | + """ |
| 14 | + ImageKitRequest is holds the methods and attributes about server |
| 15 | + communications and communicates to server, used by Internal classes |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, private_key, public_key, url_endpoint, transformation_position, options |
| 20 | + ): |
| 21 | + self.private_key = private_key |
| 22 | + self.public_key = public_key |
| 23 | + self.url_endpoint = url_endpoint |
| 24 | + self.transformation_position = ( |
| 25 | + transformation_position or Default.DEFAULT_TRANSFORMATION_POSITION.value |
| 26 | + ) |
| 27 | + self.options = options or {} |
| 28 | + |
| 29 | + if not (self.private_key and self.public_key and self.url_endpoint): |
| 30 | + raise ValueError(ERRORS.MANDATORY_INITIALIZATION_MISSING.value) |
| 31 | + |
| 32 | + def create_headers(self): |
| 33 | + """Create headers dict and sets Authorization header |
| 34 | + """ |
| 35 | + headers = {"Accept-Encoding": "gzip, deflate"} |
| 36 | + headers.update(self.get_auth_headers()) |
| 37 | + return headers |
| 38 | + |
| 39 | + def get_auth_headers(self): |
| 40 | + """Create dictionary with encoded private key |
| 41 | + The out put is used in request header as authorization header |
| 42 | +
|
| 43 | + :return: dictionary of encoded private key |
| 44 | + """ |
| 45 | + # checking if ':' is appearing for basic authentication |
| 46 | + # otherwise password will be required with username |
| 47 | + # see basic authentication related articles about |
| 48 | + # being authenticated only with username |
| 49 | + if self.private_key[-1] != ":": |
| 50 | + self.private_key += ":" |
| 51 | + encoded_private_key = base64.b64encode(self.private_key.encode()).decode( |
| 52 | + "utf-8" |
| 53 | + ) |
| 54 | + return {"Authorization": "Basic {}".format(encoded_private_key)} |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def request(method, url, headers, params=None, files=None, data=None) -> Response: |
| 58 | + """Requests from ImageKit server used,by internal methods |
| 59 | + """ |
| 60 | + resp = requests.request( |
| 61 | + method=method, |
| 62 | + url=url, |
| 63 | + params=params, |
| 64 | + files=files, |
| 65 | + data=data, |
| 66 | + headers=headers, |
| 67 | + ) |
| 68 | + |
| 69 | + return resp |
| 70 | + |
| 71 | + def extend_url_options(self, options: Dict) -> Dict: |
| 72 | + """ |
| 73 | + adds data to the options from the object, so that |
| 74 | + required data can be used by url builder |
| 75 | + """ |
| 76 | + attr_dict = { |
| 77 | + "public_key": self.public_key, |
| 78 | + "private_key": self.private_key, |
| 79 | + "url_endpoint": self.url_endpoint, |
| 80 | + "transformation_position": self.transformation_position, |
| 81 | + } |
| 82 | + |
| 83 | + extended_options = {**self.options, **attr_dict, **options} |
| 84 | + return extended_options |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def get_signature_timestamp(seconds: int = None) -> int: |
| 88 | + """ |
| 89 | + Returns either default time stamp |
| 90 | + or current unix time and expiry seconds to get |
| 91 | + signature time stamp |
| 92 | + """ |
| 93 | + |
| 94 | + if not seconds: |
| 95 | + return Default.DEFAULT_TIMESTAMP.value |
| 96 | + current_timestamp = int(dt.now().strftime("%s")) |
| 97 | + |
| 98 | + return current_timestamp + seconds |
0 commit comments