1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5"""Astroid brain hints for some of the `http` module."""
6
7import textwrap
8
9from astroid import nodes
10from astroid.brain.helpers import register_module_extender
11from astroid.builder import AstroidBuilder
12from astroid.manager import AstroidManager
13
14
15def _http_transform() -> nodes.Module:
16 code = textwrap.dedent("""
17 from enum import IntEnum, StrEnum
18 from collections import namedtuple
19 _HTTPStatus = namedtuple('_HTTPStatus', 'value phrase description')
20
21 class HTTPMethod(StrEnum):
22 GET = "GET"
23 POST = "POST"
24 PUT = "PUT"
25 DELETE = "DELETE"
26 HEAD = "HEAD"
27 OPTIONS = "OPTIONS"
28 PATCH = "PATCH"
29 TRACE = "TRACE"
30 CONNECT = "CONNECT"
31
32 class HTTPStatus(IntEnum):
33
34 @property
35 def phrase(self):
36 return ""
37 @property
38 def value(self):
39 return 0
40 @property
41 def description(self):
42 return ""
43
44 # informational
45 CONTINUE = _HTTPStatus(100, 'Continue', 'Request received, please continue')
46 SWITCHING_PROTOCOLS = _HTTPStatus(101, 'Switching Protocols',
47 'Switching to new protocol; obey Upgrade header')
48 PROCESSING = _HTTPStatus(102, 'Processing', '')
49 EARLY_HINTS = _HTTPStatus(103, 'Early Hints')
50 OK = _HTTPStatus(200, 'OK', 'Request fulfilled, document follows')
51 CREATED = _HTTPStatus(201, 'Created', 'Document created, URL follows')
52 ACCEPTED = _HTTPStatus(202, 'Accepted',
53 'Request accepted, processing continues off-line')
54 NON_AUTHORITATIVE_INFORMATION = _HTTPStatus(203,
55 'Non-Authoritative Information', 'Request fulfilled from cache')
56 NO_CONTENT = _HTTPStatus(204, 'No Content', 'Request fulfilled, nothing follows')
57 RESET_CONTENT =_HTTPStatus(205, 'Reset Content', 'Clear input form for further input')
58 PARTIAL_CONTENT = _HTTPStatus(206, 'Partial Content', 'Partial content follows')
59 MULTI_STATUS = _HTTPStatus(207, 'Multi-Status', '')
60 ALREADY_REPORTED = _HTTPStatus(208, 'Already Reported', '')
61 IM_USED = _HTTPStatus(226, 'IM Used', '')
62 MULTIPLE_CHOICES = _HTTPStatus(300, 'Multiple Choices',
63 'Object has several resources -- see URI list')
64 MOVED_PERMANENTLY = _HTTPStatus(301, 'Moved Permanently',
65 'Object moved permanently -- see URI list')
66 FOUND = _HTTPStatus(302, 'Found', 'Object moved temporarily -- see URI list')
67 SEE_OTHER = _HTTPStatus(303, 'See Other', 'Object moved -- see Method and URL list')
68 NOT_MODIFIED = _HTTPStatus(304, 'Not Modified',
69 'Document has not changed since given time')
70 USE_PROXY = _HTTPStatus(305, 'Use Proxy',
71 'You must use proxy specified in Location to access this resource')
72 TEMPORARY_REDIRECT = _HTTPStatus(307, 'Temporary Redirect',
73 'Object moved temporarily -- see URI list')
74 PERMANENT_REDIRECT = _HTTPStatus(308, 'Permanent Redirect',
75 'Object moved permanently -- see URI list')
76 BAD_REQUEST = _HTTPStatus(400, 'Bad Request',
77 'Bad request syntax or unsupported method')
78 UNAUTHORIZED = _HTTPStatus(401, 'Unauthorized',
79 'No permission -- see authorization schemes')
80 PAYMENT_REQUIRED = _HTTPStatus(402, 'Payment Required',
81 'No payment -- see charging schemes')
82 FORBIDDEN = _HTTPStatus(403, 'Forbidden',
83 'Request forbidden -- authorization will not help')
84 NOT_FOUND = _HTTPStatus(404, 'Not Found',
85 'Nothing matches the given URI')
86 METHOD_NOT_ALLOWED = _HTTPStatus(405, 'Method Not Allowed',
87 'Specified method is invalid for this resource')
88 NOT_ACCEPTABLE = _HTTPStatus(406, 'Not Acceptable',
89 'URI not available in preferred format')
90 PROXY_AUTHENTICATION_REQUIRED = _HTTPStatus(407,
91 'Proxy Authentication Required',
92 'You must authenticate with this proxy before proceeding')
93 REQUEST_TIMEOUT = _HTTPStatus(408, 'Request Timeout',
94 'Request timed out; try again later')
95 CONFLICT = _HTTPStatus(409, 'Conflict', 'Request conflict')
96 GONE = _HTTPStatus(410, 'Gone',
97 'URI no longer exists and has been permanently removed')
98 LENGTH_REQUIRED = _HTTPStatus(411, 'Length Required',
99 'Client must specify Content-Length')
100 PRECONDITION_FAILED = _HTTPStatus(412, 'Precondition Failed',
101 'Precondition in headers is false')
102 CONTENT_TOO_LARGE = _HTTPStatus(413, 'Content Too Large',
103 'Content is too large')
104 REQUEST_ENTITY_TOO_LARGE = CONTENT_TOO_LARGE
105 URI_TOO_LONG = _HTTPStatus(414, 'URI Too Long', 'URI is too long')
106 REQUEST_URI_TOO_LONG = URI_TOO_LONG
107 UNSUPPORTED_MEDIA_TYPE = _HTTPStatus(415, 'Unsupported Media Type',
108 'Entity body in unsupported format')
109 RANGE_NOT_SATISFIABLE = (416, 'Range Not Satisfiable',
110 'Cannot satisfy request range')
111 REQUESTED_RANGE_NOT_SATISFIABLE = RANGE_NOT_SATISFIABLE
112 EXPECTATION_FAILED = _HTTPStatus(417, 'Expectation Failed',
113 'Expect condition could not be satisfied')
114 IM_A_TEAPOT = _HTTPStatus(418, 'I\\\'m a Teapot',
115 'Server refuses to brew coffee because it is a teapot.')
116 MISDIRECTED_REQUEST = _HTTPStatus(421, 'Misdirected Request',
117 'Server is not able to produce a response')
118 UNPROCESSABLE_CONTENT = _HTTPStatus(422, 'Unprocessable Content')
119 UNPROCESSABLE_ENTITY = UNPROCESSABLE_CONTENT
120 LOCKED = _HTTPStatus(423, 'Locked')
121 FAILED_DEPENDENCY = _HTTPStatus(424, 'Failed Dependency')
122 TOO_EARLY = _HTTPStatus(425, 'Too Early')
123 UPGRADE_REQUIRED = _HTTPStatus(426, 'Upgrade Required')
124 PRECONDITION_REQUIRED = _HTTPStatus(428, 'Precondition Required',
125 'The origin server requires the request to be conditional')
126 TOO_MANY_REQUESTS = _HTTPStatus(429, 'Too Many Requests',
127 'The user has sent too many requests in '
128 'a given amount of time ("rate limiting")')
129 REQUEST_HEADER_FIELDS_TOO_LARGE = _HTTPStatus(431,
130 'Request Header Fields Too Large',
131 'The server is unwilling to process the request because its header '
132 'fields are too large')
133 UNAVAILABLE_FOR_LEGAL_REASONS = _HTTPStatus(451,
134 'Unavailable For Legal Reasons',
135 'The server is denying access to the '
136 'resource as a consequence of a legal demand')
137 INTERNAL_SERVER_ERROR = _HTTPStatus(500, 'Internal Server Error',
138 'Server got itself in trouble')
139 NOT_IMPLEMENTED = _HTTPStatus(501, 'Not Implemented',
140 'Server does not support this operation')
141 BAD_GATEWAY = _HTTPStatus(502, 'Bad Gateway',
142 'Invalid responses from another server/proxy')
143 SERVICE_UNAVAILABLE = _HTTPStatus(503, 'Service Unavailable',
144 'The server cannot process the request due to a high load')
145 GATEWAY_TIMEOUT = _HTTPStatus(504, 'Gateway Timeout',
146 'The gateway server did not receive a timely response')
147 HTTP_VERSION_NOT_SUPPORTED = _HTTPStatus(505, 'HTTP Version Not Supported',
148 'Cannot fulfill request')
149 VARIANT_ALSO_NEGOTIATES = _HTTPStatus(506, 'Variant Also Negotiates')
150 INSUFFICIENT_STORAGE = _HTTPStatus(507, 'Insufficient Storage')
151 LOOP_DETECTED = _HTTPStatus(508, 'Loop Detected')
152 NOT_EXTENDED = _HTTPStatus(510, 'Not Extended')
153 NETWORK_AUTHENTICATION_REQUIRED = _HTTPStatus(511,
154 'Network Authentication Required',
155 'The client needs to authenticate to gain network access')
156 """)
157 return AstroidBuilder(AstroidManager()).string_build(code)
158
159
160def _http_client_transform() -> nodes.Module:
161 return AstroidBuilder(AstroidManager()).string_build(textwrap.dedent("""
162 from http import HTTPStatus
163
164 CONTINUE = HTTPStatus.CONTINUE
165 SWITCHING_PROTOCOLS = HTTPStatus.SWITCHING_PROTOCOLS
166 PROCESSING = HTTPStatus.PROCESSING
167 EARLY_HINTS = HTTPStatus.EARLY_HINTS
168 OK = HTTPStatus.OK
169 CREATED = HTTPStatus.CREATED
170 ACCEPTED = HTTPStatus.ACCEPTED
171 NON_AUTHORITATIVE_INFORMATION = HTTPStatus.NON_AUTHORITATIVE_INFORMATION
172 NO_CONTENT = HTTPStatus.NO_CONTENT
173 RESET_CONTENT = HTTPStatus.RESET_CONTENT
174 PARTIAL_CONTENT = HTTPStatus.PARTIAL_CONTENT
175 MULTI_STATUS = HTTPStatus.MULTI_STATUS
176 ALREADY_REPORTED = HTTPStatus.ALREADY_REPORTED
177 IM_USED = HTTPStatus.IM_USED
178 MULTIPLE_CHOICES = HTTPStatus.MULTIPLE_CHOICES
179 MOVED_PERMANENTLY = HTTPStatus.MOVED_PERMANENTLY
180 FOUND = HTTPStatus.FOUND
181 SEE_OTHER = HTTPStatus.SEE_OTHER
182 NOT_MODIFIED = HTTPStatus.NOT_MODIFIED
183 USE_PROXY = HTTPStatus.USE_PROXY
184 TEMPORARY_REDIRECT = HTTPStatus.TEMPORARY_REDIRECT
185 PERMANENT_REDIRECT = HTTPStatus.PERMANENT_REDIRECT
186 BAD_REQUEST = HTTPStatus.BAD_REQUEST
187 UNAUTHORIZED = HTTPStatus.UNAUTHORIZED
188 PAYMENT_REQUIRED = HTTPStatus.PAYMENT_REQUIRED
189 FORBIDDEN = HTTPStatus.FORBIDDEN
190 NOT_FOUND = HTTPStatus.NOT_FOUND
191 METHOD_NOT_ALLOWED = HTTPStatus.METHOD_NOT_ALLOWED
192 NOT_ACCEPTABLE = HTTPStatus.NOT_ACCEPTABLE
193 PROXY_AUTHENTICATION_REQUIRED = HTTPStatus.PROXY_AUTHENTICATION_REQUIRED
194 REQUEST_TIMEOUT = HTTPStatus.REQUEST_TIMEOUT
195 CONFLICT = HTTPStatus.CONFLICT
196 GONE = HTTPStatus.GONE
197 LENGTH_REQUIRED = HTTPStatus.LENGTH_REQUIRED
198 PRECONDITION_FAILED = HTTPStatus.PRECONDITION_FAILED
199 CONTENT_TOO_LARGE = HTTPStatus.CONTENT_TOO_LARGE
200 REQUEST_ENTITY_TOO_LARGE = HTTPStatus.CONTENT_TOO_LARGE
201 URI_TOO_LONG = HTTPStatus.URI_TOO_LONG
202 REQUEST_URI_TOO_LONG = HTTPStatus.URI_TOO_LONG
203 UNSUPPORTED_MEDIA_TYPE = HTTPStatus.UNSUPPORTED_MEDIA_TYPE
204 RANGE_NOT_SATISFIABLE = HTTPStatus.RANGE_NOT_SATISFIABLE
205 REQUESTED_RANGE_NOT_SATISFIABLE = HTTPStatus.RANGE_NOT_SATISFIABLE
206 EXPECTATION_FAILED = HTTPStatus.EXPECTATION_FAILED
207 IM_A_TEAPOT = HTTPStatus.IM_A_TEAPOT
208 UNPROCESSABLE_CONTENT = HTTPStatus.UNPROCESSABLE_CONTENT
209 UNPROCESSABLE_ENTITY = HTTPStatus.UNPROCESSABLE_CONTENT
210 LOCKED = HTTPStatus.LOCKED
211 FAILED_DEPENDENCY = HTTPStatus.FAILED_DEPENDENCY
212 TOO_EARLY = HTTPStatus.TOO_EARLY
213 UPGRADE_REQUIRED = HTTPStatus.UPGRADE_REQUIRED
214 PRECONDITION_REQUIRED = HTTPStatus.PRECONDITION_REQUIRED
215 TOO_MANY_REQUESTS = HTTPStatus.TOO_MANY_REQUESTS
216 REQUEST_HEADER_FIELDS_TOO_LARGE = HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE
217 INTERNAL_SERVER_ERROR = HTTPStatus.INTERNAL_SERVER_ERROR
218 NOT_IMPLEMENTED = HTTPStatus.NOT_IMPLEMENTED
219 BAD_GATEWAY = HTTPStatus.BAD_GATEWAY
220 SERVICE_UNAVAILABLE = HTTPStatus.SERVICE_UNAVAILABLE
221 GATEWAY_TIMEOUT = HTTPStatus.GATEWAY_TIMEOUT
222 HTTP_VERSION_NOT_SUPPORTED = HTTPStatus.HTTP_VERSION_NOT_SUPPORTED
223 VARIANT_ALSO_NEGOTIATES = HTTPStatus.VARIANT_ALSO_NEGOTIATES
224 INSUFFICIENT_STORAGE = HTTPStatus.INSUFFICIENT_STORAGE
225 LOOP_DETECTED = HTTPStatus.LOOP_DETECTED
226 NOT_EXTENDED = HTTPStatus.NOT_EXTENDED
227 NETWORK_AUTHENTICATION_REQUIRED = HTTPStatus.NETWORK_AUTHENTICATION_REQUIRED
228 """))
229
230
231def register(manager: AstroidManager) -> None:
232 register_module_extender(manager, "http", _http_transform)
233 register_module_extender(manager, "http.client", _http_client_transform)