mirror of
https://github.com/httpie/cli.git
synced 2025-06-27 04:51:28 +02:00
Improved --debug.
This commit is contained in:
parent
49e44d9b7e
commit
1fbe7a6121
@ -3,7 +3,7 @@ HTTPie - cURL for humans.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
__author__ = 'Jakub Roztocil'
|
__author__ = 'Jakub Roztocil'
|
||||||
__version__ = '0.2.7'
|
__version__ = '0.2.7dev'
|
||||||
__licence__ = 'BSD'
|
__licence__ = 'BSD'
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,17 +220,23 @@ parser.add_argument(
|
|||||||
''')
|
''')
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--timeout', type=float,
|
'--timeout', type=float, default=30,
|
||||||
help=_('''
|
help=_('''
|
||||||
Float describes the timeout of the request
|
The timeout of the request in seconds. The default value is 30
|
||||||
(Use socket.setdefaulttimeout() as fallback).
|
seconds.
|
||||||
|
''')
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--traceback', action='store_true', default=False,
|
||||||
|
help=_('''
|
||||||
|
Prints exception traceback should one occur.
|
||||||
''')
|
''')
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--debug', action='store_true', default=False,
|
'--debug', action='store_true', default=False,
|
||||||
help=_('''
|
help=_('''
|
||||||
Prints exception traceback should one occur and other
|
Prints exception traceback should one occur, and also other
|
||||||
information useful for debugging HTTPie itself.
|
information useful for debugging HTTPie itself and bug reports.
|
||||||
''')
|
''')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,10 +13,14 @@ Invocation flow:
|
|||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import errno
|
import errno
|
||||||
|
from pprint import pformat
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import requests.auth
|
import requests.auth
|
||||||
from requests.compat import str
|
from requests.compat import str
|
||||||
|
from httpie import __version__ as httpie_version
|
||||||
|
from requests import __version__ as requests_version
|
||||||
|
from pygments import __version__ as pygments_version
|
||||||
|
|
||||||
from .cli import parser
|
from .cli import parser
|
||||||
from .models import Environment
|
from .models import Environment
|
||||||
@ -28,7 +32,7 @@ FORM = 'application/x-www-form-urlencoded; charset=utf-8'
|
|||||||
JSON = 'application/json; charset=utf-8'
|
JSON = 'application/json; charset=utf-8'
|
||||||
|
|
||||||
|
|
||||||
def get_response(args):
|
def get_requests_kwargs(args):
|
||||||
"""Send the request and return a `request.Response`."""
|
"""Send the request and return a `request.Response`."""
|
||||||
|
|
||||||
auto_json = args.data and not args.form
|
auto_json = args.data and not args.form
|
||||||
@ -58,20 +62,25 @@ def get_response(args):
|
|||||||
'digest': requests.auth.HTTPDigestAuth,
|
'digest': requests.auth.HTTPDigestAuth,
|
||||||
}[args.auth_type](args.auth.key, args.auth.value)
|
}[args.auth_type](args.auth.key, args.auth.value)
|
||||||
|
|
||||||
return requests.request(
|
kwargs = {
|
||||||
prefetch=False,
|
'prefetch': False,
|
||||||
method=args.method.lower(),
|
'method': args.method.lower(),
|
||||||
url=args.url,
|
'url': args.url,
|
||||||
headers=args.headers,
|
'headers': args.headers,
|
||||||
data=args.data,
|
'data': args.data,
|
||||||
verify={'yes': True, 'no': False}.get(args.verify, args.verify),
|
'verify': {
|
||||||
timeout=args.timeout,
|
'yes': True,
|
||||||
auth=credentials,
|
'no': False
|
||||||
proxies=dict((p.key, p.value) for p in args.proxy),
|
}.get(args.verify,args.verify),
|
||||||
files=args.files,
|
'timeout': args.timeout,
|
||||||
allow_redirects=args.allow_redirects,
|
'auth': credentials,
|
||||||
params=args.params,
|
'proxies': dict((p.key, p.value) for p in args.proxy),
|
||||||
)
|
'files': args.files,
|
||||||
|
'allow_redirects': args.allow_redirects,
|
||||||
|
'params': args.params
|
||||||
|
}
|
||||||
|
|
||||||
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
def get_exist_status(code, allow_redirects=False):
|
def get_exist_status(code, allow_redirects=False):
|
||||||
@ -101,11 +110,23 @@ def main(args=sys.argv[1:], env=Environment()):
|
|||||||
env.stderr.write('\nhttp: error: %s\n' % msg)
|
env.stderr.write('\nhttp: error: %s\n' % msg)
|
||||||
|
|
||||||
debug = '--debug' in args
|
debug = '--debug' in args
|
||||||
|
traceback = debug or '--traceback' in args
|
||||||
status = EXIT.OK
|
status = EXIT.OK
|
||||||
|
|
||||||
|
if debug:
|
||||||
|
sys.stderr.write('HTTPie version: %s\n' % httpie_version)
|
||||||
|
sys.stderr.write('Requests version: %s\n' % requests_version)
|
||||||
|
sys.stderr.write('Pygments version: %s\n' % pygments_version)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args = parser.parse_args(args=args, env=env)
|
args = parser.parse_args(args=args, env=env)
|
||||||
response = get_response(args)
|
kwargs = get_requests_kwargs(args)
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
|
sys.stderr.write(
|
||||||
|
'\n>>> requests.request(%s)\n\n' % pformat(kwargs))
|
||||||
|
|
||||||
|
response = requests.request(**kwargs)
|
||||||
|
|
||||||
if args.check_status:
|
if args.check_status:
|
||||||
status = get_exist_status(response.status_code,
|
status = get_exist_status(response.status_code,
|
||||||
@ -121,14 +142,14 @@ def main(args=sys.argv[1:], env=Environment()):
|
|||||||
flush=env.stdout_isatty or args.stream)
|
flush=env.stdout_isatty or args.stream)
|
||||||
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
if not debug and e.errno == errno.EPIPE:
|
if not traceback and e.errno == errno.EPIPE:
|
||||||
# Ignore broken pipes unless --debug.
|
# Ignore broken pipes unless --debug.
|
||||||
env.stderr.write('\n')
|
env.stderr.write('\n')
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
if debug:
|
if traceback:
|
||||||
raise
|
raise
|
||||||
env.stderr.write('\n')
|
env.stderr.write('\n')
|
||||||
status = EXIT.ERROR
|
status = EXIT.ERROR
|
||||||
@ -136,7 +157,7 @@ def main(args=sys.argv[1:], env=Environment()):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
# TODO: distinguish between expected and unexpected errors.
|
# TODO: distinguish between expected and unexpected errors.
|
||||||
# network errors vs. bugs, etc.
|
# network errors vs. bugs, etc.
|
||||||
if debug:
|
if traceback:
|
||||||
raise
|
raise
|
||||||
error('%s: %s', type(e).__name__, str(e))
|
error('%s: %s', type(e).__name__, str(e))
|
||||||
status = EXIT.ERROR
|
status = EXIT.ERROR
|
||||||
|
@ -101,6 +101,9 @@ class Parser(argparse.ArgumentParser):
|
|||||||
|
|
||||||
args = super(Parser, self).parse_args(args, namespace)
|
args = super(Parser, self).parse_args(args, namespace)
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
|
args.traceback = True
|
||||||
|
|
||||||
if args.output:
|
if args.output:
|
||||||
env.stdout = args.output
|
env.stdout = args.output
|
||||||
env.stdout_isatty = False
|
env.stdout_isatty = False
|
||||||
@ -171,7 +174,7 @@ class Parser(argparse.ArgumentParser):
|
|||||||
0, KeyValueArgType(*SEP_GROUP_ITEMS).__call__(args.url))
|
0, KeyValueArgType(*SEP_GROUP_ITEMS).__call__(args.url))
|
||||||
|
|
||||||
except argparse.ArgumentTypeError as e:
|
except argparse.ArgumentTypeError as e:
|
||||||
if args.debug:
|
if args.traceback:
|
||||||
raise
|
raise
|
||||||
self.error(e.message)
|
self.error(e.message)
|
||||||
|
|
||||||
@ -201,7 +204,7 @@ class Parser(argparse.ArgumentParser):
|
|||||||
files=args.files,
|
files=args.files,
|
||||||
params=args.params)
|
params=args.params)
|
||||||
except ParseError as e:
|
except ParseError as e:
|
||||||
if args.debug:
|
if args.traceback:
|
||||||
raise
|
raise
|
||||||
self.error(e.message)
|
self.error(e.message)
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ def http(*args, **kwargs):
|
|||||||
try:
|
try:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
exit_status = main(args=['--debug'] + list(args), **kwargs)
|
exit_status = main(args=['--traceback'] + list(args), **kwargs)
|
||||||
except Exception:
|
except Exception:
|
||||||
sys.stderr.write(env.stderr.read())
|
sys.stderr.write(env.stderr.read())
|
||||||
raise
|
raise
|
||||||
|
Loading…
x
Reference in New Issue
Block a user