From 8e04a24b90475608d65ad13ce66d59a41ed8b457 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Thu, 29 Aug 2019 13:08:02 +0200 Subject: [PATCH] Reintroduce `$ https` command alias with `https://` as default scheme Close #608 --- .travis.yml | 3 --- CHANGELOG.rst | 1 + README.rst | 26 +++++++++++++++++++++----- httpie/core.py | 9 +++++++-- httpie/input.py | 14 +++++++++----- setup.py | 6 +----- tests/test_cli.py | 6 +++++- tests/test_errors.py | 4 ++-- tests/utils.py | 7 ++++--- 9 files changed, 50 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index cd0525d0..3457e405 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,6 @@ env: global: - NEWEST_PYTHON=3.7 python: - # Python 3.4 fails installing packages - # - # - 3.4 - 3.5 - 3.6 # - 3.7 # is done in the matrix below as described in travis-ci/travis-ci#9069 diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0777e3ae..00a2b08a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ This project adheres to `Semantic Versioning `_. * Removed Python’s default limit of 100 response headers. * Added ``--max-headers`` to allow setting the max header limit. * Added ``--compress``. +* Added ``https`` alias command with ``https://`` as the default scheme. `1.0.3`_ (2019-08-26) diff --git a/README.rst b/README.rst index 15972475..0cff7bb6 100644 --- a/README.rst +++ b/README.rst @@ -126,7 +126,7 @@ and always provides the latest version) is to use `pip`_: Python version -------------- -Starting with version 2.0.0 (currently under development) Python 3.x is required. +Starting with version 2.0.0 (currently under development) Python 3.5+ is required. Unstable version @@ -351,17 +351,33 @@ If the port is omitted, then port 80 is assumed. Host: localhost -Custom default scheme +Other default schemes --------------------- -You can use the ``--default-scheme `` option to create -shortcuts for other protocols than HTTP: +When HTTPie is invoked as ``https`` then the default scheme is ``https://`` +(``$ https example.org`` will make a request to ``https://example.org``). + +You can also use the ``--default-scheme `` option to create +shortcuts for other protocols than HTTP (possibly supported via plugins). +Example for the `httpie-unixsocket `_ plugin: .. code-block:: bash - $ alias https='http --default-scheme=https' + # Before + $ http http+unix://%2Fvar%2Frun%2Fdocker.sock/info +.. code-block:: bash + + # Create an alias + $ alias http-unix='http --default-scheme="http+unix"' + + +.. code-block:: bash + + # Now the scheme can be omitted + $ http-unix %2Fvar%2Frun%2Fdocker.sock/info + Request items ============= diff --git a/httpie/core.py b/httpie/core.py index fb3d9662..f39635c4 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -168,7 +168,7 @@ def program(args, env, log_error): args.output_file.close() -def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): +def main(args=sys.argv, env=Environment(), custom_log_error=None): """ The main function. @@ -179,6 +179,7 @@ def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): """ args = decode_args(args, env.stdin_encoding) + program_name, *args = args plugin_manager.load_installed_plugins() def log_error(msg, *args, **kwargs): @@ -206,7 +207,11 @@ def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): exit_status = ExitStatus.SUCCESS try: - parsed_args = parser.parse_args(args=args, env=env) + parsed_args = parser.parse_args( + args=args, + program_name=program_name, + env=env, + ) except KeyboardInterrupt: env.stderr.write('\n') if include_traceback: diff --git a/httpie/input.py b/httpie/input.py index 53b9924a..2b179daf 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -132,13 +132,14 @@ class HTTPieArgumentParser(ArgumentParser): def __init__(self, *args, **kwargs): kwargs['add_help'] = False super(HTTPieArgumentParser, self).__init__(*args, **kwargs) + self.env = None + self.args = None # noinspection PyMethodOverriding - def parse_args(self, env, args=None, namespace=None): - + def parse_args(self, env, program_name='http', args=None, namespace=None): self.env = env - self.args, no_options = super(HTTPieArgumentParser, self)\ - .parse_known_args(args, namespace) + self.args, no_options = super( + HTTPieArgumentParser, self).parse_known_args(args, namespace) if self.args.debug: self.args.traceback = True @@ -154,7 +155,10 @@ class HTTPieArgumentParser(ArgumentParser): if not self.args.ignore_stdin and not env.stdin_isatty: self._body_from_file(self.env.stdin) if not URL_SCHEME_RE.match(self.args.url): - scheme = self.args.default_scheme + "://" + if os.path.basename(program_name) == 'https': + scheme = 'https://' + else: + scheme = self.args.default_scheme + "://" # See if we're using curl style shorthand for localhost (:3000/foo) shorthand = re.match(r'^:(?!:)(\d*)(/?.*)$', self.args.url) diff --git a/setup.py b/setup.py index 0914da1f..eb4649ad 100644 --- a/setup.py +++ b/setup.py @@ -82,6 +82,7 @@ setup( entry_points={ 'console_scripts': [ 'http = httpie.__main__:main', + 'https = httpie.__main__:main', ], }, extras_require=extras_require, @@ -91,11 +92,6 @@ setup( classifiers=[ 'Development Status :: 5 - Production/Stable', 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.1', - 'Programming Language :: Python :: 3.2', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', diff --git a/tests/test_cli.py b/tests/test_cli.py index 5daa3bb7..d4606f52 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -342,6 +342,10 @@ class TestSchemes: with pytest.raises(InvalidSchema): http('bah', '--default=scheme=foo+bar-BAZ.123') - def test_default_scheme(self, httpbin_secure): + def test_default_scheme_option(self, httpbin_secure): url = '{0}:{1}'.format(httpbin_secure.host, httpbin_secure.port) assert HTTP_OK in http(url, '--default-scheme=https') + + def test_scheme_when_invoked_as_https(self, httpbin_secure): + url = '{0}:{1}'.format(httpbin_secure.host, httpbin_secure.port) + assert HTTP_OK in http(url, program_name='https') diff --git a/tests/test_errors.py b/tests/test_errors.py index 56e61c39..596e2f5e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -20,7 +20,7 @@ def test_error(get_response): exc = ConnectionError('Connection aborted') exc.request = Request(method='GET', url='http://www.google.com') get_response.side_effect = exc - ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error) + ret = main(['http', '--ignore-stdin', 'www.google.com'], custom_log_error=error) assert ret == ExitStatus.ERROR assert error_msg == ( 'ConnectionError: ' @@ -34,7 +34,7 @@ def test_error_traceback(get_response): exc.request = Request(method='GET', url='http://www.google.com') get_response.side_effect = exc with raises(ConnectionError): - main(['--ignore-stdin', '--traceback', 'www.google.com']) + main(['http', '--ignore-stdin', '--traceback', 'www.google.com']) def test_max_headers_limit(httpbin_both): diff --git a/tests/utils.py b/tests/utils.py index 19939ca3..aa16f906 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -140,7 +140,7 @@ class ExitStatusError(Exception): pass -def http(*args, **kwargs): +def http(*args, program_name='http', **kwargs): # noinspection PyUnresolvedReferences """ Run HTTPie and capture stderr/out and exit status. @@ -197,7 +197,8 @@ def http(*args, **kwargs): add_to_args.append('--traceback') if not any('--timeout' in arg for arg in args_with_config_defaults): add_to_args.append('--timeout=3') - args = add_to_args + args + + complete_args = [program_name, *add_to_args, *args] def dump_stderr(): stderr.seek(0) @@ -205,7 +206,7 @@ def http(*args, **kwargs): try: try: - exit_status = main(args=args, **kwargs) + exit_status = main(args=complete_args, **kwargs) if '--download' in args: # Let the progress reporter thread finish. time.sleep(.5)