diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c074c40585cda5828554ae2030de72110ccee0e..d8e71779f3d3fcedc427266519e5d8ec94c5fb27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - a hint in the documentation how to filter for (non-)existence of fields (#141) - the coBib command `prompt` to the TUI's command palette (!310) +### Changed +- HTTP requests are now done inside of `requests.Session` instances (!233) + ### Fixed - the `Escape` key during an input prompt triggered after a `Confirm` prompt (e.g. a cancelled `quit`) - the behavior of the `Home` and `End` keybindings in the TUI's search view (!310) diff --git a/src/cobib/importers/zotero.py b/src/cobib/importers/zotero.py index d2bafc8f68ae61afb7e16bace8d3753f42b60aef..ed531ba7b8a1e0129b5cdb83ef477f0d1111681d 100644 --- a/src/cobib/importers/zotero.py +++ b/src/cobib/importers/zotero.py @@ -144,7 +144,8 @@ class ZoteroImporter(Importer): Event.PreZoteroImport.fire(self) - raw_result = requests.get(self.protected_url, headers=self.authentication, timeout=30) + session = requests.Session() + raw_result = session.get(self.protected_url, headers=self.authentication, timeout=30) if raw_result.encoding is None: raw_result.encoding = "utf-8" diff --git a/src/cobib/parsers/arxiv.py b/src/cobib/parsers/arxiv.py index 397bd588af48c7bd1cdbd369c4871dc0ad771a57..d15423c24d7e84dc8698ba0346dabe3725d15c9d 100644 --- a/src/cobib/parsers/arxiv.py +++ b/src/cobib/parsers/arxiv.py @@ -61,7 +61,8 @@ class ArxivParser(Parser): arxiv_id = match.group(1) LOGGER.info("Gathering BibTex data for arXiv ID: %s.", arxiv_id) try: - page = requests.get(ARXIV_URL + arxiv_id, timeout=10) + session = requests.Session() + page = session.get(ARXIV_URL + arxiv_id, timeout=10) if page.encoding is None: page.encoding = "utf-8" except requests.exceptions.RequestException as err: diff --git a/src/cobib/parsers/doi.py b/src/cobib/parsers/doi.py index edfaca0d01c6de624c2b27c31011a78e4f7e7c27..ddbd3194f5eefc886cc8e2a8209679e1e8b232b7 100644 --- a/src/cobib/parsers/doi.py +++ b/src/cobib/parsers/doi.py @@ -69,19 +69,20 @@ class DOIParser(Parser): doi = match.group(1) LOGGER.info("Gathering BibTex data for DOI: %s.", doi) try: - page = requests.get(DOI_URL + doi, headers=DOI_HEADER, timeout=10) + session = requests.Session() + page = session.get(DOI_URL + doi, headers=DOI_HEADER, timeout=10) if page.encoding is None: page.encoding = "utf-8" # this assumes that the doi.org page redirects to the correct journal's landing page redirected_url: str = "" - header = requests.head(DOI_URL + doi, timeout=1).headers + header = session.head(DOI_URL + doi, timeout=1).headers LOGGER.debug("The DOI URL header: '%s'", header) max_iter = 3 while "Location" in header and max_iter: max_iter -= 1 redirected_url = header["Location"] LOGGER.debug("The found URL redirects to: '%s'", redirected_url) - header = requests.head(redirected_url, timeout=1).headers + header = session.head(redirected_url, timeout=1).headers except requests.exceptions.RequestException as err: LOGGER.error("An Exception occurred while trying to query the DOI: %s.", doi) LOGGER.error(err) diff --git a/src/cobib/parsers/isbn.py b/src/cobib/parsers/isbn.py index fb996441b24c6129fe786fcc7d80a1371aedd07d..e0ba75926ca1699a1767a31037f9cb592a35dea8 100644 --- a/src/cobib/parsers/isbn.py +++ b/src/cobib/parsers/isbn.py @@ -64,7 +64,8 @@ class ISBNParser(Parser): LOGGER.info("Gathering BibTex data for ISBN: %s.", isbn) isbn_plain = "".join([i for i in isbn if i.isdigit()]) try: - page = requests.get(ISBN_URL + isbn_plain + "&jscmd=data&format=json", timeout=10) + session = requests.Session() + page = session.get(ISBN_URL + isbn_plain + "&jscmd=data&format=json", timeout=10) if page.encoding is None: page.encoding = "utf-8" except requests.exceptions.RequestException as err: diff --git a/src/cobib/parsers/url.py b/src/cobib/parsers/url.py index cd56ac68bdf18417dfec078f92e48c558f26709c..b7c3d05df0f9183fd4a872fd318a822a7291eecd 100644 --- a/src/cobib/parsers/url.py +++ b/src/cobib/parsers/url.py @@ -63,7 +63,8 @@ class URLParser(Parser): return entries try: - page = requests.get(string, timeout=10) + session = requests.Session() + page = session.get(string, timeout=10) if page.encoding is None: page.encoding = "utf-8" except requests.exceptions.RequestException as err: diff --git a/src/cobib/utils/file_downloader.py b/src/cobib/utils/file_downloader.py index 2b13a38daeaebf2db2aa13c63c0213731328d251..53e14cfd0f4f2486d546e0b15a137f176a2a216b 100644 --- a/src/cobib/utils/file_downloader.py +++ b/src/cobib/utils/file_downloader.py @@ -117,7 +117,8 @@ class FileDownloader: LOGGER.info("Downloading %s to %s", url, path) try: - response = requests.get(url, timeout=10, stream=True, headers=headers) + session = requests.Session() + response = session.get(url, timeout=10, stream=True, headers=headers) total_length_str = response.headers.get("content-length", None) total_length = int(total_length_str) if total_length_str is not None else None except requests.exceptions.RequestException as err: diff --git a/tests/parsers/test_arxiv.py b/tests/parsers/test_arxiv.py index a3559512d4919dc49dcbaac73ebf658897691d0b..4d13784343a56a9c9f2227584746f288d61d5c27 100644 --- a/tests/parsers/test_arxiv.py +++ b/tests/parsers/test_arxiv.py @@ -101,7 +101,7 @@ class TestArxivParser(ParserTest): """Mock function to raise an Exception.""" raise requests.exceptions.RequestException() - monkeypatch.setattr(requests, "get", raise_exception) + monkeypatch.setattr(requests.Session, "get", raise_exception) ArxivParser().parse("1701.0821") assert ( diff --git a/tests/parsers/test_doi.py b/tests/parsers/test_doi.py index d8240385130bb685484002aa8002e643cf7a1a46..4c88998db1c10e12d0f619e6e9bbaa20c5cc76bd 100644 --- a/tests/parsers/test_doi.py +++ b/tests/parsers/test_doi.py @@ -89,7 +89,7 @@ class TestDOIParser(ParserTest): """Mock function to raise an Exception.""" raise requests.exceptions.RequestException() - monkeypatch.setattr(requests, "get", raise_exception) + monkeypatch.setattr(requests.Session, "get", raise_exception) DOIParser().parse("10.1021/acs.jpclett.3c00330") assert ( diff --git a/tests/parsers/test_isbn.py b/tests/parsers/test_isbn.py index 92d5de15a09f561cdbf0068e1cfd786d0dc8d2ea..c6ade6962a4b1f325ace530fdab3efbf5a2f7cfd 100644 --- a/tests/parsers/test_isbn.py +++ b/tests/parsers/test_isbn.py @@ -86,7 +86,7 @@ class TestISBNParser(ParserTest): """Mock function to raise an Exception.""" raise requests.exceptions.RequestException() - monkeypatch.setattr(requests, "get", raise_exception) + monkeypatch.setattr(requests.Session, "get", raise_exception) ISBNParser().parse("978-1-449-35573-9") if any( diff --git a/tests/utils/test_file_downloader.py b/tests/utils/test_file_downloader.py index 4f3ed11c2acd1c8ed798cfbe788ad2b63120978b..9b3532967ddfa0bbf8aa0dc96603f4dfe44e057c 100644 --- a/tests/utils/test_file_downloader.py +++ b/tests/utils/test_file_downloader.py @@ -178,7 +178,7 @@ async def test_gracefully_fail_download(monkeypatch: pytest.MonkeyPatch) -> None """Mock function to raise an Exception.""" raise requests.exceptions.RequestException() - monkeypatch.setattr(requests, "get", raise_exception) + monkeypatch.setattr(requests.Session, "get", raise_exception) with tempfile.TemporaryDirectory() as tmpdirname: assert (