Skip to content

Commit

Permalink
Addresses Issue httpie#243
Browse files Browse the repository at this point in the history
  • Loading branch information
dlynch158 committed Dec 16, 2014
1 parent 0481957 commit c3fe650
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion httpie/cli.py
Expand Up @@ -16,7 +16,7 @@
from httpie.input import (Parser, AuthCredentialsArgType, KeyValueArgType,
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ALL_ITEMS,
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
OUT_RESP_BODY, OUTPUT_OPTIONS,
OUT_RESP_BODY, OUT_RESP_TIME, OUTPUT_OPTIONS,
OUTPUT_OPTIONS_DEFAULT, PRETTY_MAP,
PRETTY_STDOUT_TTY_ONLY, SessionNameValidator,
readable_file_arg)
Expand Down Expand Up @@ -234,6 +234,7 @@ def _split_lines(self, text, width):
'{req_body}' request body
'{res_head}' response headers
'{res_body}' response body
'{res_time}' response time
The default behaviour is '{default}' (i.e., the response headers and body
is printed), if standard output is not redirected. If the output is piped
Expand All @@ -246,6 +247,7 @@ def _split_lines(self, text, width):
req_body=OUT_REQ_BODY,
res_head=OUT_RESP_HEAD,
res_body=OUT_RESP_BODY,
res_time=OUT_RESP_TIME,
default=OUTPUT_OPTIONS_DEFAULT,
)
)
Expand Down
3 changes: 3 additions & 0 deletions httpie/client.py
Expand Up @@ -31,6 +31,9 @@ def get_response(args, config_dir):
read_only=bool(args.session_read_only),
)

with open('/home/daniel/mydebug.log', mode='w', encoding='utf-8') as a_file:
a_file.write("Response elapsed: {}".format(response.elapsed))

return response


Expand Down
4 changes: 3 additions & 1 deletion httpie/input.py
Expand Up @@ -76,12 +76,14 @@
OUT_REQ_BODY = 'B'
OUT_RESP_HEAD = 'h'
OUT_RESP_BODY = 'b'
OUT_RESP_TIME = 't'

OUTPUT_OPTIONS = frozenset([
OUT_REQ_HEAD,
OUT_REQ_BODY,
OUT_RESP_HEAD,
OUT_RESP_BODY
OUT_RESP_BODY,
OUT_RESP_TIME
])

# Pretty
Expand Down
28 changes: 24 additions & 4 deletions httpie/output/streams.py
Expand Up @@ -5,7 +5,8 @@
from httpie.context import Environment
from httpie.models import HTTPRequest, HTTPResponse
from httpie.input import (OUT_REQ_BODY, OUT_REQ_HEAD,
OUT_RESP_HEAD, OUT_RESP_BODY)
OUT_RESP_HEAD, OUT_RESP_BODY,
OUT_RESP_TIME)
from httpie.output.processing import Formatting, Conversion


Expand Down Expand Up @@ -64,6 +65,7 @@ def build_output_stream(args, env, request, response):
req_b = OUT_REQ_BODY in args.output_options
resp_h = OUT_RESP_HEAD in args.output_options
resp_b = OUT_RESP_BODY in args.output_options
resp_t = OUT_RESP_TIME in args.output_options
req = req_h or req_b
resp = resp_h or resp_b

Expand All @@ -86,6 +88,12 @@ def build_output_stream(args, env, request, response):
with_headers=resp_h,
with_body=resp_b))

if resp_t:
output.append(Stream(
msg=HTTPResponse(response),
with_time=resp_t,
elapsed_time=response.elapsed))

if env.stdout_isatty and resp_b:
# Ensure a blank line after the response body.
# For terminal output only.
Expand Down Expand Up @@ -124,18 +132,20 @@ def get_stream_type(env, args):
class BaseStream(object):
"""Base HTTP message output stream class."""

def __init__(self, msg, with_headers=True, with_body=True,
on_body_chunk_downloaded=None):
def __init__(self, msg, with_headers=True, with_body=True, with_time=False,
on_body_chunk_downloaded=None, elapsed_time=0):
"""
:param msg: a :class:`models.HTTPMessage` subclass
:param with_headers: if `True`, headers will be included
:param with_body: if `True`, body will be included
"""
assert with_headers or with_body
assert with_headers or with_body or with_time
self.msg = msg
self.with_headers = with_headers
self.with_body = with_body
self.with_time = with_time
self.elapsed_time = elapsed_time
self.on_body_chunk_downloaded = on_body_chunk_downloaded

def get_headers(self):
Expand Down Expand Up @@ -163,6 +173,16 @@ def __iter__(self):
yield b'\n'
yield e.message

if self.with_time:
hours = self.elapsed_time.days * 24
(minutes, seconds) = divmod(self.elapsed_time.seconds, 60)
milliseconds = self.elapsed_time.microseconds / 1000
yield '{hours}:{minutes}:{seconds}.{milliseconds}'.format(
hours=hours, minutes=minutes,
seconds=seconds, milliseconds=milliseconds).encode('utf8')
yield b'\r\n\r\n'



class RawStream(BaseStream):
"""The message is streamed in chunks with no processing."""
Expand Down

0 comments on commit c3fe650

Please sign in to comment.