import json


def build_n1ql_request_body(query: str, *args, **kwargs) -> dict:
    """Build request body for N1QL REST API.

    Request body consists of ``statement`` key, ``args`` key
    (if using positional parameters), and any key prefixed with ``$``
    (if using named parameters).

    See https://docs.couchbase.com/server/current/n1ql/n1ql-rest-api/index.html for reference.

    :params query: N1QL query string.
    :params *args: Positional parameters passed as ``args`` in request body.
    :params **kwargs: Named parameters passed as ``$``-prefixed
                      parameter in request body.
    """
    body = {"statement": query}

    if args:
        body["args"] = json.dumps(args or [])

    if kwargs:
        for k, v in kwargs.items():
            body[f"${k}"] = json.dumps(v)
    return body