]> git.scottworley.com Git - paperdoorknob/commitdiff
fetch: Cache
authorScott Worley <scottworley@scottworley.com>
Thu, 23 Nov 2023 21:27:38 +0000 (13:27 -0800)
committerScott Worley <scottworley@scottworley.com>
Wed, 20 Dec 2023 01:37:10 +0000 (17:37 -0800)
default.nix
paperdoorknob.py
paperdoorknob_test.py

index affb0683816190d9f9cfcd52f405a048db0c27d2..6331754cffa9704802efea37409ece886c61ba80 100644 (file)
@@ -1,11 +1,11 @@
 { pkgs ? import <nixpkgs> { }, lint ? false }:
 { pkgs ? import <nixpkgs> { }, 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 ./.;
   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;
     nativeCheckInputs = [ mypy types-requests ]
       ++ lib.optionals lint [ autopep8 pylint ];
     doCheck = true;
index b4f5e52417ba1f935bfb8b128f0b7df2da6483e7..c1117c2680d3f2eb6788542116602606423fb6df 100644 (file)
@@ -7,6 +7,7 @@
 
 from argparse import ArgumentParser
 import requests
 
 from argparse import ArgumentParser
 import requests
+import requests_cache
 
 
 def command_line_parser() -> ArgumentParser:
 
 
 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()
 
 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)
 
 
         fetch(args.url, session, args.timeout)
 
 
index 88897f4cd64937639ff723a5ae3432ab27caedbe..e966a6b5b0385789b593566531e3ead27e1a49fd 100644 (file)
@@ -9,6 +9,7 @@ import unittest
 import threading
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import requests
 import threading
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import requests
+import requests_cache
 import paperdoorknob
 
 TIMEOUT = 8
 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)
 
             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):
     def testFetchErrors(self) -> None:
         with requests.session() as s:
             with self.assertRaises(requests.HTTPError):