def format_query(query, pretty_print=False, track_in_lsi=False, always_ok=
    False, error_code='', format_statements=True):
    """This function formats and URL-encodes a raw LiQL query to be able to use it within a Community v2 API URL.

    .. versionchanged:: 2.1.0
       Queries ending in a semicolon (``;``) will have that character stripped to avoid syntax errors.

    .. versionchanged:: 2.0.0
       Added URL-encoding support for several additional characters.

    :param query: The LiQL query to be formatted and url-encoded
    :type query: str
    :param pretty_print: Defines if the response should be "pretty printed" (``False`` by default)
    :type pretty_print: bool
    :param track_in_lsi: Defines if the query should be tracked within LSI (``False`` by default)
    :type track_in_lsi: bool
    :param always_ok: Defines if the HTTP response should **always** be ``200 OK`` (``False`` by default)
    :type always_ok: bool
    :param error_code: Allows an error code to optionally be supplied for testing purposes (ignored by default)
    :type error_code: str
    :param format_statements: Determines if statements (e.g. ``SELECT``, ``FROM``, et.) should be formatted to be in
                              all caps (``True`` by default)
    :type format_statements: bool
    :returns: The properly formatted query to be inserted in the URL
    """
    chars_to_encode = {' ': '+', '=': '%3D', '"': '%22', "'": '%27', '(':
        '%28', '}': '%29', '@': '%40'}
    query_statements = {'select': 'SELECT', 'from': 'FROM', 'where':
        'WHERE', 'order by': 'ORDER BY', 'desc': 'DESC ', 'limit': 'LIMIT',
        'asc': 'ASC', 'offset': 'OFFSET'}
    parameters = {'pretty_print': '&api.pretty_print=true', 'track_in_lsi':
        '&api.for_ui_search=true', 'always_ok': '&api.always_ok',
        'error_code': '&api.error_code='}
    replacements = [chars_to_encode]
    if format_statements:
        replacements.append(query_statements)
    for criteria in replacements:
        for orig_string, new_string in criteria.items():
            query = query.replace(orig_string, new_string)
            if query.endswith(';'):
                query = query[:-1]
    if pretty_print:
        query = f"{query}{parameters.get('pretty_print')}"
    if track_in_lsi:
        query = f"{query}{parameters.get('track_in_lsi')}"
    if always_ok:
        query = f"{query}{parameters.get('always_ok')}"
    if error_code != '':
        query = f"{query}{parameters.get('error_code')}{error_code}"
    return query
