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