Skip to content

Commit e5e7f4d

Browse files
committed
Add samples module for demo client code
1 parent 1821b49 commit e5e7f4d

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed

src/neuralaudio/samples/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file was auto-generated by Fern from our API Definition.
2+

src/neuralaudio/samples/client.py

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
# This file was auto-generated by Fern from our API Definition.
2+
3+
from ..core.client_wrapper import SyncClientWrapper
4+
import typing
5+
from ..core.request_options import RequestOptions
6+
from ..core.jsonable_encoder import jsonable_encoder
7+
from ..core.unchecked_base_model import construct_type
8+
from ..errors.unprocessable_entity_error import UnprocessableEntityError
9+
from ..types.http_validation_error import HttpValidationError
10+
from json.decoder import JSONDecodeError
11+
from ..core.api_error import ApiError
12+
from ..core.client_wrapper import AsyncClientWrapper
13+
14+
15+
class SamplesClient:
16+
def __init__(self, *, client_wrapper: SyncClientWrapper):
17+
self._client_wrapper = client_wrapper
18+
19+
def delete(
20+
self, voice_id: str, sample_id: str, *, request_options: typing.Optional[RequestOptions] = None
21+
) -> typing.Optional[typing.Any]:
22+
"""
23+
Removes a sample by its ID.
24+
25+
Parameters
26+
----------
27+
voice_id : str
28+
Voice ID to be used, you can use https://api.neuralaudio.solutions/v1/voices to list all the available voices.
29+
30+
sample_id : str
31+
Sample ID to be used, you can use GET https://api.neuralaudio.solutions/v1/voices/{voice_id} to list all the available samples for a voice.
32+
33+
request_options : typing.Optional[RequestOptions]
34+
Request-specific configuration.
35+
36+
Returns
37+
-------
38+
typing.Optional[typing.Any]
39+
Successful Response
40+
41+
Examples
42+
--------
43+
from neuralaudio import NeuralAudio
44+
45+
client = NeuralAudio(
46+
api_key="YOUR_API_KEY",
47+
)
48+
client.samples.delete(
49+
voice_id="VOICE_ID",
50+
sample_id="SAMPLE_ID",
51+
)
52+
"""
53+
_response = self._client_wrapper.httpx_client.request(
54+
f"v1/voices/{jsonable_encoder(voice_id)}/samples/{jsonable_encoder(sample_id)}",
55+
method="DELETE",
56+
request_options=request_options,
57+
)
58+
try:
59+
if 200 <= _response.status_code < 300:
60+
return typing.cast(
61+
typing.Optional[typing.Any],
62+
construct_type(
63+
type_=typing.Optional[typing.Any], # type: ignore
64+
object_=_response.json(),
65+
),
66+
)
67+
if _response.status_code == 422:
68+
raise UnprocessableEntityError(
69+
typing.cast(
70+
HttpValidationError,
71+
construct_type(
72+
type_=HttpValidationError, # type: ignore
73+
object_=_response.json(),
74+
),
75+
)
76+
)
77+
_response_json = _response.json()
78+
except JSONDecodeError:
79+
raise ApiError(status_code=_response.status_code, body=_response.text)
80+
raise ApiError(status_code=_response.status_code, body=_response_json)
81+
82+
def get_audio(
83+
self, voice_id: str, sample_id: str, *, request_options: typing.Optional[RequestOptions] = None
84+
) -> typing.Iterator[bytes]:
85+
"""
86+
Returns the audio corresponding to a sample attached to a voice.
87+
88+
Parameters
89+
----------
90+
voice_id : str
91+
Voice ID to be used, you can use https://api.neuralaudio.solutions/v1/voices to list all the available voices.
92+
93+
sample_id : str
94+
Sample ID to be used, you can use GET https://api.neuralaudio.solutions/v1/voices/{voice_id} to list all the available samples for a voice.
95+
96+
request_options : typing.Optional[RequestOptions]
97+
Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.
98+
99+
Yields
100+
------
101+
typing.Iterator[bytes]
102+
Successful Response
103+
104+
Examples
105+
--------
106+
from neuralaudio import NeuralAudio
107+
108+
client = NeuralAudio(
109+
api_key="YOUR_API_KEY",
110+
)
111+
client.samples.get_audio(
112+
voice_id="VOICE_ID",
113+
sample_id="SAMPLE_ID",
114+
)
115+
"""
116+
with self._client_wrapper.httpx_client.stream(
117+
f"v1/voices/{jsonable_encoder(voice_id)}/samples/{jsonable_encoder(sample_id)}/audio",
118+
method="GET",
119+
request_options=request_options,
120+
) as _response:
121+
try:
122+
if 200 <= _response.status_code < 300:
123+
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
124+
for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
125+
yield _chunk
126+
return
127+
_response.read()
128+
if _response.status_code == 422:
129+
raise UnprocessableEntityError(
130+
typing.cast(
131+
HttpValidationError,
132+
construct_type(
133+
type_=HttpValidationError, # type: ignore
134+
object_=_response.json(),
135+
),
136+
)
137+
)
138+
_response_json = _response.json()
139+
except JSONDecodeError:
140+
raise ApiError(status_code=_response.status_code, body=_response.text)
141+
raise ApiError(status_code=_response.status_code, body=_response_json)
142+
143+
144+
class AsyncSamplesClient:
145+
def __init__(self, *, client_wrapper: AsyncClientWrapper):
146+
self._client_wrapper = client_wrapper
147+
148+
async def delete(
149+
self, voice_id: str, sample_id: str, *, request_options: typing.Optional[RequestOptions] = None
150+
) -> typing.Optional[typing.Any]:
151+
"""
152+
Removes a sample by its ID.
153+
154+
Parameters
155+
----------
156+
voice_id : str
157+
Voice ID to be used, you can use https://api.neuralaudio.solutions/v1/voices to list all the available voices.
158+
159+
sample_id : str
160+
Sample ID to be used, you can use GET https://api.neuralaudio.solutions/v1/voices/{voice_id} to list all the available samples for a voice.
161+
162+
request_options : typing.Optional[RequestOptions]
163+
Request-specific configuration.
164+
165+
Returns
166+
-------
167+
typing.Optional[typing.Any]
168+
Successful Response
169+
170+
Examples
171+
--------
172+
import asyncio
173+
174+
from neuralaudio import AsyncNeuralAudio
175+
176+
client = AsyncNeuralAudio(
177+
api_key="YOUR_API_KEY",
178+
)
179+
180+
181+
async def main() -> None:
182+
await client.samples.delete(
183+
voice_id="VOICE_ID",
184+
sample_id="SAMPLE_ID",
185+
)
186+
187+
188+
asyncio.run(main())
189+
"""
190+
_response = await self._client_wrapper.httpx_client.request(
191+
f"v1/voices/{jsonable_encoder(voice_id)}/samples/{jsonable_encoder(sample_id)}",
192+
method="DELETE",
193+
request_options=request_options,
194+
)
195+
try:
196+
if 200 <= _response.status_code < 300:
197+
return typing.cast(
198+
typing.Optional[typing.Any],
199+
construct_type(
200+
type_=typing.Optional[typing.Any], # type: ignore
201+
object_=_response.json(),
202+
),
203+
)
204+
if _response.status_code == 422:
205+
raise UnprocessableEntityError(
206+
typing.cast(
207+
HttpValidationError,
208+
construct_type(
209+
type_=HttpValidationError, # type: ignore
210+
object_=_response.json(),
211+
),
212+
)
213+
)
214+
_response_json = _response.json()
215+
except JSONDecodeError:
216+
raise ApiError(status_code=_response.status_code, body=_response.text)
217+
raise ApiError(status_code=_response.status_code, body=_response_json)
218+
219+
async def get_audio(
220+
self, voice_id: str, sample_id: str, *, request_options: typing.Optional[RequestOptions] = None
221+
) -> typing.AsyncIterator[bytes]:
222+
"""
223+
Returns the audio corresponding to a sample attached to a voice.
224+
225+
Parameters
226+
----------
227+
voice_id : str
228+
Voice ID to be used, you can use https://api.neuralaudio.solutions/v1/voices to list all the available voices.
229+
230+
sample_id : str
231+
Sample ID to be used, you can use GET https://api.neuralaudio.solutions/v1/voices/{voice_id} to list all the available samples for a voice.
232+
233+
request_options : typing.Optional[RequestOptions]
234+
Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.
235+
236+
Yields
237+
------
238+
typing.AsyncIterator[bytes]
239+
Successful Response
240+
241+
Examples
242+
--------
243+
import asyncio
244+
245+
from neuralaudio import AsyncNeuralAudio
246+
247+
client = AsyncNeuralAudio(
248+
api_key="YOUR_API_KEY",
249+
)
250+
251+
252+
async def main() -> None:
253+
await client.samples.get_audio(
254+
voice_id="VOICE_ID",
255+
sample_id="SAMPLE_ID",
256+
)
257+
258+
259+
asyncio.run(main())
260+
"""
261+
async with self._client_wrapper.httpx_client.stream(
262+
f"v1/voices/{jsonable_encoder(voice_id)}/samples/{jsonable_encoder(sample_id)}/audio",
263+
method="GET",
264+
request_options=request_options,
265+
) as _response:
266+
try:
267+
if 200 <= _response.status_code < 300:
268+
_chunk_size = request_options.get("chunk_size", 1024) if request_options is not None else 1024
269+
async for _chunk in _response.aiter_bytes(chunk_size=_chunk_size):
270+
yield _chunk
271+
return
272+
await _response.aread()
273+
if _response.status_code == 422:
274+
raise UnprocessableEntityError(
275+
typing.cast(
276+
HttpValidationError,
277+
construct_type(
278+
type_=HttpValidationError, # type: ignore
279+
object_=_response.json(),
280+
),
281+
)
282+
)
283+
_response_json = _response.json()
284+
except JSONDecodeError:
285+
raise ApiError(status_code=_response.status_code, body=_response.text)
286+
raise ApiError(status_code=_response.status_code, body=_response_json)

0 commit comments

Comments
 (0)