#!/usr/bin/env python
"""
A tool to generate Curl commands for Gluster based on the inputs
"""
import hashlib
from urlparse import urlparse
import jwt

from argparse import ArgumentParser, RawDescriptionHelpFormatter


def get_sign(**kwargs):
    # TODO: Add Data to Sign
    message = bytes("{method}\n{url}".format(
        **kwargs)).encode('utf-8')
    secret = bytes(kwargs.get("app_secret")).encode('utf-8')
    qsh = hashlib.sha256(message).hexdigest()
    return jwt.encode({'iss': kwargs.get("app_id"), 'qsh': qsh},
                      secret, algorithm='HS256')


def get_curl_cmd(args, space=""):
    op = ["{0}curl".format(space)]
    if args.show_headers:
        op[-1] += " -i"

    op[-1] += " -X {0}".format(args.method)
    if args.id:
        url_data = urlparse(args.url)
        signature = get_sign(url=url_data.path,
                             app_secret=args.secret,
                             app_id=args.id,
                             method=args.method)
        op.append('-H "Authorization: Bearer {signature}"'.format(
            signature=signature))

    op.append("{url}".format(url=args.url))

    join_str = " \\\n{0}    ".format(space)
    return join_str.join(op)


def main():
    parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
                            description=__doc__)
    parser.add_argument("--id", help="Application ID")
    parser.add_argument("--secret", help="Application Secret")
    parser.add_argument("--method", help="HTTP Method", default="GET")
    parser.add_argument("--data", help="JSON encoded Data")
    parser.add_argument("--show-headers", help="Show HTTP Response headers",
                        action="store_true")
    parser.add_argument("url", help="URL")
    args = parser.parse_args()

    print get_curl_cmd(args)


if __name__ == "__main__":
    main()
