Skip to content

Commit

Permalink
handle options in grpc connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rfrerebe-stx committed Jul 1, 2024
1 parent 9c0e452 commit 3baf776
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/systemathics/apis/helpers/channel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

DEFAULT_ENDPOINT = "https://grpc.ganymede.cloud"

def get_grpc_channel(endpoint = "") -> grpc.Channel:
def get_grpc_channel(endpoint : str, **kwargs) -> grpc.Channel:
"""
Get a channel suitable to call Ganymede gRPC API.
If no endpoint parameter is given, try to use GRPC_APIS environment variable or fallback to DEFAULT_ENDPOINT.
Expand All @@ -27,17 +27,18 @@ def get_grpc_channel(endpoint = "") -> grpc.Channel:
endpoint = endpoint if endpoint else os.getenv('GRPC_APIS','')
endpoint = endpoint if endpoint else DEFAULT_ENDPOINT # if no endpoint was provided, use the default one
endpoint = endpoint if endpoint.startswith("http") else f"https://{endpoint}" # if no scheme was provided, assume it's https
return _get_grpc_channel(endpoint)
return _get_grpc_channel(endpoint, **kwargs)

def _get_grpc_channel(endpoint: str) -> grpc.Channel:
def _get_grpc_channel(endpoint: str, **kwargs) -> grpc.Channel:
if not endpoint or not endpoint.startswith("http"):
raise "endpoint should have scheme http or https"
options = [ (arg,kwargs[arg]) for arg in kwargs ]
if (endpoint.startswith("https")):
return grpc.secure_channel(endpoint.replace("https://",""), _get_channel_credentials())
return grpc.secure_channel(endpoint.replace("https://",""), _get_channel_credentials(), options = options)
else:
return grpc.insecure_channel(endpoint.replace("http://",""))
return grpc.insecure_channel(endpoint.replace("http://",""), options = options)

def get_aio_grpc_channel(endpoint = "") -> grpc.aio.Channel:
def get_aio_grpc_channel(endpoint = "", **kwargs) -> grpc.aio.Channel:
"""
Get an aio channel suitable to call Ganymede gRPC API.
If no endpoint parameter is given, try to use GRPC_APIS environment variable or fallback to DEFAULT_ENDPOINT.
Expand All @@ -50,15 +51,16 @@ def get_aio_grpc_channel(endpoint = "") -> grpc.aio.Channel:
endpoint = endpoint if endpoint else os.getenv('GRPC_APIS','')
endpoint = endpoint if endpoint else DEFAULT_ENDPOINT # if no endpoint was provided, use the default one
endpoint = endpoint if endpoint.startswith("http") else f"https://{endpoint}" # if no scheme was provided, assume it's https
return _get_aio_grpc_channel(endpoint)
return _get_aio_grpc_channel(endpoint, **kwargs)

def _get_aio_grpc_channel(endpoint: str) -> grpc.aio.Channel:
def _get_aio_grpc_channel(endpoint: str, **kwargs) -> grpc.aio.Channel:
if not endpoint or not endpoint.startswith("http"):
raise "endpoint should have scheme http or https"
options = [ (arg,kwargs[arg]) for arg in kwargs ]
if (endpoint.startswith("https")):
return grpc.aio.secure_channel(endpoint.replace("https://",""), _get_channel_credentials())
return grpc.aio.secure_channel(endpoint.replace("https://",""), _get_channel_credentials(), options = options )
else:
return grpc.aio.insecure_channel(endpoint.replace("http://",""))
return grpc.aio.insecure_channel(endpoint.replace("http://",""), options = options )

def _get_channel_credentials() -> grpc.ChannelCredentials:
# If we have a SSL_CERT_FILE env variable, use it
Expand Down

0 comments on commit 3baf776

Please sign in to comment.