def visit_immutation(self, node, children): context = self._final_context() child_type = children[0].expr_name if child_type == 'preview': if self.tool == 'httpie': command = ['http'] + context.httpie_args(self.method, quote=True) else: assert self.tool == 'curl' command = ['curl'] + context.curl_args(self.method, quote=True) click.echo(' '.join(command)) elif child_type == 'action': output = BytesIO() try: env = Environment(stdout=output, is_windows=False) httpie_main(context.httpie_args(self.method), env=env) content = output.getvalue() finally: output.close() # XXX: Work around a bug of click.echo_via_pager(). When you pass # a bytestring to echo_via_pager(), it converts the bytestring with # str(b'abc'), which makes it "b'abc'". if six.PY2: content = unicode(content, 'utf-8') # noqa else: content = str(content, 'utf-8') click.echo_via_pager(content) return node
def visit_immutation(self, node, children): context = self._final_context() child_type = children[0].expr_name if child_type == 'preview': if self.tool == 'httpie': command = ['http'] + context.httpie_args(self.method, quote=True) else: assert self.tool == 'curl' command = ['curl'] + context.curl_args(self.method, quote=True) click.echo(' '.join(command)) elif child_type == 'action': output = BytesIO() try: env = Environment(stdout=output, is_windows=False) httpie_main(context.httpie_args(self.method), env=env) content = output.getvalue() finally: output.close() # XXX: Work around a bug of click.echo_via_pager(). When you pass # a bytestring to echo_via_pager(), it converts the bytestring with # str(b'abc'), which makes it "b'abc'". if six.PY2: content = unicode(content, 'utf-8') # noqa else: content = str(content, 'utf-8') click.echo_via_pager(content) return node
def visit_immutation(self, node, children): context = self._final_context() child_type = children[0].expr_name if child_type == 'preview': if self.tool == 'httpie': command = ['http'] + context.httpie_args(self.method, quote=True) else: assert self.tool == 'curl' command = ['curl'] + context.curl_args(self.method, quote=True) click.echo(' '.join(command)) elif child_type == 'action': output = BytesIO() try: env = Environment(stdout=output, is_windows=False) # XXX: httpie_main() doesn't provide an API for us to get the # HTTP response object, so we use this super dirty hack - # sys.settrace() to intercept get_response() that is called in # httpie_main() internally. The HTTP response intercepted is # assigned to self.last_response, which may be useful for # self.listener. sys.settrace(self._trace_get_response) try: httpie_main(context.httpie_args(self.method), env=env) finally: sys.settrace(None) content = output.getvalue() finally: output.close() # XXX: Work around a bug of click.echo_via_pager(). When you pass # a bytestring to echo_via_pager(), it converts the bytestring with # str(b'abc'), which makes it "b'abc'". if six.PY2: content = unicode(content, 'utf-8') # noqa else: content = str(content, 'utf-8') click.echo_via_pager(content) if self.last_response: self.listener.response_returned(self.context, self.last_response) return node
def _call_httpie_main(self): context = self._final_context() args = extract_args_for_httpie_main(context, self.method) env = Environment(stdout=self.output, stdin=sys.stdin, is_windows=False) env.stdout_isatty = self.output.isatty() env.stdin_isatty = sys.stdin.isatty() # XXX: httpie_main() doesn't provide an API for us to get the # HTTP response object, so we use this super dirty hack - # sys.settrace() to intercept get_response() that is called in # httpie_main() internally. The HTTP response intercepted is # assigned to self.last_response, which self.listener may be # interested in. sys.settrace(self._trace_get_response) try: httpie_main(args, env=env) finally: sys.settrace(None)
def _call_httpie_main(self): context = self._final_context() args = extract_args_for_httpie_main(context, self.method) env = Environment(stdout=self.output, stdin=sys.stdin, is_windows=False) env.stdout_isatty = self.output.isatty() env.stdin_isatty = sys.stdin.isatty() # XXX: httpie_main() doesn't provide an API for us to get the # HTTP response object, so we use this super dirty hack - # sys.settrace() to intercept get_response() that is called in # httpie_main() internally. The HTTP response intercepted is # assigned to self.last_response, which self.listener may be # interested in. sys.settrace(self._trace_get_response) try: httpie_main(args, env=env) finally: sys.settrace(None)