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 do_output_response = (cliparse.OUT_RESP_HEADERS in args.output_options
or cliparse.OUT_RESP_BODY 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 = [] output = []
if do_output_request: if do_output_request:
output.append(httpmessage.format( output.append(httpmessage.format(
message=httpmessage.from_request(response.request), message=httpmessage.from_request(response.request),
pretty=do_prettify, prettifier=prettifier,
formatter=formatter,
with_headers=cliparse.OUT_REQ_HEADERS in args.output_options, with_headers=cliparse.OUT_REQ_HEADERS in args.output_options,
with_body=cliparse.OUT_REQ_BODY 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: if do_output_response:
output.append(httpmessage.format( output.append(httpmessage.format(
message=httpmessage.from_response(response), message=httpmessage.from_response(response),
pretty=do_prettify, prettifier=prettifier,
formatter=formatter,
with_headers=cliparse.OUT_RESP_HEADERS in args.output_options, with_headers=cliparse.OUT_RESP_HEADERS in args.output_options,
with_body=cliparse.OUT_RESP_BODY 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 from requests.compat import urlparse
@ -48,26 +44,24 @@ def from_response(response):
content_type=response_headers.get('Content-Type')) 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): with_headers=True, with_body=True):
"""Return a `unicode` representation of `message`. """ """Return a `unicode` representation of `message`. """
pretty = prettifier is not None
bits = [] bits = []
if with_headers: if with_headers:
bits.append(message.line) bits.append(message.line)
bits.append(message.headers) bits.append(message.headers)
if pretty:
bits = [prettifier.headers('\n'.join(bits))]
if with_body and message.body: if with_body and message.body:
bits.append('\n') bits.append('\n')
if with_body and message.body: if with_body and message.body:
if (pretty and message.content_type and if pretty and message.content_type:
message.content_type.split(';')[0] == 'application/json'): bits.append(prettifier.body(message.body, message.content_type))
# Indent and sort the JSON data.
bits.append(json.dumps(json.loads(message.body),
sort_keys=True, indent=4))
else: else:
bits.append(message.body) 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 import pygments
from pygments import token
from pygments.util import ClassNotFound 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.lexers import get_lexer_for_mimetype, HttpLexer
from pygments.formatters.terminal import TerminalFormatter
from pygments.formatters.terminal256 import Terminal256Formatter 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 from . import solarized
@ -20,11 +19,31 @@ FORMATTER = (Terminal256Formatter
else TerminalFormatter) else TerminalFormatter)
def formatter(style_name): class PrettyHttp(object):
if style_name == 'solarized':
style = solarized.SolarizedStyle
else: def __init__(self, style_name):
style = get_style_by_name(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)