Source code for worldline.acquiring.sdk.communication.response_header
import re
from typing import Mapping, Optional, Tuple
[docs]
def get_header_value(headers: Optional[Mapping[str, str]], header_name: str) -> Optional[str]:
"""
:return: The value of the header with the given name, or None if there was no such header.
"""
if headers is None:
return None
for name, value in headers.items():
if name.lower() == header_name.lower():
return value
return None
[docs]
def get_header(headers: Optional[Mapping[str, str]], header_name: str) -> Optional[Tuple[str, str]]:
"""
:return: The header with the given name as a tuple with the name and value, or None if there was no such header.
"""
if headers is None:
return None
for name, value in headers.items():
if name.lower() == header_name.lower():
return name, value
return None
[docs]
def get_disposition_filename(headers: Optional[Mapping[str, str]]) -> Optional[str]:
"""
:return: The value of the filename parameter of the Content-Disposition header, or None if there was no such header or parameter.
"""
header_value = get_header_value(headers, "Content-Disposition")
if header_value is None:
return None
pattern = re.compile("(?:^|;)\\s*filename\\s*=\\s*(.*?)\\s*(?:;|$)", re.IGNORECASE)
match = pattern.search(header_value)
if match is not None:
filename = match.group(1)
return __trim_quotes(filename)
return None
def __trim_quotes(filename: str) -> str:
if len(filename) < 2:
return filename
if (filename.startswith("\"") and filename.endswith("\"")) or \
(filename.startswith("'") and filename.endswith("'")):
return filename[1:-1]
return filename