Skip to content

Commit de08b42

Browse files
committed
Add base base_url
1 parent 145d2df commit de08b42

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

src/hume/client.py

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,44 @@
99
from .environment import HumeClientEnvironment
1010

1111

12+
def _base_url_to_environment(base_url: str) -> HumeClientEnvironment:
13+
if base_url.startswith("http://"):
14+
return HumeClientEnvironment(
15+
base=base_url,
16+
evi=base_url.replace("http://", "ws://") + "/v0/evi",
17+
tts=base_url.replace("http://", "ws://") + "/v0/tts",
18+
stream=base_url.replace("http://", "ws://") + "/v0/stream",
19+
)
20+
elif base_url.startswith("https://"):
21+
return HumeClientEnvironment(
22+
base=base_url,
23+
evi=base_url.replace("https://", "wss://") + "/v0/evi",
24+
tts=base_url.replace("https://", "wss://") + "/v0/tts",
25+
stream=base_url.replace("https://", "wss://") + "/v0/stream",
26+
)
27+
else:
28+
# Assume https if no protocol specified
29+
return HumeClientEnvironment(
30+
base="https://" + base_url,
31+
evi="wss://" + base_url + "/v0/evi",
32+
tts="wss://" + base_url + "/v0/tts",
33+
stream="wss://" + base_url + "/v0/stream",
34+
)
35+
36+
1237
class HumeClient(BaseHumeClient):
1338
"""
1439
Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propogate to these functions.
1540
1641
Parameters
1742
----------
1843
base_url : typing.Optional[str]
19-
The base url to use for requests from the client.
20-
21-
environment : HumeClientEnvironment
44+
The base URL to use for requests from the client. If provided, this will be converted
45+
to a HumeClientEnvironment. Can be a full URL (http://... or https://...) or just
46+
a hostname (which will default to https://).
47+
environment : typing.Optional[HumeClientEnvironment]
2248
The environment to use for requests from the client. from .environment import HumeClientEnvironment
23-
24-
25-
26-
Defaults to HumeClientEnvironment.PROD
27-
28-
29-
49+
Defaults to None, which will use HumeClientEnvironment.PROD. Cannot be specified together with base_url.
3050
api_key : typing.Optional[str]
3151
timeout : typing.Optional[float]
3252
The timeout to be used, in seconds, for requests by default the timeout is 60 seconds, unless a custom httpx client is used, in which case a default is not set.
@@ -49,18 +69,27 @@ class HumeClient(BaseHumeClient):
4969
def __init__(
5070
self,
5171
*,
52-
# TODO: this was removed by the generator?
53-
# base_url: typing.Optional[str] = None,
54-
environment: HumeClientEnvironment = HumeClientEnvironment.PROD,
72+
base_url: typing.Optional[str] = None,
73+
environment: typing.Optional[HumeClientEnvironment] = None,
5574
api_key: typing.Optional[str] = None,
5675
headers: typing.Optional[typing.Dict[str, str]] = None,
5776
timeout: typing.Optional[float] = None,
5877
follow_redirects: typing.Optional[bool] = True,
5978
httpx_client: typing.Optional[httpx.Client] = None
6079
):
80+
# Error if both base_url and environment are specified
81+
if base_url is not None and environment is not None:
82+
raise ValueError("Cannot specify both 'base_url' and 'environment'. Please use only one.")
83+
84+
# Convert base_url string to environment if provided
85+
if base_url is not None:
86+
environment = _base_url_to_environment(base_url)
87+
88+
# Default to PROD if neither base_url nor environment was provided
89+
if environment is None:
90+
environment = HumeClientEnvironment.PROD
91+
6192
super().__init__(
62-
# TODO: this was removed by the generator?
63-
# base_url=base_url,
6493
environment=environment,
6594
api_key=api_key,
6695
headers=headers,
@@ -76,10 +105,12 @@ class AsyncHumeClient(AsyncBaseHumeClient):
76105
Parameters
77106
----------
78107
base_url : typing.Optional[str]
79-
The base url to use for requests from the client.
80-
environment : HumeClientEnvironment
108+
The base URL to use for requests from the client. If provided, this will be converted
109+
to a HumeClientEnvironment. Can be a full URL (http://... or https://...) or just
110+
a hostname (which will default to https://).
111+
environment : typing.Optional[HumeClientEnvironment]
81112
The environment to use for requests from the client. from .environment import HumeClientEnvironment
82-
Defaults to HumeClientEnvironment.PROD
113+
Defaults to None, which will use HumeClientEnvironment.PROD. Cannot be specified together with base_url.
83114
api_key : typing.Optional[str]
84115
timeout : typing.Optional[float]
85116
The timeout to be used, in seconds, for requests by default the timeout is 60 seconds, unless a custom httpx client is used, in which case a default is not set.
@@ -98,18 +129,27 @@ class AsyncHumeClient(AsyncBaseHumeClient):
98129
def __init__(
99130
self,
100131
*,
101-
# TODO: this was removed by the generator?
102-
# base_url: typing.Optional[str] = None,
103-
environment: HumeClientEnvironment = HumeClientEnvironment.PROD,
132+
base_url: typing.Optional[str] = None,
133+
environment: typing.Optional[HumeClientEnvironment] = None,
104134
headers: typing.Optional[typing.Dict[str, str]] = None,
105135
api_key: typing.Optional[str] = None,
106136
timeout: typing.Optional[float] = None,
107137
follow_redirects: typing.Optional[bool] = True,
108138
httpx_client: typing.Optional[httpx.AsyncClient] = None
109139
):
140+
# Error if both base_url and environment are specified
141+
if base_url is not None and environment is not None:
142+
raise ValueError("Cannot specify both 'base_url' and 'environment'. Please use only one.")
143+
144+
# Convert base_url string to environment if provided
145+
if base_url is not None:
146+
environment = _base_url_to_environment(base_url)
147+
148+
# Default to PROD if neither base_url nor environment was provided
149+
if environment is None:
150+
environment = HumeClientEnvironment.PROD
151+
110152
super().__init__(
111-
# TODO: this was removed by the generator?
112-
# base_url=base_url,
113153
environment=environment,
114154
api_key=api_key,
115155
headers=headers,

0 commit comments

Comments
 (0)