1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00

Add httpie.status

This commit is contained in:
Jakub Roztocil 2019-09-16 13:26:18 +02:00
parent 64c81fc2ec
commit 374c371ef1
13 changed files with 55 additions and 49 deletions

View File

@ -2,27 +2,7 @@
HTTPie - a CLI, cURL-like tool for humans.
"""
from enum import Enum
__version__ = '2.0.0-dev'
__author__ = 'Jakub Roztocil'
__licence__ = 'BSD'
class ExitStatus(Enum):
"""Program exit code constants."""
SUCCESS = 0
ERROR = 1
PLUGIN_ERROR = 7
# 128+2 SIGINT <http://www.tldp.org/LDP/abs/html/exitcodes.html>
ERROR_CTRL_C = 130
ERROR_TIMEOUT = 2
ERROR_TOO_MANY_REDIRECTS = 6
# Used only when requested with --check-status:
ERROR_HTTP_3XX = 3
ERROR_HTTP_4XX = 4
ERROR_HTTP_5XX = 5

View File

@ -10,7 +10,7 @@ def main():
from .core import main
exit_status = main()
except KeyboardInterrupt:
from . import ExitStatus
from httpie.status import ExitStatus
exit_status = ExitStatus.ERROR_CTRL_C
sys.exit(exit_status.value)

View File

@ -25,7 +25,7 @@ from httpie.sessions import DEFAULT_SESSIONS_DIR
parser = HTTPieArgumentParser(
prog='http',
description='%s <http://httpie.org>' % __doc__.strip(),
description='%s <https://httpie.org>' % __doc__.strip(),
epilog=dedent("""
For every --OPTION there is also a --no-OPTION that reverts OPTION
to its default value.

View File

@ -7,7 +7,8 @@ import requests
from pygments import __version__ as pygments_version
from requests import __version__ as requests_version
from httpie import ExitStatus, __version__ as httpie_version
from httpie import __version__ as httpie_version
from httpie.status import ExitStatus, http_status_to_exit_status
from httpie.client import collect_messages
from httpie.context import Environment
from httpie.downloads import Downloader
@ -154,7 +155,7 @@ def program(
else:
final_response = message
if args.check_status or downloader:
exit_status = get_exit_status(
exit_status = http_status_to_exit_status(
http_status=message.status_code,
follow=args.follow
)
@ -193,21 +194,6 @@ def program(
args.output_file.close()
def get_exit_status(http_status: int, follow=False) -> ExitStatus:
"""Translate HTTP status code to exit status code."""
if 300 <= http_status <= 399 and not follow:
# Redirect
return ExitStatus.ERROR_HTTP_3XX
elif 400 <= http_status <= 499:
# Client Error
return ExitStatus.ERROR_HTTP_4XX
elif 500 <= http_status <= 599:
# Server Error
return ExitStatus.ERROR_HTTP_5XX
else:
return ExitStatus.SUCCESS
def print_debug_info(env: Environment):
env.stderr.writelines([
'HTTPie %s\n' % httpie_version,

39
httpie/status.py Normal file
View File

@ -0,0 +1,39 @@
from enum import Enum
class ExitStatus(Enum):
"""Program exit code constants."""
SUCCESS = 0
ERROR = 1
PLUGIN_ERROR = 7
# 128+2 SIGINT <http://www.tldp.org/LDP/abs/html/exitcodes.html>
ERROR_CTRL_C = 130
ERROR_TIMEOUT = 2
ERROR_TOO_MANY_REDIRECTS = 6
# Used only when requested with --check-status:
ERROR_HTTP_3XX = 3
ERROR_HTTP_4XX = 4
ERROR_HTTP_5XX = 5
def http_status_to_exit_status(http_status: int, follow=False) -> ExitStatus:
"""
Translate HTTP status code to exit status code.
(Relevant only when invoked with --check-status or --download.)
"""
if 300 <= http_status <= 399 and not follow:
# Redirect
return ExitStatus.ERROR_HTTP_3XX
elif 400 <= http_status <= 499:
# Client Error
return ExitStatus.ERROR_HTTP_4XX
elif 500 <= http_status <= 599:
# Server Error
return ExitStatus.ERROR_HTTP_5XX
else:
return ExitStatus.SUCCESS

View File

@ -10,7 +10,7 @@ from fixtures import (
FILE_CONTENT, FILE_PATH, FILE_PATH_ARG, JSON_FILE_CONTENT,
JSON_FILE_PATH_ARG,
)
from httpie import ExitStatus
from httpie.status import ExitStatus
from httpie.cli import constants
from httpie.cli.definition import parser
from httpie.cli.argtypes import KeyValueArg, KeyValueArgType

View File

@ -3,7 +3,7 @@ from pytest import raises
from requests import Request
from requests.exceptions import ConnectionError
from httpie import ExitStatus
from httpie.status import ExitStatus
from httpie.core import main
from utils import HTTP_OK, http

View File

@ -1,6 +1,6 @@
import mock
from httpie import ExitStatus
from httpie.status import ExitStatus
from utils import MockEnvironment, http, HTTP_OK

View File

@ -1,6 +1,7 @@
"""High-level tests."""
import pytest
import httpie.status
from httpie.cli.exceptions import ParseError
from utils import MockEnvironment, http, HTTP_OK
from fixtures import FILE_PATH, FILE_CONTENT
@ -10,19 +11,19 @@ import httpie
def test_debug():
r = http('--debug')
assert r.exit_status == httpie.ExitStatus.SUCCESS
assert r.exit_status == httpie.status.ExitStatus.SUCCESS
assert 'HTTPie %s' % httpie.__version__ in r.stderr
def test_help():
r = http('--help', tolerate_error_exit_status=True)
assert r.exit_status == httpie.ExitStatus.SUCCESS
assert r.exit_status == httpie.status.ExitStatus.SUCCESS
assert 'https://github.com/jakubroztocil/httpie/issues' in r
def test_version():
r = http('--version', tolerate_error_exit_status=True)
assert r.exit_status == httpie.ExitStatus.SUCCESS
assert r.exit_status == httpie.status.ExitStatus.SUCCESS
# FIXME: py3 has version in stdout, py2 in stderr
assert httpie.__version__ == r.strip()

View File

@ -5,7 +5,7 @@ from urllib.request import urlopen
import pytest
from utils import MockEnvironment, http, HTTP_OK, COLOR, CRLF
from httpie import ExitStatus
from httpie.status import ExitStatus
from httpie.output.formatters.colors import get_lexer

View File

@ -1,7 +1,7 @@
"""High-level tests."""
import pytest
from httpie import ExitStatus
from httpie.status import ExitStatus
from utils import http, HTTP_OK

View File

@ -4,7 +4,7 @@ import pytest
import pytest_httpbin.certs
import requests.exceptions
from httpie import ExitStatus
from httpie.status import ExitStatus
from httpie.cli.constants import SSL_VERSION_ARG_MAPPING
from utils import HTTP_OK, TESTS_ROOT, http

View File

@ -8,7 +8,7 @@ import tempfile
from pathlib import Path
from typing import Optional
from httpie import ExitStatus
from httpie.status import ExitStatus
from httpie.config import Config
from httpie.context import Environment
from httpie.core import main