Skip to content

Commit

Permalink
Fix Some Query Params Not Getting Appended
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvonthenen committed Dec 13, 2023
1 parent 4bd375b commit b3c5460
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 137 deletions.
13 changes: 9 additions & 4 deletions deepgram/clients/abstract_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ..options import DeepgramClientOptions
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError
from .helpers import append_query_params


class AbstractAsyncRestClient:
Expand Down Expand Up @@ -57,13 +58,17 @@ async def patch(self, url: str, options=None, **kwargs):
"PATCH", url, params=options, headers=self.config.headers, **kwargs
)

async def delete(self, url: str):
return await self._handle_request("DELETE", url, headers=self.config.headers)
async def delete(self, url: str, options=None):
return await self._handle_request("DELETE", url, params=options, headers=self.config.headers)

async def _handle_request(self, method, url, params, headers, **kwargs):
new_url = url
if params is not None:
new_url = append_query_params(new_url, params)

async def _handle_request(self, method, url, **kwargs):
try:
with httpx.Client() as client:
response = client.request(method, url, **kwargs)
response = client.request(method, new_url, headers=headers, **kwargs)
response.raise_for_status()
return response.text
except httpx._exceptions.HTTPError as e:
Expand Down
14 changes: 10 additions & 4 deletions deepgram/clients/abstract_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import httpx
import json

from .helpers import append_query_params
from ..options import DeepgramClientOptions
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError

Expand Down Expand Up @@ -56,15 +57,20 @@ def patch(self, url: str, options=None, timeout=None, **kwargs):
"PATCH", url, params=options, headers=self.config.headers, timeout=timeout, **kwargs
)

def delete(self, url: str, timeout=None):
return self._handle_request("DELETE", url, headers=self.config.headers, timeout=timeout)
def delete(self, url: str, options=None, timeout=None):
return self._handle_request("DELETE", url, params=options, headers=self.config.headers, timeout=timeout)

def _handle_request(self, method, url, params, headers, timeout, **kwargs):
new_url = url
if params is not None:
new_url = append_query_params(url, params)

def _handle_request(self, method, url, timeout, **kwargs):
if timeout is None:
timeout = httpx.Timeout(10.0, connect=10.0)

try:
with httpx.Client(timeout=timeout) as client:
response = client.request(method, url, **kwargs)
response = client.request(method, new_url, headers=headers, **kwargs)
response.raise_for_status()
return response.text
except httpx._exceptions.HTTPError as e:
Expand Down
24 changes: 24 additions & 0 deletions deepgram/clients/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

from urllib.parse import urlparse, urlunparse, parse_qs, urlencode

def append_query_params(url, params):
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)

for key, value in params.items():
if value is None:
continue
if isinstance(value, bool):
value = str(value).lower()
if isinstance(value, list):
for item in value:
query_params[key] = query_params.get(key, []) + [str(item)]
else:
query_params[key] = [str(value)]

updated_query_string = urlencode(query_params, doseq=True)
updated_url = parsed_url._replace(query=updated_query_string).geturl()
return updated_url
166 changes: 94 additions & 72 deletions deepgram/clients/manage/v1/client.py

Large diffs are not rendered by default.

39 changes: 27 additions & 12 deletions deepgram/clients/prerecorded/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

import json
import logging, verboselogs

from ...abstract_async_client import AbstractAsyncRestClient
Expand Down Expand Up @@ -51,7 +52,7 @@ async def transcribe_url(
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_url ENTER")

if options is not None and "callback" in options:
if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
return await self.transcribe_url_callback(
source, options["callback"], options, endpoint
Expand All @@ -68,7 +69,12 @@ async def transcribe_url(
self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
self.logger.info("options: %s", options)
res = PrerecordedResponse.from_json(await self.post(url, options, json=body))
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = json.loads(options.to_json())
result = await self.post(url, options=options, json=body)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_url succeeded")
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
Expand Down Expand Up @@ -115,9 +121,12 @@ async def transcribe_url_callback(
self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
self.logger.info("options: %s", options)
json = await self.post(url, options, json=body)
self.logger.info("json: %s", json)
res = AsyncPrerecordedResponse.from_json(json)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = json.loads(options.to_json())
result = await self.post(url, options=options, json=body)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_url_callback succeeded")
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
Expand Down Expand Up @@ -149,7 +158,7 @@ async def transcribe_file(
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_file ENTER")

if options is not None and "callback" in options:
if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
return await self.transcribe_file_callback(
source, options["callback"], options, endpoint
Expand All @@ -167,9 +176,12 @@ async def transcribe_file(

self.logger.info("url: %s", url)
self.logger.info("options: %s", options)
json = await self.post(url, options, content=body)
self.logger.info("json: %s", json)
res = PrerecordedResponse.from_json(json)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = json.loads(options.to_json())
result = await self.post(url, options=options, content=body)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_file succeeded")
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
Expand Down Expand Up @@ -217,9 +229,12 @@ async def transcribe_file_callback(

self.logger.info("url: %s", url)
self.logger.info("options: %s", options)
json = await self.post(url, options, json=body)
self.logger.info("json: %s", json)
res = AsyncPrerecordedResponse.from_json(json)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = json.loads(options.to_json())
result = await self.post(url, options=options, json=body)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_file_callback succeeded")
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
Expand Down
44 changes: 27 additions & 17 deletions deepgram/clients/prerecorded/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import httpx
import logging, verboselogs
import inspect
import json

from ...abstract_sync_client import AbstractSyncRestClient
from ..errors import DeepgramTypeError
Expand Down Expand Up @@ -53,12 +53,14 @@ def transcribe_url(
endpoint: str = "v1/listen",
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_url ENTER")
url = f"{self.config.url}/{endpoint}"
if options is not None and "callback" in options:

if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
return self.transcribe_url_callback(
source, options["callback"], options, endpoint
)

url = f"{self.config.url}/{endpoint}"
if is_url_source(source):
body = source
else:
Expand All @@ -71,8 +73,10 @@ def transcribe_url(
self.logger.info("options: %s", options)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = options.to_json()
res = PrerecordedResponse.from_json(self.post(url, options, json=body, timeout=timeout))
options = json.loads(options.to_json())
result = self.post(url, options=options, json=body, timeout=timeout)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_url succeeded")
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
Expand Down Expand Up @@ -122,10 +126,10 @@ def transcribe_url_callback(
self.logger.info("options: %s", options)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = options.to_json()
json = self.post(url, options, json=body, timeout=timeout)
self.logger.info("json: %s", json)
res = AsyncPrerecordedResponse.from_json(json)
options = json.loads(options.to_json())
result = self.post(url, options=options, json=body, timeout=timeout)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_url_callback succeeded")
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
Expand Down Expand Up @@ -158,6 +162,12 @@ def transcribe_file(
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_file ENTER")

if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
return self.transcribe_file_callback(
source, options["callback"], options, endpoint
)

url = f"{self.config.url}/{endpoint}"
if is_buffer_source(source):
body = source["buffer"]
Expand All @@ -172,10 +182,10 @@ def transcribe_file(
self.logger.info("options: %s", options)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = options.to_json()
json = self.post(url, options, content=body, timeout=timeout)
self.logger.info("json: %s", json)
res = PrerecordedResponse.from_json(json)
options = json.loads(options.to_json())
result = self.post(url, options=options, content=body, timeout=timeout)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_file succeeded")
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
Expand Down Expand Up @@ -226,10 +236,10 @@ def transcribe_file_callback(
self.logger.info("options: %s", options)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
options = options.to_json()
json = self.post(url, options, json=body, timeout=timeout)
self.logger.info("json: %s", json)
res = AsyncPrerecordedResponse.from_json(json)
options = json.loads(options.to_json())
result = self.post(url, options=options, json=body, timeout=timeout)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
self.logger.notice("transcribe_file_callback succeeded")
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/async_invitations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
API_KEY = os.getenv("DG_API_KEY")

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient(API_KEY)
deepgram = DeepgramClient()


async def main():
Expand Down
4 changes: 2 additions & 2 deletions examples/manage/balances/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
load_dotenv()

# Create a Deepgram client using the API key
config: DeepgramClientOptions = DeepgramClientOptions(
config = DeepgramClientOptions(
verbose=logging.SPAM,
)

deepgram: DeepgramClient = DeepgramClient("", config)
deepgram = DeepgramClient("", config)


def main():
Expand Down
4 changes: 2 additions & 2 deletions examples/manage/invitations/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
load_dotenv()

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down Expand Up @@ -39,7 +39,7 @@ def main():
print(f"GetInvites() - Name: {invite.email}, Amount: {invite.scope}")

# send invite
options: InviteOptions = {"email": "[email protected]", "scope": "member"}
options = InviteOptions(email="[email protected]", scope="member")

getResp = deepgram.manage.v("1").send_invite_options(myId, options)
print(f"SendInvite() - Msg: {getResp.message}")
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/keys/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
load_dotenv()

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/members/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DELETE_MEMBER_BY_EMAIL = "[email protected]"

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/projects/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DELETE_PROJECT_BY_NAME = os.getenv("DG_DELETE_PROJECT_BY_NAME")

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/scopes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
MEMBER_BY_EMAIL = "[email protected]"

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/usage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
load_dotenv()

# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient()
deepgram = DeepgramClient()


def main():
Expand Down
10 changes: 5 additions & 5 deletions examples/prerecorded/async_url/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"url": "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"
}

options: PrerecordedOptions = {
"model": "nova",
"smart_format": "true",
"summarize": "v2",
}
options = PrerecordedOptions(
model="nova",
smart_format=True,
summarize="v2",
)

# STEP 1 Create a Deepgram client using the API key (optional - add config options)
deepgram = DeepgramClient(API_KEY)
Expand Down
6 changes: 4 additions & 2 deletions examples/prerecorded/file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

options = PrerecordedOptions(
model="nova",
smart_format="true",
summarize="v2",
smart_format=True,
utterances=True,
punctuate=True,
diarize=True,
)

# STEP 1 Create a Deepgram client using the API key (optional - add config options)
Expand Down
Loading

0 comments on commit b3c5460

Please sign in to comment.