]> git.scottworley.com Git - paperdoorknob/blobdiff - fetch.py
Use a more URL-looking url in tests
[paperdoorknob] / fetch.py
index 151e2a981eca71b2d07e8b7713a687fd7748de5a..eb904a4f4b0e525756c977d442fb0e4144dc0825 100644 (file)
--- a/fetch.py
+++ b/fetch.py
@@ -8,14 +8,16 @@
 from abc import ABC, abstractmethod
 from contextlib import contextmanager
 from sys import stderr
-from typing import IO, Iterator
+from typing import Callable, Iterator
 
 import requests
 import requests_cache
 
+from version import paperdoorknob_version
 
-_headers = {
-    'User-Agent': 'paperdoorknob/0.0.1 (https://git.scottworley.com/paperdoorknob/)'}
+
+_headers = {'User-Agent': f'paperdoorknob/{paperdoorknob_version} ' +
+            '(https://git.scottworley.com/paperdoorknob/)'}
 
 
 class Fetcher(ABC):
@@ -71,17 +73,16 @@ def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]:
 
 @contextmanager
 def CachingFetcher(
-        cache_path: str,
-        timeout: int,
-        report_stream: IO[str] = stderr) -> Iterator[_CachingFetcher]:
+    cache_path: str,
+    timeout: int,
+    log: Callable[[str], None] = lambda x: print(x, file=stderr),
+) -> Iterator[_CachingFetcher]:
     with requests_cache.CachedSession(cache_path, cache_control=True) as session:
         fetcher = _CachingFetcher(session, timeout)
         yield fetcher
         if fetcher.request_count > 0:
             percent = 100.0 * fetcher.cache_hit_count / fetcher.request_count
-            print(
-                f"Fetch cache hits: {fetcher.cache_hit_count} ({percent:.1f}%)",
-                file=report_stream)
+            log(f"Fetch cache hits: {fetcher.cache_hit_count} ({percent:.1f}%)")
 
 
 class FakeFetcher(Fetcher):