Skip to content

Commit

Permalink
feat: ✨ expose http timeout and ssl_context options on the entry func…
Browse files Browse the repository at this point in the history
…tions (#101)

fix: use nase inherited constructor in sync `Client`

feat: use default ssl context by default

RESOLVES #100
  • Loading branch information
zoido authored Jul 25, 2024
1 parent bbbffb2 commit f63a371
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
25 changes: 23 additions & 2 deletions src/h2o_discovery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import dataclasses
import datetime
import os
import ssl
from typing import Optional
from typing import Tuple
from typing import Union
Expand All @@ -19,6 +21,9 @@ def discover(
environment: Optional[str] = None,
discovery_address: Optional[str] = None,
config_path: Optional[Union[str, bytes, os.PathLike]] = None,
*,
http_timeout: Optional[datetime.timedelta] = None,
ssl_context: Optional[ssl.SSLContext] = None,
) -> Discovery:
"""Obtains and returns a Discovery object from the discovery service.
Expand All @@ -39,6 +44,11 @@ def discover(
environment: The H2O Cloud environment URL to use (e.g. https://cloud.h2o.ai).
discovery_address: The address of the discovery service.
config_path: The path to the H2O CLI configuration file.
http_timeout: The timeout for HTTP requests. Value applies to all of the
timeouts (connect, read, write).
ssl_context: The SSL context to use for HTTPS requests.
If not specified default SSL context is used.
Raises:
exceptions.DiscoveryLookupError: If the URI cannot be determined.
Expand All @@ -47,7 +57,9 @@ def discover(
"""
uri, cfg = _lookup_and_load(environment, discovery_address, config_path)

discovery = load.load_discovery(client.Client(uri))
discovery = load.load_discovery(
client.Client(uri=uri, timeout=http_timeout, ssl_context=ssl_context)
)
credentials = load.load_credentials(
clients=discovery.clients, config_tokens=cfg.tokens
)
Expand All @@ -59,6 +71,9 @@ async def discover_async(
environment: Optional[str] = None,
discovery_address: Optional[str] = None,
config_path: Optional[Union[str, bytes, os.PathLike]] = None,
*,
http_timeout: Optional[datetime.timedelta] = None,
ssl_context: Optional[ssl.SSLContext] = None,
) -> Discovery:
"""Obtains and returns a Discovery object from the discovery service.
Expand All @@ -79,6 +94,10 @@ async def discover_async(
environment: The H2O Cloud environment URL to use (e.g. https://cloud.h2o.ai).
discovery_address: The address of the discovery service.
config_path: The path to the H2O CLI configuration file.
http_timeout: The timeout for HTTP requests. Value applies to all of the
timeouts (connect, read, write).
ssl_context: The SSL context to use for HTTPS requests.
If not specified default SSL context is used.
Raises:
exceptions.DiscoveryLookupError: If the URI cannot be determined.
Expand All @@ -88,7 +107,9 @@ async def discover_async(

uri, cfg = _lookup_and_load(environment, discovery_address, config_path)

discovery = await load.load_discovery_async(client.AsyncClient(uri))
discovery = await load.load_discovery_async(
client.AsyncClient(uri=uri, timeout=http_timeout, ssl_context=ssl_context)
)
credentials = load.load_credentials(
clients=discovery.clients, config_tokens=cfg.tokens
)
Expand Down
10 changes: 1 addition & 9 deletions src/h2o_discovery/_internal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import ssl
from typing import List
from typing import Optional
from typing import Union

import httpx

Expand All @@ -23,26 +22,19 @@ def __init__(
ssl_context: Optional[ssl.SSLContext] = None,
):
self._uri = uri
self._ssl_context = ssl_context
self._verify = ssl_context or ssl.create_default_context()

self._timeout = 5.0
if timeout is not None:
self._timeout = timeout.total_seconds()

self._verify: Union[bool, ssl.SSLContext] = True
if self._ssl_context is not None:
self._verify = self._ssl_context


class Client(_BaseClient):
"""Synchronous Implementation of the Discovery Service API.
Listing methods do pagination and always return all of the available objects.
"""

def __init__(self, uri: str):
self._uri = uri

def get_environment(self) -> model.Environment:
"""Returns the information about the environment."""
with httpx.Client(base_url=self._uri) as client:
Expand Down

0 comments on commit f63a371

Please sign in to comment.