bug: fix a possible failed authentication when using gRPC.
This commit is contained in:
@@ -18,6 +18,7 @@ import logging
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from betterproto import Message
|
from betterproto import Message
|
||||||
|
from grpclib import GRPCError, Status
|
||||||
from grpclib.client import Channel
|
from grpclib.client import Channel
|
||||||
|
|
||||||
from pyasic.errors import APIError
|
from pyasic.errors import APIError
|
||||||
@@ -92,13 +93,23 @@ class BOSerGRPCAPI:
|
|||||||
metadata = []
|
metadata = []
|
||||||
if auth:
|
if auth:
|
||||||
metadata.append(("authorization", await self.auth()))
|
metadata.append(("authorization", await self.auth()))
|
||||||
|
try:
|
||||||
async with Channel(self.ip, self.port) as c:
|
async with Channel(self.ip, self.port) as c:
|
||||||
endpoint = getattr(BOSMinerGRPCStub(c), command)
|
endpoint = getattr(BOSMinerGRPCStub(c), command)
|
||||||
if endpoint is None:
|
if endpoint is None:
|
||||||
if not ignore_errors:
|
if not ignore_errors:
|
||||||
raise APIError(f"Command not found - {endpoint}")
|
raise APIError(f"Command not found - {endpoint}")
|
||||||
return {}
|
return {}
|
||||||
|
try:
|
||||||
return (await endpoint(message, metadata=metadata)).to_pydict()
|
return (await endpoint(message, metadata=metadata)).to_pydict()
|
||||||
|
except GRPCError as e:
|
||||||
|
if e.status == Status.UNAUTHENTICATED:
|
||||||
|
await self._get_auth()
|
||||||
|
metadata = [("authorization", await self.auth())]
|
||||||
|
return (await endpoint(message, metadata=metadata)).to_pydict()
|
||||||
|
raise e
|
||||||
|
except GRPCError as e:
|
||||||
|
raise APIError(f"gRPC command failed - {endpoint}") from e
|
||||||
|
|
||||||
async def auth(self):
|
async def auth(self):
|
||||||
if self._auth is not None and self._auth_time - datetime.now() < timedelta(
|
if self._auth is not None and self._auth_time - datetime.now() < timedelta(
|
||||||
|
|||||||
Reference in New Issue
Block a user