Skip to content

Commit

Permalink
Update All Files Using Black Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Nov 28, 2023
1 parent 11c293c commit 2d9c589
Show file tree
Hide file tree
Showing 41 changed files with 836 additions and 493 deletions.
21 changes: 18 additions & 3 deletions deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

# version
__version__ = '3.0.0'
__version__ = "3.0.0"

# entry point for the deepgram python sdk
from .client import DeepgramClient, DeepgramApiKeyError
Expand All @@ -14,10 +14,25 @@
from .clients.live.client import LiveClient, LegacyLiveClient, LiveOptions

# prerecorded
from .clients.prerecorded.client import PreRecordedClient, PrerecordedOptions, PrerecordedSource, FileSource, UrlSource
from .clients.prerecorded.client import (
PreRecordedClient,
PrerecordedOptions,
PrerecordedSource,
FileSource,
UrlSource,
)

# manage
from .clients.manage.client import ManageClient, ProjectOptions, KeyOptions, ScopeOptions, InviteOptions, UsageRequestOptions, UsageSummaryOptions, UsageFieldsOptions
from .clients.manage.client import (
ManageClient,
ProjectOptions,
KeyOptions,
ScopeOptions,
InviteOptions,
UsageRequestOptions,
UsageSummaryOptions,
UsageFieldsOptions,
)

# utilities
from .audio.microphone.microphone import Microphone
6 changes: 4 additions & 2 deletions deepgram/audio/microphone/errors.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


class DeepgramMicrophoneError(Exception):
"""
Exception raised for known errors related to Microphone library.
Expand All @@ -11,10 +12,11 @@ class DeepgramMicrophoneError(Exception):
status (str): The HTTP status associated with the API error.
original_error (str - json): The original error that was raised.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramMicrophoneError"
self.message = message

def __str__(self):
return f"{self.name}: {self.message}"
return f"{self.name}: {self.message}"
11 changes: 7 additions & 4 deletions deepgram/audio/microphone/microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
RATE = 16000
CHUNK = 8000


class Microphone:
"""
TODO
TODO
"""
def __init__(self, push_callback, format=FORMAT, rate=RATE, chunk=CHUNK, channels=CHANNELS):

def __init__(
self, push_callback, format=FORMAT, rate=RATE, chunk=CHUNK, channels=CHANNELS
):
self.audio = pyaudio.PyAudio()
self.chunk = chunk
self.rate = rate
Expand All @@ -37,7 +41,7 @@ def is_active(self):
def start(self):
if self.stream is not None:
raise DeepgramMicrophoneError("Microphone already started")

self.stream = self.audio.open(
format=self.format,
channels=self.channels,
Expand Down Expand Up @@ -86,4 +90,3 @@ def finish(self):
self.stream.stop_stream()
self.stream.close()
self.stream = None

12 changes: 7 additions & 5 deletions deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .options import DeepgramClientOptions
from .errors import DeepgramApiKeyError, DeepgramModuleError


class DeepgramClient:
"""
Represents a client for interacting with the Deepgram API.
Expand All @@ -31,32 +32,33 @@ class DeepgramClient:
onprem: Returns an OnPremClient instance for interacting with Deepgram's on-premises API.
"""

def __init__(self, api_key: str, config: Optional[DeepgramClientOptions] = None):
if not api_key:
raise DeepgramApiKeyError("Deepgram API key is required")

self.api_key = api_key
if config is None: # Use default configuration
if config is None: # Use default configuration
self.config = DeepgramClientOptions(self.api_key)
else:
config.set_apikey(self.api_key)
self.config = config

@property
def listen(self):
return ListenClient(self.config)

@property
def manage(self):
return self.Version(self.config, "manage")

@property
def onprem(self):
return self.Version(self.config, "onprem")

# INTERNAL CLASSES
class Version:
def __init__(self, config, parent : str):
def __init__(self, config, parent: str):
self.config = config
self.parent = parent

Expand Down
24 changes: 18 additions & 6 deletions deepgram/clients/abstract_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..options import DeepgramClientOptions
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError


class AbstractRestfulClient:
"""
An abstract base class for a RESTful HTTP client.
Expand All @@ -28,6 +29,7 @@ class AbstractRestfulClient:
DeepgramApiError: Raised for known API errors.
DeepgramUnknownApiError: Raised for unknown API errors.
"""

def __init__(self, config: DeepgramClientOptions):
if config is None:
raise DeepgramError("Config are required")
Expand All @@ -36,19 +38,27 @@ def __init__(self, config: DeepgramClientOptions):
self.client = httpx.AsyncClient()

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

async def post(self, url: str, options=None, **kwargs):
return await self._handle_request('POST', url, params=options, headers=self.config.headers, **kwargs)
return await self._handle_request(
"POST", url, params=options, headers=self.config.headers, **kwargs
)

async def put(self, url: str, options=None, **kwargs):
return await self._handle_request('PUT', url, params=options, headers=self.config.headers, **kwargs)
return await self._handle_request(
"PUT", url, params=options, headers=self.config.headers, **kwargs
)

async def patch(self, url: str, options=None, **kwargs):
return await self._handle_request('PATCH', url, params=options, headers=self.config.headers, **kwargs)
return await self._handle_request(
"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)
return await self._handle_request("DELETE", url, headers=self.config.headers)

async def _handle_request(self, method, url, **kwargs):
try:
Expand All @@ -61,7 +71,9 @@ async def _handle_request(self, method, url, **kwargs):
status_code = e.response.status_code or 500
try:
json_object = json.loads(e.response.text)
raise DeepgramApiError(json_object.get('message'), status_code, json.dumps(json_object)) from e
raise DeepgramApiError(
json_object.get("message"), status_code, json.dumps(json_object)
) from e
except json.decoder.JSONDecodeError:
raise DeepgramUnknownApiError(e.response.text, status_code) from e
except ValueError as e:
Expand Down
12 changes: 10 additions & 2 deletions deepgram/clients/errors.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


class DeepgramError(Exception):
"""
Exception raised for unknown errors related to the Deepgram API.
Expand All @@ -10,6 +11,7 @@ class DeepgramError(Exception):
message (str): The error message describing the exception.
status (str): The HTTP status associated with the API error.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramError"
Expand All @@ -18,17 +20,20 @@ def __init__(self, message: str):
def __str__(self):
return f"{self.name}: {self.message}"


class DeepgramModuleError(Exception):
"""
Base class for exceptions raised for a missing Deepgram module.
Attributes:
message (str): The error message describing the exception.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramModuleError"


class DeepgramApiError(Exception):
"""
Exception raised for known errors (in json response format) related to the Deepgram API.
Expand All @@ -38,16 +43,18 @@ class DeepgramApiError(Exception):
status (str): The HTTP status associated with the API error.
original_error (str - json): The original error that was raised.
"""
def __init__(self, message: str, status: str, original_error = None):

def __init__(self, message: str, status: str, original_error=None):
super().__init__(message)
self.name = "DeepgramApiError"
self.status = status
self.message = message
self.original_error = original_error

def __str__(self):
return f"{self.name}: {self.message} (Status: {self.status})"


class DeepgramUnknownApiError(Exception):
"""
Exception raised for unknown errors related to the Deepgram API.
Expand All @@ -56,6 +63,7 @@ class DeepgramUnknownApiError(Exception):
message (str): The error message describing the exception.
status (str): The HTTP status associated with the API error.
"""

def __init__(self, message: str, status: str):
super().__init__(message, status)
self.name = "DeepgramUnknownApiError"
Expand Down
9 changes: 5 additions & 4 deletions deepgram/clients/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@
from .live.client import LiveClient, LegacyLiveClient
from .errors import DeepgramModuleError


class ListenClient:
def __init__(self, config: DeepgramClientOptions):
self.config = config

@property
def prerecorded(self):
return self.Version(self.config, "prerecorded")

@property
def live(self):
return self.Version(self.config, "live")

@property
def legacylive(self):
return LegacyLiveClient(self.config)

# INTERNAL CLASSES
class Version:
def __init__(self, config, parent : str):
def __init__(self, config, parent: str):
self.config = config
self.parent = parent

Expand Down
25 changes: 15 additions & 10 deletions deepgram/clients/live/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
from .v1.legacy_client import LegacyLiveClient as LegacyLiveClientLatest
from .v1.options import LiveOptions as LiveOptionsLatest

'''
"""
The client.py points to the current supported version in the SDK.
Older versions are supported in the SDK for backwards compatibility.
'''
"""


class LiveOptions(LiveOptionsLatest):
pass
pass


class LiveClient(LiveClientLatest):
"""
"""
Please see LiveClientLatest for details
"""
def __init__(self, config):
super().__init__(config)

def __init__(self, config):
super().__init__(config)


class LegacyLiveClient(LegacyLiveClientLatest):
"""
"""
Please see LiveClientLatest for details
"""
def __init__(self, config):
super().__init__(config)


def __init__(self, config):
super().__init__(config)
1 change: 1 addition & 0 deletions deepgram/clients/live/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from enum import Enum


class LiveTranscriptionEvents(Enum):
Open = "open"
Close = "close"
Expand Down
6 changes: 5 additions & 1 deletion deepgram/clients/live/errors.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


class DeepgramError(Exception):
"""
Exception raised for unknown errors related to the Deepgram API.
Expand All @@ -10,6 +11,7 @@ class DeepgramError(Exception):
message (str): The error message describing the exception.
status (str): The HTTP status associated with the API error.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramError"
Expand All @@ -18,6 +20,7 @@ def __init__(self, message: str):
def __str__(self):
return f"{self.name}: {self.message}"


class DeepgramWebsocketError(Exception):
"""
Exception raised for known errors related to Websocket library.
Expand All @@ -27,10 +30,11 @@ class DeepgramWebsocketError(Exception):
status (str): The HTTP status associated with the API error.
original_error (str - json): The original error that was raised.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramWebsocketError"
self.message = message

def __str__(self):
return f"{self.name}: {self.message}"
Loading

0 comments on commit 2d9c589

Please sign in to comment.