httpie-cli/httpie/__main__.py

127 lines
4.1 KiB
Python
Raw Normal View History

2012-02-25 13:39:38 +01:00
#!/usr/bin/env python
import sys
import json
2012-04-26 14:48:38 +02:00
2012-03-15 00:11:49 +01:00
import requests
2012-04-26 14:48:38 +02:00
2012-04-25 01:32:53 +02:00
from requests.compat import str
2012-04-26 14:48:38 +02:00
2012-04-25 01:32:53 +02:00
from . import httpmessage
from . import cliparse
2012-03-04 10:48:30 +01:00
from . import cli
from . import pretty
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-04-25 01:32:53 +02:00
def _get_response(parser, args, stdin, stdin_isatty):
2012-03-02 01:40:40 +01:00
if not stdin_isatty:
2012-04-25 01:32:53 +02:00
if args.data:
2012-03-04 10:48:30 +01:00
parser.error('Request body (stdin) and request '
'data (key=value) cannot be mixed.')
2012-04-25 01:32:53 +02:00
args.data = stdin.read()
if args.json or (not args.form and args.data):
# JSON
if not args.files and (
'Content-Type' not in args.headers
and (args.data or args.json)):
args.headers['Content-Type'] = TYPE_JSON
2012-03-02 01:40:40 +01:00
if stdin_isatty:
2012-04-25 01:32:53 +02:00
# Serialize the parsed data.
args.data = json.dumps(args.data)
2012-04-25 02:10:58 +02:00
if 'Accept' not in args.headers:
2012-04-25 01:32:53 +02:00
# Default Accept to JSON as well.
args.headers['Accept'] = 'application/json'
elif not args.files and 'Content-Type' not in args.headers:
# Form
args.headers['Content-Type'] = TYPE_FORM
2012-02-25 13:39:38 +01:00
# Fire the request.
2012-02-26 16:13:12 +01:00
try:
2012-03-22 15:40:03 +01:00
credentials = None
if args.auth:
auth_type = (requests.auth.HTTPDigestAuth
if args.auth_type == 'digest'
else requests.auth.HTTPBasicAuth)
credentials = auth_type(args.auth.key, args.auth.value)
2012-03-22 15:40:03 +01:00
2012-04-25 01:32:53 +02:00
return requests.request(
2012-02-26 16:13:12 +01:00
method=args.method.lower(),
url=args.url if '://' in args.url else 'http://%s' % args.url,
2012-04-25 01:32:53 +02:00
headers=args.headers,
data=args.data,
verify={'yes': True, 'no': False}.get(args.verify, args.verify),
2012-02-26 16:13:12 +01:00
timeout=args.timeout,
2012-03-22 15:40:03 +01:00
auth=credentials,
proxies=dict((p.key, p.value) for p in args.proxy),
2012-04-25 01:32:53 +02:00
files=args.files,
allow_redirects=args.allow_redirects,
2012-02-26 16:13:12 +01:00
)
2012-04-25 01:32:53 +02:00
2012-03-04 10:48:30 +01:00
except (KeyboardInterrupt, SystemExit):
2012-04-25 01:32:53 +02:00
sys.stderr.write('\n')
2012-02-26 16:13:12 +01:00
sys.exit(1)
except Exception as e:
if args.traceback:
raise
2012-04-25 01:32:53 +02:00
sys.stderr.write(str(e.message) + '\n')
2012-02-26 16:13:12 +01:00
sys.exit(1)
2012-02-25 13:39:38 +01:00
2012-04-25 01:32:53 +02:00
def _get_output(args, stdout_isatty, response):
do_prettify = (args.prettify is True or
(args.prettify == cliparse.PRETTIFY_STDOUT_TTY_ONLY
and stdout_isatty))
do_output_request = (cliparse.OUT_REQ_HEADERS in args.output_options
or cliparse.OUT_REQ_BODY in args.output_options)
2012-04-25 01:32:53 +02:00
do_output_response = (cliparse.OUT_RESP_HEADERS in args.output_options
or cliparse.OUT_RESP_BODY in args.output_options)
prettifier = pretty.PrettyHttp(args.style) if do_prettify else None
output = []
2012-04-25 01:32:53 +02:00
if do_output_request:
2012-04-25 01:32:53 +02:00
output.append(httpmessage.format(
message=httpmessage.from_request(response.request),
prettifier=prettifier,
2012-04-25 01:32:53 +02:00
with_headers=cliparse.OUT_REQ_HEADERS in args.output_options,
with_body=cliparse.OUT_REQ_BODY in args.output_options
2012-03-15 00:11:49 +01:00
))
output.append('\n')
if do_output_response:
2012-04-25 01:32:53 +02:00
output.append('\n')
if do_output_response:
2012-04-25 01:32:53 +02:00
output.append(httpmessage.format(
message=httpmessage.from_response(response),
prettifier=prettifier,
2012-04-25 01:32:53 +02:00
with_headers=cliparse.OUT_RESP_HEADERS in args.output_options,
with_body=cliparse.OUT_RESP_BODY in args.output_options
2012-03-15 00:11:49 +01:00
))
2012-04-25 01:32:53 +02:00
output.append('\n')
return ''.join(output)
2012-04-25 01:32:53 +02:00
def main(args=None,
stdin=sys.stdin, stdin_isatty=sys.stdin.isatty(),
stdout=sys.stdout, stdout_isatty=sys.stdout.isatty()):
parser = cli.parser
args = parser.parse_args(args if args is not None else sys.argv[1:])
response = _get_response(parser, args, stdin, stdin_isatty)
output = _get_output(args, stdout_isatty, response)
output_bytes = output.encode('utf8')
f = (stdout.buffer if hasattr(stdout, 'buffer') else stdout)
f.write(output_bytes)
2012-02-25 13:39:38 +01:00
2012-03-04 11:29:55 +01:00
2012-02-25 13:39:38 +01:00
if __name__ == '__main__':
main()