33import sys
44from datetime import datetime as dt
55from typing import Any , Dict , List
6- from urllib .parse import ParseResult , urlparse , urlunparse , parse_qsl , urlencode
6+ from urllib .parse import ParseResult , urlparse , urlunparse , parse_qsl , urlencode , quote
77
88from .constants .defaults import Default
99from .constants .supported_transform import SUPPORTED_TRANS
@@ -85,6 +85,7 @@ def build_url(self, options: dict) -> str:
8585 private_key = options .get ("private_key" )
8686 expiry_timestamp = self .get_signature_timestamp (expire_seconds )
8787 url_signature = self .get_signature (
88+ self ,
8889 private_key = private_key ,
8990 url = url_object .geturl (),
9091 url_endpoint = url_endpoint ,
@@ -129,7 +130,14 @@ def get_signature_timestamp(expiry_seconds: int = None) -> int:
129130 return current_timestamp + expiry_seconds
130131
131132 @staticmethod
132- def get_signature (private_key , url , url_endpoint , expiry_timestamp : int ) -> str :
133+ def get_signature (self , private_key , url , url_endpoint , expiry_timestamp : int ) -> str :
134+ last_slash_pos = url .rfind ('/' )
135+ question_mark_pos = url .find ('?' , last_slash_pos )
136+ path = url [last_slash_pos + 1 :question_mark_pos ] if question_mark_pos != - 1 else url [last_slash_pos + 1 :]
137+ encoded_path = self .encode_string_if_required (self ,path )
138+ encoded_url = url [:last_slash_pos + 1 ] + encoded_path + url [question_mark_pos :] if question_mark_pos != - 1 else url [:last_slash_pos + 1 ] + encoded_path
139+ url = encoded_url
140+ print (url )
133141 """ "
134142 create signature(hashed hex key) from
135143 private_key, url, url_endpoint and expiry_timestamp
@@ -211,3 +219,11 @@ def transformation_to_str(transformation):
211219 )
212220
213221 return Default .CHAIN_TRANSFORM_DELIMITER .value .join (parsed_transforms )
222+
223+ @staticmethod
224+ def has_more_than_ascii (s ):
225+ return any (ord (char ) > 127 for char in s )
226+
227+ @staticmethod
228+ def encode_string_if_required (self ,s ):
229+ return quote (s ) if self .has_more_than_ascii (s ) else s
0 commit comments