from typing import Union
from typing import Iterable
from typing import Dict
from typing import List
import requests
from bs4 import BeautifulSoup


def process_request(url: str, tags: Union[str, Iterable[str]]) \
    -> Dict[str, Union[str, List[str]]]:
    """ Scrape and parse xml retrieved from ``url``
    and return parsed contents within ``tags``.

    Parameters
    ----------
    url: str
    tags: str or an iterable of str
        HTML tag(s) to retrieve the contents of.

    Returns
    -------
    A dictionary of ``tags`` as keys
    and their contents (str or list of str) as values.

    Raises
    ------
    HTML and parsing errors from ``requests`` and ``BeautifulSoup`` libraries.

    """
    contents = {}
    if isinstance(tags, str):
        tags = (tags,)
    raw_html = requests.get(url)
    parse = BeautifulSoup(raw_html.content, 'html.parser')
    for tag in tags:
        data = parse.find_all(tag)
        if len(set(data)) == 1:
            contents[tag] = data[0].text
        else:
            contents[tag] = [item.text for item in data]  # keep picklable
    return contents