No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

network_request.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # Copyright 2019 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Wrapper script which makes a network request.
  15. Basic Usage: network_request.py post
  16. --url <url>
  17. --header <header> (optional, support multiple)
  18. --body <body> (optional)
  19. --timeout <secs> (optional)
  20. --verbose (optional)
  21. """
  22. import argparse
  23. import inspect
  24. import logging
  25. import socket
  26. import sys
  27. # pylint: disable=g-import-not-at-top
  28. # pylint: disable=g-importing-member
  29. try:
  30. from six.moves.http_client import HTTPSConnection
  31. from six.moves.http_client import HTTPConnection
  32. from six.moves.http_client import HTTPException
  33. except ImportError:
  34. from http.client import HTTPSConnection
  35. from http.client import HTTPConnection
  36. from http.client import HTTPException
  37. try:
  38. from six.moves.urllib.parse import urlparse
  39. except ImportError:
  40. from urllib.parse import urlparse
  41. # pylint: enable=g-import-not-at-top
  42. # pylint: enable=g-importing-member
  43. # Set up logger as soon as possible
  44. formatter = logging.Formatter('[%(levelname)s] %(message)s')
  45. handler = logging.StreamHandler(stream=sys.stdout)
  46. handler.setFormatter(formatter)
  47. handler.setLevel(logging.INFO)
  48. logger = logging.getLogger(__name__)
  49. logger.addHandler(handler)
  50. logger.setLevel(logging.INFO)
  51. # Custom exit codes for known issues.
  52. # System exit codes in python are valid from 0 - 256, so we will map some common
  53. # ones here to understand successes and failures.
  54. # Uses lower ints to not collide w/ HTTP status codes that the script may return
  55. EXIT_CODE_SUCCESS = 0
  56. EXIT_CODE_SYS_ERROR = 1
  57. EXIT_CODE_INVALID_REQUEST_VALUES = 2
  58. EXIT_CODE_GENERIC_HTTPLIB_ERROR = 3
  59. EXIT_CODE_HTTP_TIMEOUT = 4
  60. EXIT_CODE_HTTP_REDIRECT_ERROR = 5
  61. EXIT_CODE_HTTP_NOT_FOUND_ERROR = 6
  62. EXIT_CODE_HTTP_SERVER_ERROR = 7
  63. EXIT_CODE_HTTP_UNKNOWN_ERROR = 8
  64. MAX_EXIT_CODE = 8
  65. # All used http verbs
  66. POST = 'POST'
  67. def unwrap_kwarg_namespace(func):
  68. """Transform a Namespace object from argparse into proper args and kwargs.
  69. For a function that will be delegated to from argparse, inspect all of the
  70. argments and extract them from the Namespace object.
  71. Args:
  72. func: the function that we are wrapping to modify behavior
  73. Returns:
  74. a new function that unwraps all of the arguments in a namespace and then
  75. delegates to the passed function with those args.
  76. """
  77. # When we move to python 3, getfullargspec so that we can tell the
  78. # difference between args and kwargs -- then this could be used for functions
  79. # that have both args and kwargs
  80. if 'getfullargspec' in dir(inspect):
  81. argspec = inspect.getfullargspec(func)
  82. else:
  83. argspec = inspect.getargspec(func) # Python 2 compatibility.
  84. def wrapped(argparse_namespace=None, **kwargs):
  85. """Take a Namespace object and map it to kwargs.
  86. Inspect the argspec of the passed function. Loop over all the args that
  87. are present in the function and try to map them by name to arguments in the
  88. namespace. For keyword arguments, we do not require that they be present
  89. in the Namespace.
  90. Args:
  91. argparse_namespace: an arparse.Namespace object, the result of calling
  92. argparse.ArgumentParser().parse_args()
  93. **kwargs: keyword arguments that may be passed to the original function
  94. Returns:
  95. The return of the wrapped function from the parent.
  96. Raises:
  97. ValueError in the event that an argument is passed to the cli that is not
  98. in the set of named kwargs
  99. """
  100. if not argparse_namespace:
  101. return func(**kwargs)
  102. reserved_namespace_keywords = ['func']
  103. new_kwargs = {}
  104. args = argspec.args or []
  105. for arg_name in args:
  106. passed_value = getattr(argparse_namespace, arg_name, None)
  107. if passed_value is not None:
  108. new_kwargs[arg_name] = passed_value
  109. for namespace_key in vars(argparse_namespace).keys():
  110. # ignore namespace keywords that have been set not passed in via cli
  111. if namespace_key in reserved_namespace_keywords:
  112. continue
  113. # make sure that we haven't passed something we should be processing
  114. if namespace_key not in args:
  115. raise ValueError('CLI argument "{}" does not match any argument in '
  116. 'function {}'.format(namespace_key, func.__name__))
  117. return func(**new_kwargs)
  118. wrapped.__name__ = func.__name__
  119. return wrapped
  120. class NetworkRequest(object):
  121. """A container for an network request object.
  122. This class holds on to all of the attributes necessary for making a
  123. network request via httplib.
  124. """
  125. def __init__(self, url, method, headers, body, timeout):
  126. self.url = url.lower()
  127. self.parsed_url = urlparse(self.url)
  128. self.method = method
  129. self.headers = headers
  130. self.body = body
  131. self.timeout = timeout
  132. self.is_secure_connection = self.is_secure_connection()
  133. def execute_request(self):
  134. """"Execute the request, and get a response.
  135. Returns:
  136. an HttpResponse object from httplib
  137. """
  138. if self.is_secure_connection:
  139. conn = HTTPSConnection(self.get_hostname(), timeout=self.timeout)
  140. else:
  141. conn = HTTPConnection(self.get_hostname(), timeout=self.timeout)
  142. conn.request(self.method, self.url, self.body, self.headers)
  143. response = conn.getresponse()
  144. return response
  145. def get_hostname(self):
  146. """Return the hostname for the url."""
  147. return self.parsed_url.netloc
  148. def is_secure_connection(self):
  149. """Checks for a secure connection of https.
  150. Returns:
  151. True if the scheme is "https"; False if "http"
  152. Raises:
  153. ValueError when the scheme does not match http or https
  154. """
  155. scheme = self.parsed_url.scheme
  156. if scheme == 'http':
  157. return False
  158. elif scheme == 'https':
  159. return True
  160. else:
  161. raise ValueError('The url scheme is not "http" nor "https"'
  162. ': {}'.format(scheme))
  163. def parse_colon_delimited_options(option_args):
  164. """Parses a key value from a string.
  165. Args:
  166. option_args: Key value string delimited by a color, ex: ("key:value")
  167. Returns:
  168. Return an array with the key as the first element and value as the second
  169. Raises:
  170. ValueError: If the key value option is not formatted correctly
  171. """
  172. options = {}
  173. if not option_args:
  174. return options
  175. for single_arg in option_args:
  176. values = single_arg.split(':')
  177. if len(values) != 2:
  178. raise ValueError('An option arg must be a single key/value pair '
  179. 'delimited by a colon - ex: "thing_key:thing_value"')
  180. key = values[0].strip()
  181. value = values[1].strip()
  182. options[key] = value
  183. return options
  184. def make_request(request):
  185. """Makes a synchronous network request and return the HTTP status code.
  186. Args:
  187. request: a well formulated request object
  188. Returns:
  189. The HTTP status code of the network request.
  190. '1' maps to invalid request headers.
  191. """
  192. logger.info('Sending network request -')
  193. logger.info('\tUrl: %s', request.url)
  194. logger.debug('\tMethod: %s', request.method)
  195. logger.debug('\tHeaders: %s', request.headers)
  196. logger.debug('\tBody: %s', request.body)
  197. try:
  198. response = request.execute_request()
  199. except socket.timeout:
  200. logger.exception(
  201. 'Timed out post request to %s in %d seconds for request body: %s',
  202. request.url, request.timeout, request.body)
  203. return EXIT_CODE_HTTP_TIMEOUT
  204. except (HTTPException, socket.error):
  205. logger.exception(
  206. 'Encountered generic exception in posting to %s with request body %s',
  207. request.url, request.body)
  208. return EXIT_CODE_GENERIC_HTTPLIB_ERROR
  209. status = response.status
  210. headers = response.getheaders()
  211. logger.info('Received Network response -')
  212. logger.info('\tStatus code: %d', status)
  213. logger.debug('\tResponse headers: %s', headers)
  214. if status < 200 or status > 299:
  215. logger.error('Request (%s) failed with status code %d\n', request.url,
  216. status)
  217. # If we wanted this script to support get, we need to
  218. # figure out what mechanism we intend for capturing the response
  219. return status
  220. @unwrap_kwarg_namespace
  221. def post(url=None, header=None, body=None, timeout=5, verbose=False):
  222. """Sends a post request.
  223. Args:
  224. url: The url of the request
  225. header: A list of headers for the request
  226. body: The body for the request
  227. timeout: Timeout in seconds for the request
  228. verbose: Should debug logs be displayed
  229. Returns:
  230. Return an array with the key as the first element and value as the second
  231. """
  232. if verbose:
  233. handler.setLevel(logging.DEBUG)
  234. logger.setLevel(logging.DEBUG)
  235. try:
  236. logger.info('Parsing headers: %s', header)
  237. headers = parse_colon_delimited_options(header)
  238. except ValueError:
  239. logging.exception('Could not parse the parameters with "--header": %s',
  240. header)
  241. return EXIT_CODE_INVALID_REQUEST_VALUES
  242. try:
  243. request = NetworkRequest(url, POST, headers, body, float(timeout))
  244. except ValueError:
  245. logger.exception('Invalid request values passed into the script.')
  246. return EXIT_CODE_INVALID_REQUEST_VALUES
  247. status = make_request(request)
  248. # View exit code after running to get the http status code: 'echo $?'
  249. return status
  250. def get_argsparser():
  251. """Returns the argument parser.
  252. Returns:
  253. Argument parser for the script.
  254. """
  255. parser = argparse.ArgumentParser(
  256. description='The script takes in the arguments of a network request. '
  257. 'The network request is sent and the http status code will be'
  258. 'returned as the exit code.')
  259. subparsers = parser.add_subparsers(help='Commands:')
  260. post_parser = subparsers.add_parser(
  261. post.__name__, help='{} help'.format(post.__name__))
  262. post_parser.add_argument(
  263. '--url',
  264. help='Request url. Ex: https://www.google.com/somePath/',
  265. required=True,
  266. dest='url')
  267. post_parser.add_argument(
  268. '--header',
  269. help='Request headers as a space delimited list of key '
  270. 'value pairs. Ex: "key1:value1 key2:value2"',
  271. action='append',
  272. required=False,
  273. dest='header')
  274. post_parser.add_argument(
  275. '--body',
  276. help='The body of the network request',
  277. required=True,
  278. dest='body')
  279. post_parser.add_argument(
  280. '--timeout',
  281. help='The timeout in seconds',
  282. default=10.0,
  283. required=False,
  284. dest='timeout')
  285. post_parser.add_argument(
  286. '--verbose',
  287. help='Should verbose logging be outputted',
  288. action='store_true',
  289. default=False,
  290. required=False,
  291. dest='verbose')
  292. post_parser.set_defaults(func=post)
  293. return parser
  294. def map_http_status_to_exit_code(status_code):
  295. """Map an http status code to the appropriate exit code.
  296. Exit codes in python are valid from 0-256, so we want to map these to
  297. predictable exit codes within range.
  298. Args:
  299. status_code: the input status code that was output from the network call
  300. function
  301. Returns:
  302. One of our valid exit codes declared at the top of the file or a generic
  303. unknown error code
  304. """
  305. if status_code <= MAX_EXIT_CODE:
  306. return status_code
  307. if status_code > 199 and status_code < 300:
  308. return EXIT_CODE_SUCCESS
  309. if status_code == 302:
  310. return EXIT_CODE_HTTP_REDIRECT_ERROR
  311. if status_code == 404:
  312. return EXIT_CODE_HTTP_NOT_FOUND_ERROR
  313. if status_code > 499:
  314. return EXIT_CODE_HTTP_SERVER_ERROR
  315. return EXIT_CODE_HTTP_UNKNOWN_ERROR
  316. def main():
  317. """Main function to run the program.
  318. Parse system arguments and delegate to the appropriate function.
  319. Returns:
  320. A status code - either an http status code or a custom error code
  321. """
  322. parser = get_argsparser()
  323. subparser_action = parser.parse_args()
  324. try:
  325. return subparser_action.func(subparser_action)
  326. except ValueError:
  327. logger.exception('Invalid arguments passed.')
  328. parser.print_help(sys.stderr)
  329. return EXIT_CODE_INVALID_REQUEST_VALUES
  330. return EXIT_CODE_GENERIC_HTTPLIB_ERROR
  331. if __name__ == '__main__':
  332. exit_code = map_http_status_to_exit_code(main())
  333. sys.exit(exit_code)