Get parameters as list of tuples, formatting collections. :param params: Parameters as dict or list of two-tuples :param dict collection_formats: Parameter collection formats :return: URL query string (e.g. a=Hello%20World&b=123)
(self, params, collection_formats)
| 469 | return new_params |
| 470 | |
| 471 | def parameters_to_url_query(self, params, collection_formats): |
| 472 | """Get parameters as list of tuples, formatting collections. |
| 473 | |
| 474 | :param params: Parameters as dict or list of two-tuples |
| 475 | :param dict collection_formats: Parameter collection formats |
| 476 | :return: URL query string (e.g. a=Hello%20World&b=123) |
| 477 | """ |
| 478 | new_params: List[Tuple[str, str]] = [] |
| 479 | if collection_formats is None: |
| 480 | collection_formats = {} |
| 481 | for k, v in params.items() if isinstance(params, dict) else params: |
| 482 | if isinstance(v, bool): |
| 483 | v = str(v).lower() |
| 484 | if isinstance(v, (int, float)): |
| 485 | v = str(v) |
| 486 | if isinstance(v, dict): |
| 487 | v = json.dumps(v) |
| 488 | |
| 489 | if k in collection_formats: |
| 490 | collection_format = collection_formats[k] |
| 491 | if collection_format == 'multi': |
| 492 | new_params.extend((k, str(value)) for value in v) |
| 493 | else: |
| 494 | if collection_format == 'ssv': |
| 495 | delimiter = ' ' |
| 496 | elif collection_format == 'tsv': |
| 497 | delimiter = '\t' |
| 498 | elif collection_format == 'pipes': |
| 499 | delimiter = '|' |
| 500 | else: # csv is the default |
| 501 | delimiter = ',' |
| 502 | new_params.append( |
| 503 | (k, delimiter.join(quote(str(value)) for value in v)) |
| 504 | ) |
| 505 | else: |
| 506 | new_params.append((k, quote(str(v)))) |
| 507 | |
| 508 | return "&".join(["=".join(map(str, item)) for item in new_params]) |
| 509 | |
| 510 | def files_parameters(self, files: Dict[str, Union[str, bytes]]): |
| 511 | """Builds form parameters. |