|
| 1 | +"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +def sort_query_parameters(url): |
| 7 | + parts = url.split("?") |
| 8 | + |
| 9 | + if len(parts) == 1: |
| 10 | + return url |
| 11 | + |
| 12 | + query = parts[1] |
| 13 | + params = query.split("&") |
| 14 | + |
| 15 | + params.sort(key=lambda x: x.split('=')[0]) |
| 16 | + |
| 17 | + return parts[0] + "?" + "&".join(params) |
| 18 | + |
| 19 | + |
| 20 | +def sort_serialized_maps(inp: any, regex: str, delim: str): |
| 21 | + |
| 22 | + def sort_map(m): |
| 23 | + entire_match = m.group(0) |
| 24 | + |
| 25 | + groups = m.groups() |
| 26 | + |
| 27 | + for group in groups: |
| 28 | + pairs = [] |
| 29 | + if '=' in group: |
| 30 | + pairs = group.split(delim) |
| 31 | + |
| 32 | + pairs.sort(key=lambda x: x.split('=')[0]) |
| 33 | + else: |
| 34 | + values = group.split(delim) |
| 35 | + |
| 36 | + if len(values) == 1: |
| 37 | + pairs = values |
| 38 | + else: |
| 39 | + pairs = [''] * int(len(values)/2) |
| 40 | + # loop though every 2nd item |
| 41 | + for i in range(0, len(values), 2): |
| 42 | + pairs[int(i/2)] = values[i] + delim + values[i+1] |
| 43 | + |
| 44 | + pairs.sort(key=lambda x: x.split(delim)[0]) |
| 45 | + |
| 46 | + entire_match = entire_match.replace(group, delim.join(pairs)) |
| 47 | + |
| 48 | + return entire_match |
| 49 | + |
| 50 | + if isinstance(inp, str): |
| 51 | + return re.sub(regex, sort_map, inp) |
| 52 | + elif isinstance(inp, list): |
| 53 | + for i, v in enumerate(inp): |
| 54 | + inp[i] = sort_serialized_maps(v, regex, delim) |
| 55 | + return inp |
| 56 | + elif isinstance(inp, dict): |
| 57 | + for k, v in inp.items(): |
| 58 | + inp[k] = sort_serialized_maps(v, regex, delim) |
| 59 | + return inp |
| 60 | + else: |
| 61 | + raise Exception("Unsupported type") |
0 commit comments