From: Scott Worley Date: Thu, 23 Nov 2023 21:18:27 +0000 (-0800) Subject: fetch: Multiple fetches per session X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/e138a9b49da14f16c1de2c02dd928fd5d16aed52?ds=inline fetch: Multiple fetches per session --- diff --git a/paperdoorknob.py b/paperdoorknob.py index c684739..b4f5e52 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -19,15 +19,15 @@ def command_line_parser() -> ArgumentParser: return parser -def fetch(url: str, timeout: int) -> None: - with requests.session() as s: - with s.get(url, timeout=timeout) as r: - r.raise_for_status() +def fetch(url: str, session: requests.Session, timeout: int) -> None: + with session.get(url, timeout=timeout) as r: + r.raise_for_status() def main() -> None: args = command_line_parser().parse_args() - fetch(args.url, args.timeout) + with requests.session() as session: + fetch(args.url, session, args.timeout) if __name__ == '__main__': diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 400570f..88897f4 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -59,18 +59,20 @@ class TestFetch(unittest.TestCase): self._web_server.server_close() def testFetch(self) -> None: - paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT) - self.assertEqual(self._request_counter, 1) - paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT) - self.assertEqual(self._request_counter, 2) + with requests.session() as s: + paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT) + self.assertEqual(self._request_counter, 1) + paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT) + self.assertEqual(self._request_counter, 2) def testFetchErrors(self) -> None: - with self.assertRaises(requests.HTTPError): - paperdoorknob.fetch( - f"http://localhost:{self._port()}/not_found", TIMEOUT) - with self.assertRaises(requests.HTTPError): - paperdoorknob.fetch( - f"http://localhost:{self._port()}/server_error", TIMEOUT) + with requests.session() as s: + with self.assertRaises(requests.HTTPError): + paperdoorknob.fetch( + f"http://localhost:{self._port()}/not_found", s, TIMEOUT) + with self.assertRaises(requests.HTTPError): + paperdoorknob.fetch( + f"http://localhost:{self._port()}/server_error", s, TIMEOUT) if __name__ == '__main__':