From b34a368f048581720738d2ea0b99504a98b3c64d Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 23 Nov 2023 13:27:38 -0800 Subject: [PATCH] fetch: Cache --- default.nix | 6 +++--- paperdoorknob.py | 3 ++- paperdoorknob_test.py | 8 ++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/default.nix b/default.nix index affb068..6331754 100644 --- a/default.nix +++ b/default.nix @@ -1,11 +1,11 @@ { pkgs ? import { }, lint ? false }: -pkgs.python3Packages.callPackage -({ lib, buildPythonPackage, autopep8, mypy, pylint, requests, types-requests }: +pkgs.python3Packages.callPackage ({ lib, buildPythonPackage, autopep8, mypy + , pylint, requests, requests-cache, types-requests }: buildPythonPackage rec { pname = "paperdoorknob"; version = "0.0.1"; src = lib.cleanSource ./.; - propagatedBuildInputs = [ requests ]; + propagatedBuildInputs = [ requests requests-cache ]; nativeCheckInputs = [ mypy types-requests ] ++ lib.optionals lint [ autopep8 pylint ]; doCheck = true; diff --git a/paperdoorknob.py b/paperdoorknob.py index b4f5e52..c1117c2 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -7,6 +7,7 @@ from argparse import ArgumentParser import requests +import requests_cache def command_line_parser() -> ArgumentParser: @@ -26,7 +27,7 @@ def fetch(url: str, session: requests.Session, timeout: int) -> None: def main() -> None: args = command_line_parser().parse_args() - with requests.session() as session: + with requests_cache.CachedSession() as session: fetch(args.url, session, args.timeout) diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 88897f4..e966a6b 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -9,6 +9,7 @@ import unittest import threading from http.server import BaseHTTPRequestHandler, HTTPServer import requests +import requests_cache import paperdoorknob TIMEOUT = 8 @@ -65,6 +66,13 @@ class TestFetch(unittest.TestCase): paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT) self.assertEqual(self._request_counter, 2) + def testFetchCaching(self) -> None: + with requests_cache.CachedSession() 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, 1) + def testFetchErrors(self) -> None: with requests.session() as s: with self.assertRaises(requests.HTTPError): -- 2.44.1