httpie-cli/httpie/__main__.py

111 lines
3.5 KiB
Python
Raw Normal View History

2012-02-25 13:39:38 +01:00
#!/usr/bin/env python
import os
import sys
import json
import requests
2012-03-04 10:48:30 +01:00
from collections import OrderedDict
2012-02-25 13:39:38 +01:00
from requests.structures import CaseInsensitiveDict
2012-03-04 10:48:30 +01:00
from . import cli
from . import pretty
from . import __version__ as version
2012-02-25 13:39:38 +01:00
DEFAULT_UA = 'HTTPie/%s' % version
2012-02-25 13:39:38 +01:00
TYPE_FORM = 'application/x-www-form-urlencoded; charset=utf-8'
TYPE_JSON = 'application/json; charset=utf-8'
2012-03-02 01:40:40 +01:00
def main(args=None,
stdin=sys.stdin,
2012-03-02 02:36:21 +01:00
stdin_isatty=sys.stdin.isatty(),
2012-03-02 01:40:40 +01:00
stdout=sys.stdout,
stdout_isatty=sys.stdout.isatty()):
2012-03-04 10:48:30 +01:00
parser = cli.parser
2012-03-02 01:40:40 +01:00
args = parser.parse_args(args if args is not None else sys.argv[1:])
do_prettify = (args.prettify is True or
2012-03-04 10:48:30 +01:00
(args.prettify == cli.PRETTIFY_STDOUT_TTY_ONLY and stdout_isatty))
2012-02-25 13:39:38 +01:00
# Parse request headers and data from the command line.
headers = CaseInsensitiveDict()
headers['User-Agent'] = DEFAULT_UA
2012-03-04 10:48:30 +01:00
data = OrderedDict()
try:
cli.parse_items(items=args.items, headers=headers, data=data)
except cli.ParseError as e:
if args.traceback:
raise
parser.error(e.message)
2012-02-25 13:39:38 +01:00
2012-03-02 01:40:40 +01:00
if not stdin_isatty:
2012-03-04 10:48:30 +01:00
if data:
parser.error('Request body (stdin) and request '
'data (key=value) cannot be mixed.')
2012-03-02 01:40:40 +01:00
data = stdin.read()
2012-02-25 13:39:38 +01:00
# JSON/Form content type.
if args.json or (not args.form and data):
2012-03-02 01:40:40 +01:00
if stdin_isatty:
2012-02-25 13:39:38 +01:00
data = json.dumps(data)
if 'Content-Type' not in headers and (data or args.json):
headers['Content-Type'] = TYPE_JSON
elif 'Content-Type' not in headers:
headers['Content-Type'] = TYPE_FORM
# Fire the request.
2012-02-26 16:13:12 +01:00
try:
response = requests.request(
method=args.method.lower(),
url=args.url if '://' in args.url else 'http://%s' % args.url,
headers=headers,
data=data,
verify=True if args.verify == 'yes' else args.verify,
timeout=args.timeout,
auth=(args.auth.key, args.auth.value) if args.auth else None,
proxies=dict((p.key, p.value) for p in args.proxy),
files=dict((os.path.basename(f.name), f) for f in args.file),
allow_redirects=args.allow_redirects,
2012-02-26 16:13:12 +01:00
)
2012-03-04 10:48:30 +01:00
except (KeyboardInterrupt, SystemExit):
2012-02-26 16:13:12 +01:00
sys.stderr.write('\n')
sys.exit(1)
except Exception as e:
if args.traceback:
raise
sys.stderr.write(str(e.message) + '\n')
sys.exit(1)
2012-02-25 13:39:38 +01:00
# Reconstruct the raw response.
2012-02-28 18:06:21 +01:00
encoding = response.encoding or 'ISO-8859-1'
2012-02-25 13:39:38 +01:00
original = response.raw._original_response
status_line, headers, body = (
2012-03-02 02:36:21 +01:00
'HTTP/{version} {status} {reason}'.format(
2012-02-25 13:39:38 +01:00
version='.'.join(str(original.version)),
status=original.status, reason=original.reason,
),
2012-02-28 18:06:21 +01:00
str(original.msg).decode(encoding),
response.content.decode(encoding) if response.content else u''
2012-02-25 13:39:38 +01:00
)
if do_prettify:
2012-03-02 01:40:40 +01:00
prettify = pretty.PrettyHttp(args.style)
if args.print_headers:
status_line = prettify.headers(status_line)
2012-03-02 01:40:40 +01:00
headers = prettify.headers(headers)
if args.print_body and 'Content-Type' in response.headers:
body = prettify.body(body, response.headers['Content-Type'])
# Output.
if args.print_headers:
2012-03-02 01:40:40 +01:00
stdout.write(status_line)
stdout.write('\n')
2012-03-02 02:36:21 +01:00
stdout.write(headers.encode('utf-8'))
stdout.write('\n\n')
if args.print_body:
2012-03-02 02:36:21 +01:00
stdout.write(body.encode('utf-8'))
stdout.write('\n')
2012-02-25 13:39:38 +01:00
if __name__ == '__main__':
main()