diff --git a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/utils.py b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/utils.py index f7f4046b0b74..722565f2e0d1 100644 --- a/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/utils.py +++ b/sdk/communication/azure-communication-chat/azure/communication/chat/_shared/utils.py @@ -20,9 +20,12 @@ def _convert_datetime_to_utc_int(expires_on): epoch = time.mktime(datetime(1970, 1, 1).timetuple()) return epoch-time.mktime(expires_on.timetuple()) - def parse_connection_str(conn_str): # type: (str) -> Tuple[str, str, str, str] + if conn_str is None: + raise ValueError( + "Connection string is undefined." + ) endpoint = None shared_access_key = None for element in conn_str.split(";"): @@ -33,8 +36,8 @@ def parse_connection_str(conn_str): shared_access_key = value if not all([endpoint, shared_access_key]): raise ValueError( - "Invalid connection string. Should be in the format: " - "endpoint=sb:///;accesskey=" + "Invalid connection string. You can get the connection string from your resource page in the Azure Portal. " + "The format should be as follows: endpoint=https:///;accesskey=" ) left_slash_pos = cast(str, endpoint).find("//") if left_slash_pos != -1: @@ -44,7 +47,6 @@ def parse_connection_str(conn_str): return host, str(shared_access_key) - def get_current_utc_time(): # type: () -> str return str(datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S ")) + "GMT" @@ -55,7 +57,6 @@ def get_current_utc_as_int(): current_utc_datetime = datetime.utcnow().replace(tzinfo=TZ_UTC) return _convert_datetime_to_utc_int(current_utc_datetime) - def create_access_token(token): # type: (str) -> azure.core.credentials.AccessToken """Creates an instance of azure.core.credentials.AccessToken from a @@ -84,7 +85,6 @@ def create_access_token(token): except ValueError: raise ValueError(token_parse_err_msg) - def get_authentication_policy( endpoint, # type: str credential, # type: TokenCredential or str diff --git a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py index 8ff385e81d04..5c2b79fd60dc 100644 --- a/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py +++ b/sdk/communication/azure-communication-identity/azure/communication/identity/_shared/utils.py @@ -15,8 +15,16 @@ from msrest.serialization import TZ_UTC from azure.core.credentials import AccessToken +def _convert_datetime_to_utc_int(expires_on): + epoch = time.mktime(datetime(1970, 1, 1).timetuple()) + return epoch-time.mktime(expires_on.timetuple()) + def parse_connection_str(conn_str): # type: (str) -> Tuple[str, str, str, str] + if conn_str is None: + raise ValueError( + "Connection string is undefined." + ) endpoint = None shared_access_key = None for element in conn_str.split(";"): @@ -27,8 +35,8 @@ def parse_connection_str(conn_str): shared_access_key = value if not all([endpoint, shared_access_key]): raise ValueError( - "Invalid connection string. Should be in the format: " - "endpoint=sb:///;accesskey=" + "Invalid connection string. You can get the connection string from your resource page in the Azure Portal. " + "The format should be as follows: endpoint=https:///;accesskey=" ) left_slash_pos = cast(str, endpoint).find("//") if left_slash_pos != -1: @@ -38,11 +46,15 @@ def parse_connection_str(conn_str): return host, str(shared_access_key) - def get_current_utc_time(): # type: () -> str return str(datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S ")) + "GMT" +def get_current_utc_as_int(): + # type: () -> int + current_utc_datetime = datetime.utcnow().replace(tzinfo=TZ_UTC) + return _convert_datetime_to_utc_int(current_utc_datetime) + def create_access_token(token): # type: (str) -> azure.core.credentials.AccessToken """Creates an instance of azure.core.credentials.AccessToken from a @@ -67,7 +79,7 @@ def create_access_token(token): padded_base64_payload = base64.b64decode(parts[1] + "==").decode('ascii') payload = json.loads(padded_base64_payload) return AccessToken(token, - _convert_expires_on_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) + _convert_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) except ValueError: raise ValueError(token_parse_err_msg) @@ -75,7 +87,6 @@ def _convert_expires_on_datetime_to_utc_int(expires_on): epoch = time.mktime(datetime(1970, 1, 1).timetuple()) return epoch-time.mktime(expires_on.timetuple()) - def get_authentication_policy( endpoint, # type: str credential, # type: TokenCredential or str diff --git a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/utils.py b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/utils.py index 12f338c8dbd7..b38aaafc6194 100644 --- a/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/utils.py +++ b/sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/_shared/utils.py @@ -15,8 +15,16 @@ from msrest.serialization import TZ_UTC from azure.core.credentials import AccessToken +def _convert_datetime_to_utc_int(expires_on): + epoch = time.mktime(datetime(1970, 1, 1).timetuple()) + return epoch-time.mktime(expires_on.timetuple()) + def parse_connection_str(conn_str): # type: (str) -> Tuple[str, str, str, str] + if conn_str is None: + raise ValueError( + "Connection string is undefined." + ) endpoint = None shared_access_key = None for element in conn_str.split(";"): @@ -27,8 +35,8 @@ def parse_connection_str(conn_str): shared_access_key = value if not all([endpoint, shared_access_key]): raise ValueError( - "Invalid connection string. Should be in the format: " - "endpoint=sb:///;accesskey=" + "Invalid connection string. You can get the connection string from your resource page in the Azure Portal. " + "The format should be as follows: endpoint=https:///;accesskey=" ) left_slash_pos = cast(str, endpoint).find("//") if left_slash_pos != -1: @@ -38,11 +46,15 @@ def parse_connection_str(conn_str): return host, str(shared_access_key) - def get_current_utc_time(): # type: () -> str return str(datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S ")) + "GMT" +def get_current_utc_as_int(): + # type: () -> int + current_utc_datetime = datetime.utcnow().replace(tzinfo=TZ_UTC) + return _convert_datetime_to_utc_int(current_utc_datetime) + def create_access_token(token): # type: (str) -> azure.core.credentials.AccessToken """Creates an instance of azure.core.credentials.AccessToken from a @@ -67,7 +79,7 @@ def create_access_token(token): padded_base64_payload = base64.b64decode(parts[1] + "==").decode('ascii') payload = json.loads(padded_base64_payload) return AccessToken(token, - _convert_expires_on_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) + _convert_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) except ValueError: raise ValueError(token_parse_err_msg) diff --git a/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/utils.py b/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/utils.py index 12f338c8dbd7..8cfaa8f7e67d 100644 --- a/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/utils.py +++ b/sdk/communication/azure-communication-sms/azure/communication/sms/_shared/utils.py @@ -15,8 +15,16 @@ from msrest.serialization import TZ_UTC from azure.core.credentials import AccessToken +def _convert_datetime_to_utc_int(expires_on): + epoch = time.mktime(datetime(1970, 1, 1).timetuple()) + return epoch-time.mktime(expires_on.timetuple()) + def parse_connection_str(conn_str): # type: (str) -> Tuple[str, str, str, str] + if conn_str is None: + raise ValueError( + "Connection string is undefined." + ) endpoint = None shared_access_key = None for element in conn_str.split(";"): @@ -27,8 +35,8 @@ def parse_connection_str(conn_str): shared_access_key = value if not all([endpoint, shared_access_key]): raise ValueError( - "Invalid connection string. Should be in the format: " - "endpoint=sb:///;accesskey=" + "Invalid connection string. You can get the connection string from your resource page in the Azure Portal. " + "The format should be as follows: endpoint=https:///;accesskey=" ) left_slash_pos = cast(str, endpoint).find("//") if left_slash_pos != -1: @@ -38,11 +46,15 @@ def parse_connection_str(conn_str): return host, str(shared_access_key) - def get_current_utc_time(): # type: () -> str return str(datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S ")) + "GMT" +def get_current_utc_as_int(): + # type: () -> int + current_utc_datetime = datetime.utcnow().replace(tzinfo=TZ_UTC) + return _convert_datetime_to_utc_int(current_utc_datetime) + def create_access_token(token): # type: (str) -> azure.core.credentials.AccessToken """Creates an instance of azure.core.credentials.AccessToken from a @@ -67,7 +79,7 @@ def create_access_token(token): padded_base64_payload = base64.b64decode(parts[1] + "==").decode('ascii') payload = json.loads(padded_base64_payload) return AccessToken(token, - _convert_expires_on_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) + _convert_datetime_to_utc_int(datetime.fromtimestamp(payload['exp']).replace(tzinfo=TZ_UTC))) except ValueError: raise ValueError(token_parse_err_msg) @@ -101,7 +113,3 @@ def get_authentication_policy( raise TypeError("Unsupported credential: {}. Use an access token string to use HMACCredentialsPolicy" "or a token credential from azure.identity".format(type(credential))) - -def _convert_expires_on_datetime_to_utc_int(expires_on): - epoch = time.mktime(datetime(1970, 1, 1).timetuple()) - return epoch-time.mktime(expires_on.timetuple())