Urlencode a dict or list into a string. This is similar to urllib.urlencode except that: * It uses quote, and not quote_plus * It has a default list of safe chars that don't need to be encoded, which matches what AWS services expect. If any value in the input ``mapping`` is
(mapping, safe=SAFE_CHARS)
| 717 | |
| 718 | |
| 719 | def percent_encode_sequence(mapping, safe=SAFE_CHARS): |
| 720 | """Urlencode a dict or list into a string. |
| 721 | |
| 722 | This is similar to urllib.urlencode except that: |
| 723 | |
| 724 | * It uses quote, and not quote_plus |
| 725 | * It has a default list of safe chars that don't need |
| 726 | to be encoded, which matches what AWS services expect. |
| 727 | |
| 728 | If any value in the input ``mapping`` is a list type, |
| 729 | then each list element wil be serialized. This is the equivalent |
| 730 | to ``urlencode``'s ``doseq=True`` argument. |
| 731 | |
| 732 | This function should be preferred over the stdlib |
| 733 | ``urlencode()`` function. |
| 734 | |
| 735 | :param mapping: Either a dict to urlencode or a list of |
| 736 | ``(key, value)`` pairs. |
| 737 | |
| 738 | """ |
| 739 | encoded_pairs = [] |
| 740 | if hasattr(mapping, 'items'): |
| 741 | pairs = mapping.items() |
| 742 | else: |
| 743 | pairs = mapping |
| 744 | for key, value in pairs: |
| 745 | if isinstance(value, list): |
| 746 | for element in value: |
| 747 | encoded_pairs.append( |
| 748 | f'{percent_encode(key)}={percent_encode(element)}' |
| 749 | ) |
| 750 | else: |
| 751 | encoded_pairs.append( |
| 752 | f'{percent_encode(key)}={percent_encode(value)}' |
| 753 | ) |
| 754 | return '&'.join(encoded_pairs) |
| 755 | |
| 756 | |
| 757 | def percent_encode(input_str, safe=SAFE_CHARS): |