Browse Source

replace requests with urllib.request

Fabian Peter Hammerle 2 years ago
parent
commit
f750e08b53
6 changed files with 30 additions and 65 deletions
  1. 5 0
      CHANGELOG.md
  2. 0 1
      Pipfile
  3. 1 54
      Pipfile.lock
  4. 6 5
      pubmed_bibtex/__init__.py
  5. 1 3
      setup.py
  6. 17 2
      tests/test_bibtex_entry_from_pmid.py

+ 5 - 0
CHANGELOG.md

@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 - GPLv3+ license
 
+### Changed
+- `bibtex_entry_from_pmid` raises `urllib.error.HTTPError`
+  instead of `requests.exceptions.HTTPError`
+  (replaced [requests](https://pypi.org/project/requests/) with python's `urllib.request`)
+
 ### Removed
 - compatibility with `python3.5` & `python3.6`
 

+ 0 - 1
Pipfile

@@ -13,7 +13,6 @@ pylint = "*"
 pylint-import-requirements = "*"
 pytest = "*"
 pytest-cov = "*"
-types-requests = "*"
 
 # mypy on python<3.8
 typed-ast = {markers = "python_version < '3.8'"}

+ 1 - 54
Pipfile.lock

@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "3fa17dd5030f50ebe30d2545ed270b91b8186596d13a4da06f034f5ad211d66d"
+            "sha256": "6b0871414658f1c42f53f89f5c63e9b12bae08555cd8d1bc18b466ddbfc6cbe9"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -16,46 +16,9 @@
         ]
     },
     "default": {
-        "certifi": {
-            "hashes": [
-                "sha256:78884e7c1d4b00ce3cea67b44566851c4343c120abd683433ce934a68ea58872",
-                "sha256:d62a0163eb4c2344ac042ab2bdf75399a71a2d8c7d47eac2e2ee91b9d6339569"
-            ],
-            "version": "==2021.10.8"
-        },
-        "charset-normalizer": {
-            "hashes": [
-                "sha256:876d180e9d7432c5d1dfd4c5d26b72f099d503e8fcc0feb7532c9289be60fcbd",
-                "sha256:cb957888737fc0bbcd78e3df769addb41fd1ff8cf950dc9e7ad7793f1bf44455"
-            ],
-            "markers": "python_version >= '3'",
-            "version": "==2.0.10"
-        },
-        "idna": {
-            "hashes": [
-                "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff",
-                "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"
-            ],
-            "markers": "python_version >= '3'",
-            "version": "==3.3"
-        },
         "pubmed-bibtex": {
             "editable": true,
             "path": "."
-        },
-        "requests": {
-            "hashes": [
-                "sha256:68d7c56fd5a8999887728ef304a6d12edc7be74f1cfa47714fc8b414525c9a61",
-                "sha256:f22fa1e554c9ddfd16e6e41ac79759e17be9e492b3587efa038054674760e72d"
-            ],
-            "version": "==2.27.1"
-        },
-        "urllib3": {
-            "hashes": [
-                "sha256:000ca7f471a233c2251c6c7023ee85305721bfdf18621ebff4fd17a8653427ed",
-                "sha256:0e7c33d9a63e7ddfcb86780aac87befc2fbddf46c58dbb487e0855f7ceec283c"
-            ],
-            "version": "==1.26.8"
         }
     },
     "develop": {
@@ -357,27 +320,11 @@
             "markers": "python_version < '3.8'",
             "version": "==1.5.1"
         },
-        "types-requests": {
-            "hashes": [
-                "sha256:2e0e100dd489f83870d4f61949d3a7eae4821e7bfbf46c57e463c38f92d473d4",
-                "sha256:f38bd488528cdcbce5b01dc953972f3cead0d060cfd9ee35b363066c25bab13c"
-            ],
-            "index": "pypi",
-            "version": "==2.27.7"
-        },
-        "types-urllib3": {
-            "hashes": [
-                "sha256:3adcf2cb5981809091dbff456e6999fe55f201652d8c360f99997de5ac2f556e",
-                "sha256:cfd1fbbe4ba9a605ed148294008aac8a7b8b7472651d1cc357d507ae5962e3d2"
-            ],
-            "version": "==1.26.7"
-        },
         "typing-extensions": {
             "hashes": [
                 "sha256:4ca091dea149f945ec56afb48dae714f21e8692ef22a395223bcd328961b6a0e",
                 "sha256:7f001e5ac290a0c0401508864c7ec868be4e701886d5b573a9528ed3973d9d3b"
             ],
-            "markers": "python_version < '3.10'",
             "version": "==4.0.1"
         },
         "wrapt": {

+ 6 - 5
pubmed_bibtex/__init__.py

@@ -39,8 +39,8 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 import html.parser
 import re
 import typing
-
-import requests
+import urllib.parse
+import urllib.request
 
 from pubmed_bibtex.version import __version__
 
@@ -70,8 +70,9 @@ class _TeXMedHtmlParser(html.parser.HTMLParser):
 
 def bibtex_entry_from_pmid(pmid: str) -> typing.Optional[str]:
     assert pmid.isdigit(), pmid
-    resp = requests.get(_TEXMED_URL_PATTERN.format(pmid=pmid))
-    resp.raise_for_status()  # raises requests.exceptions.HTTPError
     parser = _TeXMedHtmlParser()
-    parser.feed(resp.text)
+    with urllib.request.urlopen(  # raises urllib.error.HTTPError
+        _TEXMED_URL_PATTERN.format(pmid=urllib.parse.quote(pmid))
+    ) as resp:
+        parser.feed(resp.read().decode("utf-8"))
     return parser.bibtex_entry

+ 1 - 3
setup.py

@@ -52,9 +52,7 @@ setuptools.setup(
     },
     # >=3.6 for variable type hints
     python_requires=">=3.7",  # python<3.7 untested
-    install_requires=[
-        "requests>=2,<3",
-    ],
+    install_requires=[],
     setup_requires=["setuptools_scm"],
     tests_require=[
         "pylint>=2.3.0,<3",

+ 17 - 2
tests/test_bibtex_entry_from_pmid.py

@@ -1,8 +1,23 @@
-from pubmed_bibtex import bibtex_entry_from_pmid
+import unittest.mock
+import urllib.error
+
+import pytest
+
+import pubmed_bibtex
 
 # pylint: disable=wrong-import-order; false positive
 from conftest import TEST_BIBTEX_ENTRY, TEST_PMID
 
 
 def test_bibtex_entry_from_pmid() -> None:
-    assert bibtex_entry_from_pmid(pmid=TEST_PMID) == TEST_BIBTEX_ENTRY
+    assert pubmed_bibtex.bibtex_entry_from_pmid(pmid=TEST_PMID) == TEST_BIBTEX_ENTRY
+
+
+@unittest.mock.patch.object(
+    pubmed_bibtex,
+    "_TEXMED_URL_PATTERN",
+    "https://www.bioinformatics.org/texmed/cgi-bin/42.cgi",
+)
+def test_bibtex_entry_from_pmid_not_found() -> None:
+    with pytest.raises(urllib.error.HTTPError, match=r"^HTTP Error 404: Not Found$"):
+        pubmed_bibtex.bibtex_entry_from_pmid(pmid=TEST_PMID)