66import os
77import typing
88
9+ import httpx
10+ import requests
11+
912from openhexa .graphql import BaseOpenHexaClient
1013from openhexa .utils import create_requests_session
1114
1215
16+ class SSLError (Exception ):
17+ """Raised when SSL certificate verification fails."""
18+
19+ pass
20+
21+
22+ def handle_ssl_error (e : Exception ) -> None :
23+ """Handle SSL certificate verification errors with helpful message.
24+
25+ Args:
26+ e: The exception to handle.
27+ """
28+ if "SSL certificate verification failed" in str (e ) or "CERTIFICATE_VERIFY_FAILED" in str (e ):
29+ error_msg = (
30+ "SSL certificate verification failed. "
31+ "If you want to disable SSL verification, set the environment variable: HEXA_VERIFY_SSL=false"
32+ )
33+ if Settings .debug ():
34+ raise SSLError (error_msg ) from e
35+ else :
36+ raise SSLError (error_msg )
37+
38+
1339class Settings :
1440 """Centralized settings for the OpenHexa SDK."""
1541
@@ -45,21 +71,24 @@ def get_environment():
4571
4672def graphql (operation : str , variables : dict [str | typing .Any ] | None = None ) -> dict [str | typing .Any ]:
4773 """Performa GraphQL query."""
48- auth_token = os .environ [
49- "HEXA_TOKEN"
50- ] # Works for notebooks with the membership token and pipelines with the run token
74+ auth_token = os .environ ["HEXA_TOKEN" ]
5175 headers = {"Authorization" : f"Bearer { auth_token } " }
5276 session = create_requests_session (verify = Settings .verify_ssl ())
5377
54- req = session .post (
55- f"{ os .environ ['HEXA_SERVER_URL' ].rstrip ('/' )} /graphql/" ,
56- headers = headers ,
57- json = {
58- "query" : operation ,
59- "variables" : variables if variables is not None else {},
60- },
61- )
62- req .raise_for_status ()
78+ try :
79+ req = session .post (
80+ f"{ os .environ ['HEXA_SERVER_URL' ].rstrip ('/' )} /graphql/" ,
81+ headers = headers ,
82+ json = {
83+ "query" : operation ,
84+ "variables" : variables if variables is not None else {},
85+ },
86+ )
87+ req .raise_for_status ()
88+ except (requests .exceptions .SSLError , httpx .ConnectError ) as e :
89+ handle_ssl_error (e )
90+ raise
91+
6392 body = req .json ()
6493 if "errors" in body :
6594 raise Exception (body ["errors" ])
@@ -80,7 +109,11 @@ def __init__(self, token: str | None = None, server_url: str | None = None):
80109 url = server_url or f"{ os .environ ['HEXA_SERVER_URL' ].rstrip ('/' )} /graphql/"
81110 token = token or os .getenv ("HEXA_TOKEN" )
82111
83- super ().__init__ (url = url , token = token , verify = Settings .verify_ssl ())
112+ try :
113+ super ().__init__ (url = url , token = token , verify = Settings .verify_ssl ())
114+ except (requests .exceptions .SSLError , httpx .ConnectError ) as e :
115+ handle_ssl_error (e )
116+ raise
84117
85118
86119class Iterator (metaclass = abc .ABCMeta ):
0 commit comments