use PrettyHttp class; working --headers and --body

This commit is contained in:
Jakob Kramer 2012-04-28 14:13:40 +02:00
parent 4aa86cb438
commit bced559496
3 changed files with 40 additions and 29 deletions

View File

@ -84,14 +84,13 @@ def _get_output(args, stdout_isatty, response):
do_output_response = (cliparse.OUT_RESP_HEADERS in args.output_options
or cliparse.OUT_RESP_BODY in args.output_options)
formatter = pretty.formatter(args.style) if do_prettify else None
prettifier = pretty.PrettyHttp(args.style) if do_prettify else None
output = []
if do_output_request:
output.append(httpmessage.format(
message=httpmessage.from_request(response.request),
pretty=do_prettify,
formatter=formatter,
prettifier=prettifier,
with_headers=cliparse.OUT_REQ_HEADERS in args.output_options,
with_body=cliparse.OUT_REQ_BODY in args.output_options
))
@ -101,8 +100,7 @@ def _get_output(args, stdout_isatty, response):
if do_output_response:
output.append(httpmessage.format(
message=httpmessage.from_response(response),
pretty=do_prettify,
formatter=formatter,
prettifier=prettifier,
with_headers=cliparse.OUT_RESP_HEADERS in args.output_options,
with_body=cliparse.OUT_RESP_BODY in args.output_options
))

View File

@ -1,7 +1,3 @@
import json
from pygments import highlight
from pygments.lexers import HttpLexer
from requests.compat import urlparse
@ -48,26 +44,24 @@ def from_response(response):
content_type=response_headers.get('Content-Type'))
def format(message, formatter=None, pretty=False,
def format(message, prettifier=None,
with_headers=True, with_body=True):
"""Return a `unicode` representation of `message`. """
pretty = prettifier is not None
bits = []
if with_headers:
bits.append(message.line)
bits.append(message.headers)
if pretty:
bits = [prettifier.headers('\n'.join(bits))]
if with_body and message.body:
bits.append('\n')
if with_body and message.body:
if (pretty and message.content_type and
message.content_type.split(';')[0] == 'application/json'):
# Indent and sort the JSON data.
bits.append(json.dumps(json.loads(message.body),
sort_keys=True, indent=4))
if pretty and message.content_type:
bits.append(prettifier.body(message.body, message.content_type))
else:
bits.append(message.body)
bits.append('\n')
result = '\n'.join(bit.strip() for bit in bits)
if pretty:
return highlight(result, HttpLexer(), formatter)
return result
return '\n'.join(bit.strip() for bit in bits)

View File

@ -3,12 +3,11 @@ import json
import pygments
from pygments import token
from pygments.util import ClassNotFound
from pygments.styles import get_style_by_name, STYLE_MAP
from pygments.lexers import get_lexer_for_mimetype, HttpLexer
from pygments.formatters.terminal import TerminalFormatter
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.formatters.terminal import TerminalFormatter
from pygments.styles import get_style_by_name, STYLE_MAP
from . import solarized
@ -20,11 +19,31 @@ FORMATTER = (Terminal256Formatter
else TerminalFormatter)
def formatter(style_name):
if style_name == 'solarized':
style = solarized.SolarizedStyle
class PrettyHttp(object):
else:
style = get_style_by_name(style_name)
def __init__(self, style_name):
if style_name == 'solarized':
style = solarized.SolarizedStyle
else:
style = get_style_by_name(style_name)
self.formatter = FORMATTER(style=style)
return FORMATTER(style=style)
def headers(self, content):
return pygments.highlight(content, HttpLexer(), self.formatter)
def body(self, content, content_type):
content_type = content_type.split(';')[0]
try:
lexer = get_lexer_for_mimetype(content_type)
except ClassNotFound:
return content
if content_type == 'application/json':
try:
# Indent and sort the JSON data.
content = json.dumps(json.loads(content),
sort_keys=True, indent=4)
except:
pass
return pygments.highlight(content, lexer, self.formatter)