import yarl
from typing import Tuple
from typing import Optional


def get_components(url: yarl.URL) -> Tuple[str, Optional[str]]:
    """Return the namespace and collection of a given query.

    :param yarl.URL url: The :class:`yarl.URL` to process. Only REST
       API query URLs in the DBG census API format are allowed.
    :return: The namespace/game and collection that was accessed.
       Collection may be :obj:`None` for some queries.
    """
    components = url.path.split('/')[1:]
    if components[0].startswith('s:'):  # Remove service ID
        _ = components.pop(0)
    if components[0] in ('xml', 'json'):  # Remove format specifier
        _ = components.pop(0)
    _ = components.pop(0)  # Remove query verb
    if not components[-1]:  # Remove collection if empty
        _ = components.pop(-1)
    # If only a namespace was provided, return None as the collection
    if len(components) == 1:
        return components[0], None
    assert len(components) == 2, 'Unable to parse URL'
    return components[0], components[1]