network_request.py 12 KB

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