2121from graphql .utilities import find_breaking_changes
2222from jinja2 import Template
2323
24+ from openhexa .cli .graphql .graphql_client import Client
2425from openhexa .cli .settings import settings
2526from openhexa .sdk .pipelines import get_local_workspace_config
2627from openhexa .sdk .pipelines .runtime import get_pipeline
@@ -107,7 +108,16 @@ def get_library_versions() -> tuple[str, str]:
107108 return installed_version , installed_version
108109
109110
110- def detect_graphql_breaking_changes (token ):
111+ def _detect_graphql_breaking_changes_if_needed (token ):
112+ """Detect breaking changes if not done recently between the schema referenced in the SDK and the server using graphql-core."""
113+ ONE_HOUR = 60 * 60
114+ now_timestamp = int (datetime .now ().timestamp ())
115+ if not settings .last_breaking_change_check or now_timestamp - settings .last_breaking_change_check > ONE_HOUR :
116+ _detect_graphql_breaking_changes (token )
117+ settings .last_breaking_change_check = now_timestamp
118+
119+
120+ def _detect_graphql_breaking_changes (token ):
111121 """Detect breaking changes between the schema referenced in the SDK and the server using graphql-core."""
112122 stored_schema_obj = build_schema ((Path (__file__ ).parent / "graphql" / "schema.generated.graphql" ).open ().read ())
113123 server_schema_obj = build_client_schema (
@@ -133,11 +143,7 @@ def detect_graphql_breaking_changes(token):
133143
134144def graphql (query : str , variables = None , token = None ):
135145 """Check that there is no breaking change and perform a GraphQL request."""
136- ONE_HOUR = 60 * 60
137- now_timestamp = int (datetime .now ().timestamp ())
138- if not settings .last_breaking_change_check or now_timestamp - settings .last_breaking_change_check > ONE_HOUR :
139- detect_graphql_breaking_changes (token )
140- settings .last_breaking_change_check = now_timestamp
146+ _detect_graphql_breaking_changes_if_needed (token )
141147 return _query_graphql (query , variables , token )
142148
143149
@@ -739,3 +745,47 @@ def is_dhis2_connection_up(workspace_slug: str, connection_slug: str) -> bool:
739745 },
740746 )
741747 return response ["data" ]["connectionBySlug" ]["status" ] == "UP"
748+
749+
750+ class OpenHexaClient (Client ):
751+ """OpenHexaClient is a class that provides methods to interact with the OpenHexa GraphQL API."""
752+
753+ def __init__ (self , token = None ):
754+ """Initialize the OpenHexaClient with the OpenHexa API URL and headers."""
755+ self ._url = settings .api_url + "/graphql/"
756+ self ._token = token or settings .access_token
757+
758+ if not self ._token :
759+ raise InvalidTokenError ("No token found for workspace" )
760+
761+ super ().__init__ (
762+ url = self ._url ,
763+ headers = {
764+ "User-Agent" : f"openhexa-cli/{ version ('openhexa.sdk' )} " ,
765+ "Authorization" : f"Bearer { self ._token } " ,
766+ },
767+ )
768+ logging .getLogger ("httpx" ).setLevel (
769+ logging .WARNING
770+ ) # HTTPX logs queries by default, we disable them here with WARNING level
771+
772+ def execute (self , query , ** kwargs ):
773+ """Decorate parent execute method to log the GraphQL query and response."""
774+ _detect_graphql_breaking_changes (token = self ._token )
775+
776+ if settings .debug :
777+ click .echo ("" )
778+ click .echo ("Graphql Query:" )
779+ click .echo (f"URL: { self .url } " )
780+ click .echo (f"Query: { query } " )
781+ variables = kwargs .get ("variables" , {})
782+ click .echo (f"Variables: { variables } " )
783+
784+ response = super ().execute (query = query , ** kwargs )
785+
786+ if settings .debug :
787+ click .echo ("" )
788+ click .echo ("Graphql Response:" )
789+ click .echo (f"Response: { response } " )
790+
791+ return response
0 commit comments