brain_http.py 11 KB

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