From 2e896d11c730ee7359fb7e25dcce25b2f559c73c Mon Sep 17 00:00:00 2001 From: Laurent LAPORTE Date: Mon, 4 Sep 2023 08:49:10 +0200 Subject: [PATCH] refactor(tools): prepare for migration from `Requests` to `httpx` This commit prepares for migrating from Requests to httpx by adjusting imports and usage in LDAP, command line tools, and GUI code. --- antarest/gui.py | 8 +++++++- antarest/login/ldap.py | 7 ++++++- antarest/tools/lib.py | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/antarest/gui.py b/antarest/gui.py index 56497e9ceb..2a408610ad 100644 --- a/antarest/gui.py +++ b/antarest/gui.py @@ -6,7 +6,13 @@ from multiprocessing import Process from pathlib import Path -import requests +try: + # `httpx` is a modern alternative to the `requests` library + import httpx as requests +except ImportError: + # noinspection PyUnresolvedReferences, PyPackageRequirements + import requests + import uvicorn # type: ignore from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QAction, QApplication, QMenu, QSystemTrayIcon diff --git a/antarest/login/ldap.py b/antarest/login/ldap.py index e9233c7d18..d558238288 100644 --- a/antarest/login/ldap.py +++ b/antarest/login/ldap.py @@ -2,7 +2,12 @@ from dataclasses import dataclass from typing import Dict, List, Optional -import requests +try: + # `httpx` is a modern alternative to the `requests` library + import httpx as requests +except ImportError: + # noinspection PyUnresolvedReferences, PyPackageRequirements + import requests from antarest.core.config import Config from antarest.core.model import JSON diff --git a/antarest/tools/lib.py b/antarest/tools/lib.py index 7c50286083..b27e4dcee4 100644 --- a/antarest/tools/lib.py +++ b/antarest/tools/lib.py @@ -8,8 +8,17 @@ from zipfile import ZipFile import numpy as np -import requests -from requests import Session + +try: + # The HTTPX equivalent of `requests.Session` is `httpx.Client`. + import httpx as requests + from httpx import Client as Session +except ImportError: + # noinspection PyUnresolvedReferences, PyPackageRequirements + import requests + + # noinspection PyUnresolvedReferences,PyPackageRequirements + from requests import Session from antarest.core.cache.business.local_chache import LocalCache from antarest.core.config import CacheConfig @@ -48,7 +57,7 @@ def __init__( session: Optional[Session] = None, ): self.study_id = study_id - self.session = session or requests.session() + self.session = session or Session() # TODO fix this self.session.verify = False self.host = host