Skip to content

Commit 2e34a30

Browse files
authored
fix(httpx): try passing headers to httpx requests to prevent 403 from hetzner (#303)
1 parent b3a31ac commit 2e34a30

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

pysus/api/ducklake/catalog/adapters.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ async def _download_catalog(
158158
force: bool = False,
159159
callback: Callable[[int, int], None] | None = None,
160160
) -> None:
161-
url = f"https://{types.S3_ENDPOINT}/{types.S3_BUCKET}/{remote_path}"
161+
remote = str(remote_path).replace("\\", "/")
162+
url = f"https://{types.S3_ENDPOINT}/{types.S3_BUCKET}/{remote}"
162163

163164
if local_path.exists() and not force:
164165
try:
@@ -170,7 +171,17 @@ async def _download_catalog(
170171

171172
remote_size = 0
172173
if local_size != -1:
173-
async with httpx.AsyncClient(follow_redirects=True) as client:
174+
headers = {
175+
"User-Agent": (
176+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
177+
"AppleWebKit/537.36 (KHTML, like Gecko) "
178+
"Chrome/120.0.0.0 Safari/537.36"
179+
),
180+
"Referer": "https://github.com/AlertaDengue/PySUS",
181+
}
182+
async with httpx.AsyncClient(
183+
headers=headers, follow_redirects=True
184+
) as client:
174185
try:
175186
head = await client.head(url)
176187

@@ -189,7 +200,7 @@ async def _download_catalog(
189200

190201
try:
191202
await download_http(
192-
remote_path=remote_path,
203+
remote_path=remote,
193204
local_path=local_path,
194205
callback=callback,
195206
)

pysus/api/ducklake/functional.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@ async def download_http(
1414
local_path: Path,
1515
callback: Callable[[int, int], None] | None = None,
1616
) -> None:
17+
remote_path = str(remote_path).replace("\\", "/")
1718
url = f"https://{types.S3_ENDPOINT}/{types.S3_BUCKET}/{remote_path}"
1819
max_retries = 5
1920

2021
timeout = httpx.Timeout(15.0, read=60.0, write=20.0, connect=15.0)
2122
limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
23+
headers = {
24+
"User-Agent": (
25+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
26+
"AppleWebKit/537.36 (KHTML, like Gecko) "
27+
"Chrome/120.0.0.0 Safari/537.36"
28+
),
29+
"Referer": "https://github.com/AlertaDengue/PySUS",
30+
}
2231

2332
for attempt in range(max_retries):
2433
try:
2534
async with httpx.AsyncClient(
35+
headers=headers,
2636
follow_redirects=True,
2737
verify=False,
2838
limits=limits,

0 commit comments

Comments
 (0)