Skip to content

Commit b171899

Browse files
DOCS-298: Update Rev AI python sdk to support different base url (#116)
1 parent d1ddb4a commit b171899

16 files changed

+128
-35
lines changed

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ your [AccessToken Settings Page](https://www.rev.ai/access_token). Create a clie
2929
generated Access Token:
3030

3131
```python
32-
from rev_ai import apiclient
32+
from rev_ai import apiclient, RevAiApiDeploymentConfigMap, RevAiApiDeployment
3333

3434
# create your client
35-
client = apiclient.RevAiAPIClient("ACCESS TOKEN")
35+
# optionally configure the Rev AI deployment to use
36+
client = apiclient.RevAiAPIClient("ACCESS TOKEN", url=RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url'])
3637
```
3738

3839
### Sending a file
@@ -211,18 +212,20 @@ In order to stream audio, you will need to setup a streaming client and a media
211212

212213
```python
213214
from rev_ai.streamingclient import RevAiStreamingClient
214-
from rev_ai.models import MediaConfig
215+
from rev_ai.models import MediaConfig, RevAiApiDeploymentConfigMap, RevAiApiDeployment
215216

216217
#on_error(error)
217218
#on_close(code, reason)
218219
#on_connected(id)
219220

221+
# optionally configure the Rev AI deployment to use
220222
config = MediaConfig()
221223
streaming_client = RevAiStreamingClient("ACCESS TOKEN",
222224
config,
223225
on_error=ERRORFUNC,
224226
on_close=CLOSEFUNC,
225-
on_connected=CONNECTEDFUNC)
227+
on_connected=CONNECTEDFUNC,
228+
url=RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_websocket_url'])
226229
```
227230

228231
`on_error`, `on_close`, and `on_connected` are optional parameters that are functions to be called when the websocket errors, closes, and connects respectively. The default `on_error` raises the error, `on_close` prints out the code and reason for closing, and `on_connected` prints out the job ID.

src/rev_ai/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
from .models import Job, JobStatus, Account, Transcript, Monologue, Element, MediaConfig, \
77
CaptionType, CustomVocabulary, TopicExtractionJob, TopicExtractionResult, Topic, Informant, \
88
SpeakerName, LanguageIdentificationJob, LanguageIdentificationResult, LanguageConfidence, \
9-
SentimentAnalysisResult, SentimentValue, SentimentMessage, SentimentAnalysisJob, CustomerUrlData
9+
SentimentAnalysisResult, SentimentValue, SentimentMessage, SentimentAnalysisJob, \
10+
CustomerUrlData, RevAiApiDeploymentConfigMap, RevAiApiDeployment

src/rev_ai/apiclient.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from . import utils
77
from .baseclient import BaseClient
8-
from .models import Account, CaptionType, Job, Transcript
8+
from .models import Account, CaptionType, Job, Transcript, RevAiApiDeploymentConfigMap, \
9+
RevAiApiDeployment
910
from .models.asynchronous.summarization_options import SummarizationOptions
1011
from .models.asynchronous.summary import Summary
1112
from .models.asynchronous.translation_options import TranslationOptions
@@ -30,20 +31,25 @@ class RevAiAPIClient(BaseClient):
3031
# Default version of Rev AI
3132
version = 'v1'
3233

33-
# Default base url for Rev AI
34-
base_url = 'https://api.rev.ai/speechtotext/{}/'.format(version)
34+
# Default url for US Rev AI deployment
35+
default_url = RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']
3536

3637
# Rev AI transcript format
3738
rev_json_content_type = 'application/vnd.rev.transcript.v1.0+json'
3839

39-
def __init__(self, access_token):
40+
def __init__(self, access_token: str, url: str = None):
4041
"""Constructor
4142
4243
:param access_token: access token which authorizes all requests and links them to your
4344
account. Generated on the settings page of your account dashboard
4445
on Rev AI.
46+
:param url (optional): url of the Rev AI API deployment to use, defaults to the US
47+
deployement, i.e. 'https://api.rev.ai', which can be referenced as
48+
RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url'].
4549
"""
4650

51+
# Default speech to text base url
52+
self.base_url = f'{url if url else self.default_url}/speechtotext/{self.version}/'
4753
BaseClient.__init__(self, access_token)
4854

4955
def submit_job_url(

src/rev_ai/generic_api_client.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Generic client used to interact with our newer style apis"""
33

44
from .baseclient import BaseClient
5+
from .models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
56

67
try:
78
from urllib.parse import urljoin
@@ -13,7 +14,18 @@ class GenericApiClient(BaseClient):
1314
"""Generic client which handles logic for making requests to almost any Rev AI Api.
1415
Intended to be inherited and extended by a specific client per API"""
1516

16-
def __init__(self, access_token, api_name, api_version, parse_job_info, parse_job_result):
17+
# Default url for US Rev AI deployment
18+
default_url = RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']
19+
20+
def __init__(
21+
self,
22+
access_token: str,
23+
api_name: str,
24+
api_version: str,
25+
parse_job_info,
26+
parse_job_result,
27+
url: str = None
28+
):
1729
"""Constructor
1830
1931
:param access_token: access token which authorizes all requests and links them to your
@@ -23,10 +35,13 @@ def __init__(self, access_token, api_name, api_version, parse_job_info, parse_jo
2335
:param api_version: version of the api to submit to
2436
:param parse_job_info: method to be used to parse job information
2537
:param parse_job_result: method to be used to parse job results
38+
:param url (optional): url of the Rev AI API deployment to use, defaults to the US
39+
deployement, i.e. 'https://api.rev.ai', which can be referenced as
40+
RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url'].
2641
"""
2742

2843
BaseClient.__init__(self, access_token)
29-
self.base_url = 'https://api.rev.ai/{0}/{1}/'.format(api_name, api_version)
44+
self.base_url = f'{url if url else self.default_url}/{api_name}/{api_version}/'
3045
self.parse_job_info = parse_job_info
3146
self.parse_job_result = parse_job_result
3247

src/rev_ai/language_identification_client.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import json
55
from .generic_api_client import GenericApiClient
6-
from .models import LanguageIdentificationJob, LanguageIdentificationResult
6+
from .models import LanguageIdentificationJob, LanguageIdentificationResult, \
7+
RevAiApiDeploymentConfigMap, RevAiApiDeployment
78

89
try:
910
from urllib.parse import urljoin
@@ -20,17 +21,20 @@ class LanguageIdentificationClient(GenericApiClient):
2021
# Default api name of Rev AI language identification api
2122
api_name = 'languageid'
2223

23-
def __init__(self, access_token):
24+
def __init__(self, access_token, url=None):
2425
"""Constructor
2526
2627
:param access_token: access token which authorizes all requests and links them to your
2728
account. Generated on the settings page of your account dashboard
2829
on Rev AI.
30+
:param url (optional): url of the Rev AI API deployment to use, defaults to the US
31+
deployement, i.e. 'https://api.rev.ai', which can be referenced as
32+
RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url'].
2933
"""
3034

3135
GenericApiClient.__init__(self, access_token, self.api_name, self.api_version,
3236
LanguageIdentificationJob.from_json,
33-
LanguageIdentificationResult.from_json)
37+
LanguageIdentificationResult.from_json, url)
3438

3539
def submit_job_url(
3640
self,

src/rev_ai/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
SentimentAnalysisResult, SentimentValue, SentimentMessage, SentimentAnalysisJob
1010
from .language_id import LanguageIdentificationJob, LanguageIdentificationResult, LanguageConfidence
1111
from .customer_url_data import CustomerUrlData
12+
from .revaiapi_deployment_config_constants import RevAiApiDeployment, RevAiApiDeploymentConfigMap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from enum import Enum
2+
from typing import Dict, TypedDict
3+
4+
5+
class RevAiApiDeployment(Enum):
6+
"""
7+
Enum representing the deployment regions for Rev AI API.
8+
9+
Attributes:
10+
US: Represents the United States deployment.
11+
EU: Represents the European Union deployment.
12+
"""
13+
US = 'US'
14+
EU = 'EU'
15+
16+
17+
class RevAiApiConfig(TypedDict):
18+
"""
19+
TypedDict representing the configuration for a Rev AI API deployment.
20+
21+
Attributes:
22+
base_url: The base URL for API requests.
23+
base_websocket_url: The base URL for WebSocket connections.
24+
"""
25+
base_url: str
26+
base_websocket_url: str
27+
28+
29+
# Dictionary mapping RevAiApiDeployment enum values to their respective configuration settings.
30+
RevAiApiDeploymentConfigMap: Dict[RevAiApiDeployment, RevAiApiConfig] = {
31+
RevAiApiDeployment.US: {
32+
'base_url': 'https://api.rev.ai',
33+
'base_websocket_url': 'wss://api.rev.ai'
34+
},
35+
RevAiApiDeployment.EU: {
36+
'base_url': 'https://ec1.api.rev.ai',
37+
'base_websocket_url': 'wss://ec1.api.rev.ai'
38+
}
39+
}

src/rev_ai/streamingclient.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import six
77
import json
88
from . import __version__
9+
from .models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
10+
from .models.streaming import MediaConfig
911

1012
try:
1113
from urllib.parse import urlencode
@@ -26,13 +28,18 @@ def on_connected(job_id):
2628

2729

2830
class RevAiStreamingClient:
31+
32+
# Default url for US Rev AI deployment
33+
default_url = RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_websocket_url']
34+
2935
def __init__(self,
30-
access_token,
31-
config,
32-
version='v1',
36+
access_token: str,
37+
config: MediaConfig,
38+
version: str = 'v1',
3339
on_error=on_error,
3440
on_close=on_close,
35-
on_connected=on_connected):
41+
on_connected=on_connected,
42+
url: str = None):
3643
"""Constructor for Streaming Client
3744
:param access_token: access token which authorizes all requests and
3845
links them to your account. Generated on the settings page of your
@@ -46,6 +53,9 @@ def __init__(self,
4653
closes
4754
:param on_connected (optional): function to be called when the websocket
4855
and thread starts successfully
56+
:param url (optional): url of the Rev AI API deployment to use, defaults to the US
57+
deployement, i.e. 'wss://api.rev.ai', which can be referenced as
58+
RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_websocket_url'].
4959
"""
5060
if not access_token:
5161
raise ValueError('access_token must be provided')
@@ -55,8 +65,7 @@ def __init__(self,
5565

5666
self.access_token = access_token
5767
self.config = config
58-
self.base_url = 'wss://api.rev.ai/speechtotext/{}/stream'. \
59-
format(version)
68+
self.base_url = f'{url if url else self.default_url}/speechtotext/{version}/stream'
6069
self.on_error = on_error
6170
self.on_close = on_close
6271
self.on_connected = on_connected

tests/test_account.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
import pytest
55
from src.rev_ai.apiclient import RevAiAPIClient
6+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
67
from src.rev_ai.models.asynchronous import Account
78

89
try:
910
from urllib.parse import urljoin
1011
except ImportError:
1112
from urlparse import urljoin
1213

13-
URL = urljoin(RevAiAPIClient.base_url, 'account')
14+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
15+
URL = urljoin(SPEECH_TO_TEXT_URL, 'account')
1416

1517

1618
@pytest.mark.usefixtures('mock_session', 'make_mock_response')

tests/test_async_captions_translation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from src.rev_ai.apiclient import RevAiAPIClient
4+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
45

56
try:
67
from urllib.parse import urljoin
@@ -9,8 +10,9 @@
910

1011
TOKEN = "token"
1112
JOB_ID = '1'
12-
JOB_ID_URL = urljoin(RevAiAPIClient.base_url, 'jobs/{}'.format(JOB_ID))
13-
JOBS_URL = urljoin(RevAiAPIClient.base_url, 'jobs')
13+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
14+
JOB_ID_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs/{}'.format(JOB_ID))
15+
JOBS_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs')
1416

1517

1618
@pytest.mark.usefixtures('mock_session', 'make_mock_response')

tests/test_async_summarization.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from src.rev_ai.apiclient import RevAiAPIClient
6+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
67
from src.rev_ai.models.asynchronous.summarization_formatting_options import SummarizationFormattingOptions
78
from src.rev_ai.models.asynchronous.summarization_job_status import SummarizationJobStatus
89
from src.rev_ai.models.asynchronous.summarization_options import SummarizationOptions
@@ -15,8 +16,9 @@
1516

1617
TOKEN = "token"
1718
JOB_ID = '1'
18-
JOB_ID_URL = urljoin(RevAiAPIClient.base_url, 'jobs/{}'.format(JOB_ID))
19-
JOBS_URL = urljoin(RevAiAPIClient.base_url, 'jobs')
19+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
20+
JOB_ID_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs/{}'.format(JOB_ID))
21+
JOBS_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs')
2022
JOB_TRANSCRIPT_SUMMARY_URL = '{}/transcript/summary'.format(JOB_ID_URL)
2123

2224

tests/test_async_translation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from src.rev_ai.apiclient import RevAiAPIClient
6+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
67
from src.rev_ai.models.asynchronous.translation_job_status import TranslationJobStatus
78
from src.rev_ai.models.asynchronous.translation_language_options import TranslationLanguageOptions
89
from src.rev_ai.models.asynchronous.translation_options import TranslationOptions
@@ -15,8 +16,9 @@
1516

1617
TOKEN = "token"
1718
JOB_ID = '1'
18-
JOB_ID_URL = urljoin(RevAiAPIClient.base_url, 'jobs/{}'.format(JOB_ID))
19-
JOBS_URL = urljoin(RevAiAPIClient.base_url, 'jobs')
19+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
20+
JOB_ID_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs/{}'.format(JOB_ID))
21+
JOBS_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs')
2022

2123

2224
@pytest.mark.usefixtures('mock_session', 'make_mock_response')

tests/test_baseclient.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from src.rev_ai.apiclient import RevAiAPIClient
99
from src.rev_ai import __version__
1010
from src.rev_ai.baseclient import BaseClient
11+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
1112
from tests.helpers.errors import get_error_test_cases
1213

1314
TOKEN = "token"
@@ -34,7 +35,7 @@ def test_constructor_with_no_token(self, token):
3435
def test_make_http_request(self, error, method, mock_session,
3536
make_mock_response):
3637
status = error.get('status')
37-
URL = RevAiAPIClient.base_url
38+
URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
3839
response = make_mock_response(url=URL, status=status, json_data=error)
3940
mock_session.request.return_value = response
4041
client = RevAiAPIClient(TOKEN)

tests/test_captions.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import pytest
44
from src.rev_ai.apiclient import RevAiAPIClient
5-
from src.rev_ai.models import CaptionType
5+
from src.rev_ai.models import CaptionType, RevAiApiDeploymentConfigMap, RevAiApiDeployment
6+
67

78
try:
89
from urllib.parse import urljoin
@@ -11,7 +12,8 @@
1112

1213
JOB_ID = '1'
1314
TOKEN = "token"
14-
URL = urljoin(RevAiAPIClient.base_url, 'jobs/{}/captions'.format(JOB_ID))
15+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
16+
URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs/{}/captions'.format(JOB_ID))
1517

1618

1719
@pytest.mark.usefixtures('mock_session', 'make_mock_response')

tests/test_job.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import json
55
import pytest
66

7-
from src.rev_ai.models.customer_url_data import CustomerUrlData
87
from src.rev_ai.apiclient import RevAiAPIClient
8+
from src.rev_ai.models import RevAiApiDeploymentConfigMap, RevAiApiDeployment
99
from src.rev_ai.models.asynchronous import Job, JobStatus, SpeakerName
10+
from src.rev_ai.models.customer_url_data import CustomerUrlData
1011

1112
try:
1213
from urllib.parse import urljoin
@@ -22,8 +23,9 @@
2223
SOURCE_URL = 'https://example.com/test.mp3'
2324
SOURCE_AUTH = 'source auth headers'
2425
FILENAME = 'test.mp3'
25-
JOB_ID_URL = urljoin(RevAiAPIClient.base_url, 'jobs/{}'.format(JOB_ID))
26-
JOBS_URL = urljoin(RevAiAPIClient.base_url, 'jobs')
26+
SPEECH_TO_TEXT_URL = f"{RevAiApiDeploymentConfigMap[RevAiApiDeployment.US]['base_url']}/speechtotext/v1/"
27+
JOB_ID_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs/{}'.format(JOB_ID))
28+
JOBS_URL = urljoin(SPEECH_TO_TEXT_URL, 'jobs')
2729
CUSTOM_VOCAB = [{"phrases": ["word one", "word two"]}]
2830
CUSTOM_VOCAB_ID = "vid"
2931
LANGUAGE = 'en'

0 commit comments

Comments
 (0)