change all to httpx (#26119)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import os
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
import httpx
|
||||
|
||||
|
||||
class BaseRequest:
|
||||
proxies = {
|
||||
proxies: Mapping[str, str] | None = {
|
||||
"http": "",
|
||||
"https": "",
|
||||
}
|
||||
@@ -13,10 +15,31 @@ class BaseRequest:
|
||||
secret_key_header = ""
|
||||
|
||||
@classmethod
|
||||
def send_request(cls, method, endpoint, json=None, params=None):
|
||||
def _build_mounts(cls) -> dict[str, httpx.BaseTransport] | None:
|
||||
if not cls.proxies:
|
||||
return None
|
||||
|
||||
mounts: dict[str, httpx.BaseTransport] = {}
|
||||
for scheme, value in cls.proxies.items():
|
||||
if not value:
|
||||
continue
|
||||
key = f"{scheme}://" if not scheme.endswith("://") else scheme
|
||||
mounts[key] = httpx.HTTPTransport(proxy=value)
|
||||
return mounts or None
|
||||
|
||||
@classmethod
|
||||
def send_request(
|
||||
cls,
|
||||
method: str,
|
||||
endpoint: str,
|
||||
json: Any | None = None,
|
||||
params: Mapping[str, Any] | None = None,
|
||||
) -> Any:
|
||||
headers = {"Content-Type": "application/json", cls.secret_key_header: cls.secret_key}
|
||||
url = f"{cls.base_url}{endpoint}"
|
||||
response = requests.request(method, url, json=json, params=params, headers=headers, proxies=cls.proxies)
|
||||
mounts = cls._build_mounts()
|
||||
with httpx.Client(mounts=mounts) as client:
|
||||
response = client.request(method, url, json=json, params=params, headers=headers)
|
||||
return response.json()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user