Skip to content

Commit

Permalink
Merge pull request #173 from dvonthenen/restructure-project-part3
Browse files Browse the repository at this point in the history
Restructure Python v3 (Part 3) - Separation of Concerns
  • Loading branch information
dvonthenen committed Nov 15, 2023
2 parents 6d60fbd + 82362cc commit c7f9b0d
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 81 deletions.
10 changes: 5 additions & 5 deletions deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

# entry point for the deepgram python sdk
from .deepgram_client import DeepgramClient
from .types.deepgram_client_options import DeepgramClientOptions
from .deepgram_client_options import DeepgramClientOptions
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError, DeepgramUnknownError

# live
from .types.transcription_options import LiveOptions
from .enums import LiveTranscriptionEvents
from .clients.live.transcription_options import LiveOptions
from .clients.live.enums import LiveTranscriptionEvents

# prerecorded
from .types.transcription_options import PrerecordedOptions
from .clients.prerecorded.transcription_options import PrerecordedOptions

# manage
from .clients.manage_client import ManageClient
from .clients.manage.manage_client import ManageClient
2 changes: 1 addition & 1 deletion deepgram/clients/abstract_restful_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import httpx
import json
from typing import Dict, Any, Optional

from ..errors import DeepgramApiError, DeepgramUnknownApiError, DeepgramUnknownError
from ..helpers import is_json


class AbstractRestfulClient:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .prerecorded_client import PreRecordedClient
from .live_client import LiveClient
from .prerecorded.prerecorded_client import PreRecordedClient
from .live.live_client import LiveClient
from typing import Dict, Any, Optional


Expand Down
File renamed without changes.
27 changes: 0 additions & 27 deletions deepgram/helpers.py → deepgram/clients/live/helpers.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
import json
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
from .types.prerecorded_source import PrerecordedSource

def strip_trailing_slash(url):
parsed_url = urlparse(url)
path = parsed_url.path.rstrip('/')
modified_url = parsed_url._replace(path=path)
return urlunparse(modified_url)

url = "https://example.com/path/"
stripped_url = strip_trailing_slash(url)

def is_buffer_source(provided_source: PrerecordedSource) -> bool:
return "buffer" in provided_source

def is_readstream_source(provided_source: PrerecordedSource) -> bool:
return "stream" in provided_source

def is_url_source(provided_source: PrerecordedSource) -> bool:
return "url" in provided_source

def is_json(myjson):
try:
json_object = json.loads(myjson)
except ValueError as e:
return False
return True

def append_query_params(url, params=""):
parsed_url = urlparse(url)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from deepgram.enums import LiveTranscriptionEvents
from .enums import LiveTranscriptionEvents
from .helpers import convert_to_websocket_url, append_query_params

from deepgram.errors import DeepgramApiError
from deepgram.helpers import convert_to_websocket_url, append_query_params

import asyncio
import json
import websockets
Expand Down
25 changes: 25 additions & 0 deletions deepgram/clients/live/transcription_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Union, List, TypedDict

class LiveOptions(TypedDict, total=False):
callback: str
channels: int
diarize: bool
encoding: str
endpointing: int
interim_results: bool
keywords: str
language: str
model: str
multichannel: bool
numerals: bool
punctuate: bool
profanity_filter: bool
redact: bool
replace: str
sample_rate: int
search: str
smart_format: bool
tag: list
tier: str
version: str

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .abstract_restful_client import AbstractRestfulClient
from ..types.management import Project, ProjectsResponse, Message, ProjectOptions, KeysResponse, Key, KeyOptions, CreateKeyResponse, MembersResponse, ScopesResponse, ScopeOptions, InvitesResponse, InviteOptions, UsageRequestsResponse, UsageRequestOptions, UsageRequest, UsageSummaryOptions, UsageSummaryResponse, UsageFieldsResponse, UsageFieldsOptions, BalancesResponse, Balance
from .management import Project, ProjectsResponse, Message, ProjectOptions, KeysResponse, Key, KeyOptions, CreateKeyResponse, MembersResponse, ScopesResponse, ScopeOptions, InvitesResponse, InviteOptions, UsageRequestsResponse, UsageRequestOptions, UsageRequest, UsageSummaryOptions, UsageSummaryResponse, UsageFieldsResponse, UsageFieldsOptions, BalancesResponse, Balance

from ..abstract_restful_client import AbstractRestfulClient

class ManageClient(AbstractRestfulClient):
"""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .abstract_restful_client import AbstractRestfulClient
from ..abstract_restful_client import AbstractRestfulClient

class OnPremClient(AbstractRestfulClient):
"""
Expand Down
10 changes: 10 additions & 0 deletions deepgram/clients/prerecorded/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .prerecorded_source import PrerecordedSource

def is_buffer_source(provided_source: PrerecordedSource) -> bool:
return "buffer" in provided_source

def is_readstream_source(provided_source: PrerecordedSource) -> bool:
return "stream" in provided_source

def is_url_source(provided_source: PrerecordedSource) -> bool:
return "url" in provided_source
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from ..errors import DeepgramError
from .abstract_restful_client import AbstractRestfulClient
from ..helpers import is_buffer_source, is_readstream_source, is_url_source
from ..types.prerecorded_source import UrlSource, FileSource
from ..types.transcription_options import PrerecordedOptions
from ..types.prerecorded_response import AsyncPrerecordedResponse
from ..types.prerecorded_response import SyncPrerecordedResponse
from ...errors import DeepgramError
from ..abstract_restful_client import AbstractRestfulClient

from .helpers import is_buffer_source, is_readstream_source, is_url_source
from .prerecorded_source import UrlSource, FileSource
from .transcription_options import PrerecordedOptions
from .prerecorded_response import AsyncPrerecordedResponse, SyncPrerecordedResponse


class PreRecordedClient(AbstractRestfulClient):
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,3 @@ class PrerecordedOptions(TypedDict, total=False):
utt_split: int
utterances: bool
version: str


class LiveOptions(TypedDict, total=False):
callback: str
channels: int
diarize: bool
encoding: str
endpointing: int
interim_results: bool
keywords: str
language: str
model: str
multichannel: bool
numerals: bool
punctuate: bool
profanity_filter: bool
redact: bool
replace: str
sample_rate: int
search: str
smart_format: bool
tag: list
tier: str
version: str

16 changes: 9 additions & 7 deletions deepgram/deepgram_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import re
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
from typing import Optional
from .clients.listen_client import ListenClient
from .clients.manage_client import ManageClient
from .clients.onprem_client import OnPremClient
from .helpers import strip_trailing_slash
from .types.deepgram_client_options import DeepgramClientOptions

from .deepgram_client_options import DeepgramClientOptions
from .errors import DeepgramError

from .clients.listen import ListenClient
from .clients.manage.manage_client import ManageClient
from .clients.onprem.onprem_client import OnPremClient

class DeepgramClient:
"""
Represents a client for interacting with the Deepgram API.
Expand Down Expand Up @@ -51,7 +53,7 @@ def _get_url(self, config_options):
url = config_options['url']
if not re.match(r'^https?://', url, re.IGNORECASE):
url = 'https://' + url
return strip_trailing_slash(url)
return url.strip('/')

@property
def listen(self):
Expand All @@ -63,4 +65,4 @@ def manage(self):

@property
def onprem(self):
return OnPremClient(self.url, self.headers)
return OnPremClient(self.url, self.headers)
File renamed without changes.
4 changes: 3 additions & 1 deletion examples/demo_live.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from deepgram import DeepgramClient, LiveTranscriptionEvents, LiveOptions
from dotenv import load_dotenv
import asyncio
import aiohttp
import os

from deepgram import DeepgramClient, LiveTranscriptionEvents, LiveOptions

load_dotenv()

base_url = "api.beta.deepgram.com" # will change to https://api.beta.deepgram.com
Expand Down
1 change: 0 additions & 1 deletion examples/demo_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# Create a Deepgram client using the API key
deepgram: DeepgramClient = DeepgramClient(API_KEY)


async def main():
response = await deepgram.manage.get_projects()
# response = await deepgram.manage.get_project(PROJECT_ID)
Expand Down

0 comments on commit c7f9b0d

Please sign in to comment.