feature: add default ssl ctx to all httpx clients to speed up initialization.
This commit is contained in:
@@ -474,7 +474,7 @@ class MinerFactory:
|
||||
|
||||
async def _get_miner_web(self, ip: str):
|
||||
urls = [f"http://{ip}/", f"https://{ip}/"]
|
||||
async with httpx.AsyncClient(verify=False) as session:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as session:
|
||||
tasks = [asyncio.create_task(self._web_ping(session, url)) for url in urls]
|
||||
|
||||
text, resp = await concurrent_get_first_result(
|
||||
@@ -602,7 +602,7 @@ class MinerFactory:
|
||||
location: str,
|
||||
auth: Optional[httpx.DigestAuth] = None,
|
||||
) -> Optional[dict]:
|
||||
async with httpx.AsyncClient(verify=False) as session:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as session:
|
||||
try:
|
||||
data = await session.get(
|
||||
f"http://{str(ip)}{location}",
|
||||
@@ -798,7 +798,7 @@ class MinerFactory:
|
||||
|
||||
async def get_miner_model_innosilicon(self, ip: str) -> Optional[str]:
|
||||
try:
|
||||
async with httpx.AsyncClient(verify=False) as session:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as session:
|
||||
auth_req = await session.post(
|
||||
f"http://{ip}/api/auth",
|
||||
data={"username": "admin", "password": "admin"},
|
||||
@@ -828,7 +828,7 @@ class MinerFactory:
|
||||
pass
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(verify=False) as session:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as session:
|
||||
d = await session.post(
|
||||
f"http://{ip}/graphql",
|
||||
json={"query": "{bosminer {info{modelName}}}"},
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
_settings = { # defaults
|
||||
"network_ping_retries": 1,
|
||||
"network_ping_timeout": 3,
|
||||
@@ -39,3 +41,5 @@ def get(key: str, other: Any = None) -> Any:
|
||||
|
||||
def update(key: str, val: Any) -> Any:
|
||||
_settings[key] = val
|
||||
|
||||
ssl_cxt = httpx.create_ssl_context()
|
||||
@@ -38,7 +38,7 @@ class AntminerModernWebAPI(BaseWebAPI):
|
||||
url = f"http://{self.ip}/cgi-bin/{command}.cgi"
|
||||
auth = httpx.DigestAuth(self.username, self.pwd)
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
if parameters:
|
||||
data = await client.post(
|
||||
url, data=json.dumps(parameters), auth=auth, timeout=settings.get("api_function_timeout", 3) # noqa
|
||||
@@ -57,7 +57,7 @@ class AntminerModernWebAPI(BaseWebAPI):
|
||||
async def multicommand(
|
||||
self, *commands: str, ignore_errors: bool = False, allow_warning: bool = True
|
||||
) -> dict:
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
tasks = [
|
||||
asyncio.create_task(self._handle_multicommand(client, command))
|
||||
for command in commands
|
||||
@@ -149,7 +149,7 @@ class AntminerOldWebAPI(BaseWebAPI):
|
||||
url = f"http://{self.ip}/cgi-bin/{command}.cgi"
|
||||
auth = httpx.DigestAuth(self.username, self.pwd)
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
if parameters:
|
||||
data = await client.post(
|
||||
url, data=parameters, auth=auth, timeout=settings.get("api_function_timeout", 3)
|
||||
@@ -170,7 +170,7 @@ class AntminerOldWebAPI(BaseWebAPI):
|
||||
) -> dict:
|
||||
data = {k: None for k in commands}
|
||||
auth = httpx.DigestAuth(self.username, self.pwd)
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for command in commands:
|
||||
try:
|
||||
url = f"http://{self.ip}/cgi-bin/{command}.cgi"
|
||||
|
||||
@@ -186,7 +186,7 @@ class BOSMinerGQLAPI:
|
||||
if command.get("query") is None:
|
||||
query = {"query": self.parse_command(command)}
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
await self.auth(client)
|
||||
data = await client.post(url, json=query)
|
||||
except httpx.HTTPError:
|
||||
@@ -239,7 +239,7 @@ class BOSMinerLuCIAPI:
|
||||
|
||||
async def send_command(self, path: str, ignore_errors: bool = False) -> dict:
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
await self.auth(client)
|
||||
data = await client.get(
|
||||
f"http://{self.ip}{path}", headers={"User-Agent": "BTC Tools v0.1"}
|
||||
|
||||
@@ -31,7 +31,7 @@ class GoldshellWebAPI(BaseWebAPI):
|
||||
self.jwt = None
|
||||
|
||||
async def auth(self):
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
try:
|
||||
await client.get(f"http://{self.ip}/user/logout")
|
||||
auth = (
|
||||
@@ -71,7 +71,7 @@ class GoldshellWebAPI(BaseWebAPI):
|
||||
parameters.pop("pool_pwd")
|
||||
if not self.jwt:
|
||||
await self.auth()
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for i in range(settings.get("get_data_retries", 1)):
|
||||
try:
|
||||
if parameters:
|
||||
@@ -102,7 +102,7 @@ class GoldshellWebAPI(BaseWebAPI):
|
||||
data = {k: None for k in commands}
|
||||
data["multicommand"] = True
|
||||
await self.auth()
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for command in commands:
|
||||
try:
|
||||
response = await client.get(
|
||||
|
||||
@@ -32,7 +32,7 @@ class InnosiliconWebAPI(BaseWebAPI):
|
||||
self.jwt = None
|
||||
|
||||
async def auth(self):
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
try:
|
||||
auth = await client.post(
|
||||
f"http://{self.ip}/api/auth",
|
||||
@@ -54,7 +54,7 @@ class InnosiliconWebAPI(BaseWebAPI):
|
||||
) -> dict:
|
||||
if not self.jwt:
|
||||
await self.auth()
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for i in range(settings.get("get_data_retries", 1)):
|
||||
try:
|
||||
response = await client.post(
|
||||
@@ -90,7 +90,7 @@ class InnosiliconWebAPI(BaseWebAPI):
|
||||
data = {k: None for k in commands}
|
||||
data["multicommand"] = True
|
||||
await self.auth()
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for command in commands:
|
||||
try:
|
||||
response = await client.post(
|
||||
|
||||
@@ -31,7 +31,7 @@ class VNishWebAPI(BaseWebAPI):
|
||||
self.token = None
|
||||
|
||||
async def auth(self):
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
try:
|
||||
auth = await client.post(
|
||||
f"http://{self.ip}/api/v1/unlock",
|
||||
@@ -58,7 +58,7 @@ class VNishWebAPI(BaseWebAPI):
|
||||
) -> dict:
|
||||
if not self.token:
|
||||
await self.auth()
|
||||
async with httpx.AsyncClient() as client:
|
||||
async with httpx.AsyncClient(verify=settings.ssl_cxt) as client:
|
||||
for i in range(settings.get("get_data_retries", 1)):
|
||||
try:
|
||||
auth = self.token
|
||||
|
||||
Reference in New Issue
Block a user