mirror of
https://github.com/miaowware/qrm2.git
synced 2025-05-29 04:42:26 -04:00
utils/resources_manager.py: use httpx instead of requests
This commit is contained in:
parent
970159e81b
commit
adffd82127
@ -5,5 +5,5 @@ qrztools[async]~=1.0
|
|||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
pytz
|
pytz
|
||||||
cairosvg
|
cairosvg
|
||||||
requests
|
httpx
|
||||||
pydantic
|
pydantic
|
||||||
|
@ -9,7 +9,7 @@ SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
|||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import requests
|
import httpx
|
||||||
|
|
||||||
from utils.resources_models import Index
|
from utils.resources_models import Index
|
||||||
|
|
||||||
@ -28,8 +28,11 @@ class ResourcesManager:
|
|||||||
def sync_fetch(self, filepath: str):
|
def sync_fetch(self, filepath: str):
|
||||||
"""Fetches files in sync mode."""
|
"""Fetches files in sync mode."""
|
||||||
self.print_msg(f"Fetching {filepath}", "sync")
|
self.print_msg(f"Fetching {filepath}", "sync")
|
||||||
with requests.get(self.url + filepath) as resp:
|
resp = httpx.get(self.url + filepath)
|
||||||
return resp.content
|
resp.raise_for_status()
|
||||||
|
r = resp.content
|
||||||
|
resp.close()
|
||||||
|
return r
|
||||||
|
|
||||||
def sync_start(self, basedir: Path) -> Index:
|
def sync_start(self, basedir: Path) -> Index:
|
||||||
"""Takes cares of constructing the local resources repository and initialising the RM."""
|
"""Takes cares of constructing the local resources repository and initialising the RM."""
|
||||||
@ -40,7 +43,7 @@ class ResourcesManager:
|
|||||||
new_index: Index = self.parse_index(raw)
|
new_index: Index = self.parse_index(raw)
|
||||||
with (basedir / "index.json").open("wb") as file:
|
with (basedir / "index.json").open("wb") as file:
|
||||||
file.write(raw)
|
file.write(raw)
|
||||||
except (requests.RequestException, OSError) as ex:
|
except (httpx.RequestError, OSError) as ex:
|
||||||
self.print_msg(f"There was an issue fetching the index: {ex.__class__.__name__}: {ex}", "sync")
|
self.print_msg(f"There was an issue fetching the index: {ex.__class__.__name__}: {ex}", "sync")
|
||||||
if (basedir / "index.json").exists():
|
if (basedir / "index.json").exists():
|
||||||
self.print_msg("Old file exist, using old resources", "fallback")
|
self.print_msg("Old file exist, using old resources", "fallback")
|
||||||
@ -58,7 +61,7 @@ class ResourcesManager:
|
|||||||
try:
|
try:
|
||||||
with (basedir / file.filename).open("wb") as f:
|
with (basedir / file.filename).open("wb") as f:
|
||||||
f.write(self.sync_fetch(file.filename))
|
f.write(self.sync_fetch(file.filename))
|
||||||
except (requests.RequestException, OSError) as ex:
|
except (httpx.RequestError, OSError) as ex:
|
||||||
ex_cls = ex.__class__.__name__
|
ex_cls = ex.__class__.__name__
|
||||||
self.print_msg(f"There was an issue fetching {file.filename}: {ex_cls}: {ex}", "sync")
|
self.print_msg(f"There was an issue fetching {file.filename}: {ex_cls}: {ex}", "sync")
|
||||||
if not (basedir / file.filename).exists():
|
if not (basedir / file.filename).exists():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user