Update the given `prepared_request`'s headers with the original ones. This allows the requests to be prepared as usual, and then later merged with headers that are specified multiple times.
(
original_headers: HTTPHeadersDict,
prepared_request: requests.PreparedRequest
)
| 231 | |
| 232 | |
| 233 | def apply_missing_repeated_headers( |
| 234 | original_headers: HTTPHeadersDict, |
| 235 | prepared_request: requests.PreparedRequest |
| 236 | ) -> None: |
| 237 | """Update the given `prepared_request`'s headers with the original |
| 238 | ones. This allows the requests to be prepared as usual, and then later |
| 239 | merged with headers that are specified multiple times.""" |
| 240 | |
| 241 | new_headers = HTTPHeadersDict(prepared_request.headers) |
| 242 | for prepared_name, prepared_value in prepared_request.headers.items(): |
| 243 | if prepared_name not in original_headers: |
| 244 | continue |
| 245 | |
| 246 | original_keys, original_values = zip(*filter( |
| 247 | lambda item: item[0].casefold() == prepared_name.casefold(), |
| 248 | original_headers.items() |
| 249 | )) |
| 250 | |
| 251 | if prepared_value not in original_values: |
| 252 | # If the current value is not among the initial values |
| 253 | # set for this field, then it means that this field got |
| 254 | # overridden on the way, and we should preserve it. |
| 255 | continue |
| 256 | |
| 257 | new_headers.popone(prepared_name) |
| 258 | new_headers.update(zip(original_keys, original_values)) |
| 259 | |
| 260 | prepared_request.headers = new_headers |
| 261 | |
| 262 | |
| 263 | def make_default_headers(args: argparse.Namespace) -> HTTPHeadersDict: |
no test coverage detected