1# -*- coding: utf-8 -*-
2
3r"""
4The ``codes`` object defines a mapping from common names for HTTP statuses
5to their numerical codes, accessible either as attributes or as dictionary
6items.
7
8Example::
9
10    >>> import requests
11    >>> requests.codes['temporary_redirect']
12    307
13    >>> requests.codes.teapot
14    418
15    >>> requests.codes['\o/']
16    200
17
18Some codes have multiple names, and both upper- and lower-case versions of
19the names are allowed. For example, ``codes.ok``, ``codes.OK``, and
20``codes.okay`` all correspond to the HTTP status code 200.
21"""
22
23from .structures import LookupDict
24
25_codes = {
26
27    # Informational.
28    100: ('continue',),
29    101: ('switching_protocols',),
30    102: ('processing',),
31    103: ('checkpoint',),
32    122: ('uri_too_long', 'request_uri_too_long'),
33    200: ('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\o/', '✓'),
34    201: ('created',),
35    202: ('accepted',),
36    203: ('non_authoritative_info', 'non_authoritative_information'),
37    204: ('no_content',),
38    205: ('reset_content', 'reset'),
39    206: ('partial_content', 'partial'),
40    207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'),
41    208: ('already_reported',),
42    226: ('im_used',),
43
44    # Redirection.
45    300: ('multiple_choices',),
46    301: ('moved_permanently', 'moved', '\\o-'),
47    302: ('found',),
48    303: ('see_other', 'other'),
49    304: ('not_modified',),
50    305: ('use_proxy',),
51    306: ('switch_proxy',),
52    307: ('temporary_redirect', 'temporary_moved', 'temporary'),
53    308: ('permanent_redirect',
54          'resume_incomplete', 'resume',),  # These 2 to be removed in 3.0
55
56    # Client Error.
57    400: ('bad_request', 'bad'),
58    401: ('unauthorized',),
59    402: ('payment_required', 'payment'),
60    403: ('forbidden',),
61    404: ('not_found', '-o-'),
62    405: ('method_not_allowed', 'not_allowed'),
63    406: ('not_acceptable',),
64    407: ('proxy_authentication_required', 'proxy_auth', 'proxy_authentication'),
65    408: ('request_timeout', 'timeout'),
66    409: ('conflict',),
67    410: ('gone',),
68    411: ('length_required',),
69    412: ('precondition_failed', 'precondition'),
70    413: ('request_entity_too_large',),
71    414: ('request_uri_too_large',),
72    415: ('unsupported_media_type', 'unsupported_media', 'media_type'),
73    416: ('requested_range_not_satisfiable', 'requested_range', 'range_not_satisfiable'),
74    417: ('expectation_failed',),
75    418: ('im_a_teapot', 'teapot', 'i_am_a_teapot'),
76    421: ('misdirected_request',),
77    422: ('unprocessable_entity', 'unprocessable'),
78    423: ('locked',),
79    424: ('failed_dependency', 'dependency'),
80    425: ('unordered_collection', 'unordered'),
81    426: ('upgrade_required', 'upgrade'),
82    428: ('precondition_required', 'precondition'),
83    429: ('too_many_requests', 'too_many'),
84    431: ('header_fields_too_large', 'fields_too_large'),
85    444: ('no_response', 'none'),
86    449: ('retry_with', 'retry'),
87    450: ('blocked_by_windows_parental_controls', 'parental_controls'),
88    451: ('unavailable_for_legal_reasons', 'legal_reasons'),
89    499: ('client_closed_request',),
90
91    # Server Error.
92    500: ('internal_server_error', 'server_error', '/o\\', '✗'),
93    501: ('not_implemented',),
94    502: ('bad_gateway',),
95    503: ('service_unavailable', 'unavailable'),
96    504: ('gateway_timeout',),
97    505: ('http_version_not_supported', 'http_version'),
98    506: ('variant_also_negotiates',),
99    507: ('insufficient_storage',),
100    509: ('bandwidth_limit_exceeded', 'bandwidth'),
101    510: ('not_extended',),
102    511: ('network_authentication_required', 'network_auth', 'network_authentication'),
103}
104
105codes = LookupDict(name='status_codes')
106
107def _init():
108    for code, titles in _codes.items():
109        for title in titles:
110            setattr(codes, title, code)
111            if not title.startswith(('\\', '/')):
112                setattr(codes, title.upper(), code)
113
114    def doc(code):
115        names = ', '.join('``%s``' % n for n in _codes[code])
116        return '* %d: %s' % (code, names)
117
118    global __doc__
119    __doc__ = (__doc__ + '\n' +
120               '\n'.join(doc(code) for code in sorted(_codes))
121               if __doc__ is not None else None)
122
123_init()
124