brain_http.py 11 KB

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