import json


def build_curl_call(method, url, data=None, headers=None):
    """
    Build and return a ``curl`` command for use on the command-line.

    :param method: HTTP request method, ``GET``, ``POST``, etc.
    :type method: string
    :param url: Full HTTP path.
    :type url: string
    :param data: Data to be transmitted, usually *posted*.
    :type data: object serializable as JSON
    :param headers: Additional HTTP request headers.
    :type headers: dictionary
    :rtype: string

    Example:

    .. code-block:: python

        cmd = build_curl_call('POST', 'http://foo.com/bar', data={'x': 42},
                headers={'SUPER_SECRET_KEY': '123'})
        print(cmd)
        curl -X POST "http://foo.com/bar" -H "SUPER_SECRET_KEY: 123" --data "{\"x\": 42}"
    """
    command = 'curl -X {0} "{1}"'.format(method.upper(), url)
    if headers:
        for k, v in headers.items():
            command += ' -H "{0}: {1}"'.format(k, v)
    if data:
        jsdata = json.dumps(data)
        command += " --data {0}".format(json.dumps(jsdata))
    return command