Skip to content

Commit

Permalink
replace requests with httpx for all http requests fix #196
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Oct 14, 2024
1 parent 793ada9 commit 5a10b92
Show file tree
Hide file tree
Showing 123 changed files with 48,053 additions and 44,115 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12
3.13
14 changes: 6 additions & 8 deletions habanero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
# curl options
## verbose curl output
### setup first
import requests
import logging
import http.client
http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
import httpx
logging.basicConfig(
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG
)
### then make request
cr.works(query = "ecology")
"""
Expand Down
2 changes: 1 addition & 1 deletion habanero/cn/cn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def content_negotiation(
you'll get a `(500) Internal Server Error`
:param locale: Language locale. See `locale.locale_alias`
:param url: Base URL for the content negotiation request. Default: `https://doi.org`
:param kwargs: any additional arguments will be passed on to `requests.get`
:param kwargs: any additional arguments will be passed on to `httpx.get`
:rtype: str, which can be parsed to various formats depending on what
format you request (e.g., JSON vs. XML vs. bibtex)
Expand Down
8 changes: 4 additions & 4 deletions habanero/cn/styles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

import requests
import httpx

from ..habanero_utils import check_json

Expand All @@ -9,7 +9,7 @@ def csl_styles(**kwargs) -> list:
"""
Get list of styles from https://github.com/citation-style-language/styles
:param kwargs: any additional arguments will be passed on to `requests.get`
:param kwargs: any additional arguments will be passed on to `httpx.get`
:rtype: list, of CSL styles
Usage::
Expand All @@ -18,12 +18,12 @@ def csl_styles(**kwargs) -> list:
cn.csl_styles()
"""
base = "https://api.github.com/repos/citation-style-language/styles"
tt = requests.get(base + "/commits?per_page=1", **kwargs)
tt = httpx.get(base + "/commits?per_page=1", **kwargs)
tt.raise_for_status()
check_json(tt)
commres = tt.json()
sha = commres[0]["sha"]
sty = requests.get(base + "/git/trees/" + sha, **kwargs)
sty = httpx.get(base + "/git/trees/" + sha, **kwargs)
sty.raise_for_status()
check_json(sty)
res = sty.json()
Expand Down
6 changes: 3 additions & 3 deletions habanero/cnrequest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings

import requests
import httpx
from packaging.version import Version

from .cn_formats import cn_format_headers
Expand Down Expand Up @@ -57,11 +57,11 @@ def make_request(url, ids, format, style, locale, fail, **kwargs):

htype = {"Accept": type}
head = dict(make_ua(), **htype)
r = requests.get(url, headers=head, allow_redirects=True, **kwargs)
r = httpx.get(url, headers=head, follow_redirects=True, **kwargs)

# Raise an HTTPError if the status code of the response is 4XX or 5XX
# or warn if fail=False
if not r.ok:
if not r.is_success:
if fail:
r.raise_for_status()
else:
Expand Down
4 changes: 2 additions & 2 deletions habanero/counts/counts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any
from xml.dom import minidom

import requests
import httpx

from ..habanero_utils import make_ua

Expand Down Expand Up @@ -32,7 +32,7 @@ def citation_count(
"""
args = {"id": "doi:" + doi, "pid": key, "noredirect": True}
new_args: dict[str, Any] = dict((k, v) for k, v in args.items() if v)
res = requests.get(url, params=new_args, headers=make_ua(), **kwargs)
res = httpx.get(url, params=new_args, headers=make_ua(), **kwargs)
xmldoc = minidom.parseString(res.content)
val = xmldoc.getElementsByTagName("query")[0].attributes["fl_count"].value
return int(str(val))
16 changes: 7 additions & 9 deletions habanero/crossref/crossref.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Crossref:
Using `ua_string` you can set an additional string that will be added
to the UA string we send in every request, which looks like:
`python-requests/2.22.0 habanero/0.7.0`. We send that string with
`python-httpx/0.27.2 habanero/1.2.6`. We send that string with
the headers: `User-Agent` and `X-USER-AGENT`. Turn on verbose curl
output to see the request headers sent. To unset the `ua_string`
you set, just initialize a new Crossref class.
Expand Down Expand Up @@ -96,15 +96,13 @@ class Crossref:
**Verbose curl output**::
import requests
import logging
import http.client
http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
import httpx
logging.basicConfig(
format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG
)
from habanero import Crossref
cr = Crossref()
Expand Down
4 changes: 2 additions & 2 deletions habanero/habanero_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import re

import requests
import httpx

from . import __version__
from .exceptions import RequestError
Expand Down Expand Up @@ -77,7 +77,7 @@ def parse_json_err(x):


def make_ua(mailto=None, ua_string=None):
requa = "python-requests/" + requests.__version__
requa = "python-httpx/" + httpx.__version__
habua = "habanero/%s" % __version__
ua = requa + " " + habua
if mailto is not None:
Expand Down
21 changes: 10 additions & 11 deletions habanero/request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings

import requests
import httpx

from .exceptions import RequestError
from .filterhandler import filter_handler
Expand Down Expand Up @@ -37,7 +37,7 @@ def request(
agency=False,
progress_bar=False,
should_warn=False,
**kwargs
**kwargs,
):
"""HTTP request helper."""
warning_thrown = False
Expand Down Expand Up @@ -77,15 +77,16 @@ def request(
if ids.__class__.__name__ == "NoneType":
url = url.strip("/")
try:
r = requests.get(url, params=payload, headers=make_ua(mailto, ua_string))
r = httpx.get(url, params=payload, headers=make_ua(mailto, ua_string))
r.raise_for_status()
except requests.exceptions.HTTPError:
except httpx.HTTPStatusError:
if is_json(r):
raise RequestError(r.status_code, parse_json_err(r))
else:
r.raise_for_status()
except requests.exceptions.RequestException as e:
raise RuntimeError(e)
except httpx.HTTPError as e:
raise RuntimeError(f"HTTP Exception for {e.request.url} - {e}")
# raise RuntimeError(e)
else:
if not r:
raise RuntimeError("An unknown problem occurred with an HTTP request")
Expand Down Expand Up @@ -119,7 +120,7 @@ def request(
cursor_max,
None,
progress_bar,
**kwargs
**kwargs,
).do_request(should_warn=should_warn)
coll.append(res)
else:
Expand All @@ -130,12 +131,10 @@ def request(

endpt = endpt.strip("/")

r = requests.get(
endpt, params=payload, headers=make_ua(mailto, ua_string)
)
r = httpx.get(endpt, params=payload, headers=make_ua(mailto, ua_string))
if r.status_code > 201 and should_warn:
warning_thrown = True
mssg = "%s on %s: %s" % (r.status_code, ids[i], r.reason)
mssg = "%s on %s: %s" % (r.status_code, ids[i], r.reason_phrase)
warnings.warn(mssg)
else:
r.raise_for_status()
Expand Down
12 changes: 6 additions & 6 deletions habanero/request_class.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math
import warnings

import requests
import httpx
from tqdm import tqdm # type: ignore
from urllib3.exceptions import ConnectTimeoutError

Expand Down Expand Up @@ -144,26 +144,26 @@ def _redo_req(self, js, payload, cu, max_avail, should_warn):
def _req(self, payload, should_warn):
r = None
try:
r = requests.get(
r = httpx.get(
self._url(),
params=payload,
headers=make_ua(self.mailto, self.ua_string),
)
r.raise_for_status()
except requests.exceptions.HTTPError:
except httpx.HTTPStatusError:
try:
f = r.json()
raise RequestError(r.status_code, f["message"][0]["message"])
except:
if should_warn:
mssg = "%s: %s" % (r.status_code, r.reason)
mssg = "%s: %s" % (r.status_code, r.reason_phrase)
warnings.warn(mssg)
return None
else:
r.raise_for_status()
except ConnectTimeoutError as e:
raise requests.exceptions.ConnectTimeout(e, r)
except requests.exceptions.RequestException as e:
raise httpx.ConnectTimeout(e, r)
except httpx.HTTPError as e:
# print(e)
raise RuntimeError(e)
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ license = {file = "LICENSE.md"}
requires-python = ">=3.12"
dependencies = [
"bibtexparser==2.0.0b7",
"httpx>=0.27.2",
"packaging>=24.1",
"pyyaml>=6.0.2",
"requests>=2.32.3",
"tqdm>=4.66.5",
"urllib3==2.2.0",
]
Expand Down
Loading

0 comments on commit 5a10b92

Please sign in to comment.