Skip to content

Commit b14073b

Browse files
wilsonfreitasclaude
andcommitted
Replace requests with httpx for unified HTTP client (Phase 4)
- bcb/sgs/__init__.py: replace requests.get with httpx.get - bcb/currency.py: replace all requests.get calls with httpx.get, update _get_valid_currency_list return type to httpx.Response, replace requests.exceptions.ConnectionError with httpx.ConnectError - Add follow_redirects=True to all httpx.get calls (requests follows redirects by default; httpx does not) - pyproject.toml: remove requests from main dependencies; remove types-requests from dev dependencies (no longer needed) poetry run mypy bcb/ exits clean; all 19 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2e885f0 commit b14073b

4 files changed

Lines changed: 17 additions & 34 deletions

File tree

bcb/currency.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from io import BytesIO, StringIO
55
from typing import List, Optional, Union
66

7+
import httpx
78
import numpy as np
89
import pandas as pd
9-
import requests
1010
from lxml import html
1111

1212
from .exceptions import BCBAPIError, CurrencyNotFoundError
@@ -35,7 +35,7 @@ def _currency_id_list() -> pd.DataFrame:
3535
return CACHE.get("TEMP_CURRENCY_ID_LIST")
3636
else:
3737
url1 = "https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?" "method=exibeFormularioConsultaBoletim"
38-
res = requests.get(url1)
38+
res = httpx.get(url1, follow_redirects=True)
3939
if res.status_code != 200:
4040
msg = f"BCB API Request error, status code = {res.status_code}"
4141
raise BCBAPIError(msg, res.status_code)
@@ -49,11 +49,11 @@ def _currency_id_list() -> pd.DataFrame:
4949
return df
5050

5151

52-
def _get_valid_currency_list(_date: date, n: int = 0) -> requests.models.Response:
52+
def _get_valid_currency_list(_date: date, n: int = 0) -> httpx.Response:
5353
url2 = f"http://www4.bcb.gov.br/Download/fechamento/M{_date:%Y%m%d}.csv"
5454
try:
55-
res = requests.get(url2)
56-
except requests.exceptions.ConnectionError as ex:
55+
res = httpx.get(url2, follow_redirects=True)
56+
except httpx.ConnectError as ex:
5757
if n >= 3:
5858
raise ex
5959
return _get_valid_currency_list(_date, n + 1)
@@ -112,7 +112,7 @@ def _get_symbol(symbol: str, start_date: DateInput, end_date: DateInput) -> Opti
112112
except CurrencyNotFoundError:
113113
return None
114114
url = _currency_url(cid, start_date, end_date)
115-
res = requests.get(url)
115+
res = httpx.get(url, follow_redirects=True)
116116

117117
if res.headers["Content-Type"].startswith("text/html"):
118118
doc = html.parse(BytesIO(res.content)).getroot()

bcb/sgs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from io import StringIO
33
from typing import Dict, Generator, List, Mapping, Optional, Tuple, TypeAlias, Union
44

5+
import httpx
56
import pandas as pd
6-
import requests
77

88
from bcb.exceptions import SGSError
99
from bcb.utils import Date, DateInput
@@ -187,7 +187,7 @@ def get_json(
187187
série temporal univariada em formato JSON.
188188
"""
189189
url, payload = _get_url_and_payload(code, start, end, last)
190-
res = requests.get(url, params=payload)
190+
res = httpx.get(url, params=payload, follow_redirects=True)
191191
if res.status_code != 200:
192192
try:
193193
res_json = json.loads(res.text)

poetry.lock

Lines changed: 9 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ python = ">= 3.10"
1111
httpx = ">= 0.24.0"
1212
lxml = ">= 4.9.2"
1313
pandas = ">= 2.0.0"
14-
requests = ">= 2.31.0"
1514

1615
[tool.poetry.group.test.dependencies]
1716
pytest = ">= 7.1.3"
@@ -34,7 +33,6 @@ black = { version = ">= 22.8.0", allow-prereleases = true }
3433
jupyter = "^1.1.1"
3534
mypy = ">= 1.0.0"
3635
pytest-cov = ">= 4.0.0"
37-
types-requests = "^2.32.4.20260107"
3836

3937
[build-system]
4038
requires = ["poetry-core"]

0 commit comments

Comments
 (0)