ringcentral.core
1import urllib 2import base64 3import sys 4import re 5 6 7def is_third(): 8 return sys.version_info >= (3, 0) 9 10 11def urlencode(s): 12 """ 13 Encodes the given dictionary `s` into a URL-encoded string. 14 15 Parameters: 16 s (dict): A dictionary containing the key-value pairs to be encoded. 17 18 Returns: 19 str: A URL-encoded string representing the input dictionary. 20 21 Raises: 22 Exception: If neither `urllib.urlencode` nor `urllib.parse.urlencode` is available. 23 24 Note: 25 This function checks for the presence of `urllib.urlencode` and `urllib.parse.urlencode` 26 to ensure compatibility across Python 2 and Python 3. 27 """ 28 if hasattr(urllib, 'urlencode'): 29 return urllib.urlencode(s) 30 elif hasattr(urllib, 'parse'): 31 return urllib.parse.urlencode(s) 32 else: 33 raise Exception("No urlencode") 34 35 36def iterator(thing): 37 """ 38 Returns an iterator over key-value pairs of `thing`. 39 40 If `thing` has an `iteritems` method, it is used; otherwise, `thing.items()` is iterated over. 41 42 Parameters: 43 thing: An iterable object. 44 45 Returns: 46 iterator: An iterator over the key-value pairs of `thing`. 47 """ 48 return thing.iteritems() if hasattr(thing, 'iteritems') else iter(thing.items()) 49 50 51def base64encode(s): 52 """ 53 Encodes the input string `s` into base64 format. 54 55 Parameters: 56 s (str): The string to be encoded. 57 58 Returns: 59 str: The base64 encoded string. 60 61 """ 62 # Use Python 3 compatible base64 encoding if detected, otherwise use default encoding 63 if is_third(): 64 return str(base64.b64encode(bytes(s, 'utf8')), 'utf8') 65 else: 66 return base64.b64encode(s) 67 68 69def tostr(s): 70 if is_third(): 71 return str(s, 'utf8') 72 else: 73 return str(s) 74 75 76def clean_decrypted(s): 77 """ 78 Cleans the decrypted string `s` by removing specific control characters. 79 80 Parameters: 81 s (str): The decrypted string to be cleaned. 82 83 Returns: 84 str: The cleaned decrypted string. 85 """ 86 if is_third(): 87 return re.sub(r"[\u0001-\u0010]", '', s).strip() 88 else: 89 return s.replace('\x05', '')
12def urlencode(s): 13 """ 14 Encodes the given dictionary `s` into a URL-encoded string. 15 16 Parameters: 17 s (dict): A dictionary containing the key-value pairs to be encoded. 18 19 Returns: 20 str: A URL-encoded string representing the input dictionary. 21 22 Raises: 23 Exception: If neither `urllib.urlencode` nor `urllib.parse.urlencode` is available. 24 25 Note: 26 This function checks for the presence of `urllib.urlencode` and `urllib.parse.urlencode` 27 to ensure compatibility across Python 2 and Python 3. 28 """ 29 if hasattr(urllib, 'urlencode'): 30 return urllib.urlencode(s) 31 elif hasattr(urllib, 'parse'): 32 return urllib.parse.urlencode(s) 33 else: 34 raise Exception("No urlencode")
Encodes the given dictionary s
into a URL-encoded string.
Parameters: s (dict): A dictionary containing the key-value pairs to be encoded.
Returns: str: A URL-encoded string representing the input dictionary.
Raises:
Exception: If neither urllib.urlencode
nor urllib.parse.urlencode
is available.
Note:
This function checks for the presence of urllib.urlencode
and urllib.parse.urlencode
to ensure compatibility across Python 2 and Python 3.
37def iterator(thing): 38 """ 39 Returns an iterator over key-value pairs of `thing`. 40 41 If `thing` has an `iteritems` method, it is used; otherwise, `thing.items()` is iterated over. 42 43 Parameters: 44 thing: An iterable object. 45 46 Returns: 47 iterator: An iterator over the key-value pairs of `thing`. 48 """ 49 return thing.iteritems() if hasattr(thing, 'iteritems') else iter(thing.items())
Returns an iterator over key-value pairs of thing
.
If thing
has an iteritems
method, it is used; otherwise, thing.items()
is iterated over.
Parameters: thing: An iterable object.
Returns:
iterator: An iterator over the key-value pairs of thing
.
52def base64encode(s): 53 """ 54 Encodes the input string `s` into base64 format. 55 56 Parameters: 57 s (str): The string to be encoded. 58 59 Returns: 60 str: The base64 encoded string. 61 62 """ 63 # Use Python 3 compatible base64 encoding if detected, otherwise use default encoding 64 if is_third(): 65 return str(base64.b64encode(bytes(s, 'utf8')), 'utf8') 66 else: 67 return base64.b64encode(s)
Encodes the input string s
into base64 format.
Parameters: s (str): The string to be encoded.
Returns: str: The base64 encoded string.
77def clean_decrypted(s): 78 """ 79 Cleans the decrypted string `s` by removing specific control characters. 80 81 Parameters: 82 s (str): The decrypted string to be cleaned. 83 84 Returns: 85 str: The cleaned decrypted string. 86 """ 87 if is_third(): 88 return re.sub(r"[\u0001-\u0010]", '', s).strip() 89 else: 90 return s.replace('\x05', '')
Cleans the decrypted string s
by removing specific control characters.
Parameters: s (str): The decrypted string to be cleaned.
Returns: str: The cleaned decrypted string.