Skip to content

Commit

Permalink
Use synchronous websocket session
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jan 17, 2024
1 parent 5e88f8b commit 36cac33
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/isar_exr/api/graphql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

from gql import Client
from gql.dsl import DSLSchema
from gql.transport.httpx import HTTPXTransport
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.exceptions import (
TransportClosed,
TransportProtocolError,
TransportQueryError,
TransportServerError,
TransportAlreadyConnected,
)
from graphql import DocumentNode, GraphQLError, GraphQLSchema, build_ast_schema, parse

Expand All @@ -22,9 +24,9 @@ def __init__(self) -> None:
# in case of expired token
self._reauthenticated: bool = False
self.logger: Logger = getLogger("graphql_client")
self._initialize_client()
self._initialize_session()

def _initialize_client(self):
def _initialize_session(self):
try:
token: str = get_access_token()
except Exception as e:
Expand All @@ -41,11 +43,12 @@ def _initialize_client(self):

schema: GraphQLSchema = build_ast_schema(document)

transport: AIOHTTPTransport = AIOHTTPTransport(
transport: HTTPXTransport = HTTPXTransport(
url=settings.ROBOT_API_URL, headers=auth_header
)
self.client: Client = Client(transport=transport, schema=schema)
self.schema: DSLSchema = DSLSchema(self.client.schema)
self.session = self.client.connect_sync()

def query(
self, query: DocumentNode, query_parameters: dict[str, Any]
Expand All @@ -60,7 +63,7 @@ def query(
:raises Exception: Unknown error
"""
try:
response: Dict[str, Any] = self.client.execute(query, query_parameters)
response: Dict[str, Any] = self.session.execute(query, query_parameters)
return response
except GraphQLError as e:
self.logger.error(
Expand Down Expand Up @@ -89,6 +92,9 @@ def query(
except TransportServerError as e:
self.logger.error(f"Error in Energy Robotics server: {e}")
raise
except TransportAlreadyConnected as e:
self.logger.error(f"The transport is already connected: {e}")
raise
except Exception as e:
self.logger.error(f"Unknown error in GraphQL client: {e}")
raise
Expand Down

0 comments on commit 36cac33

Please sign in to comment.