Skip to content

Commit

Permalink
Merge pull request #19 from DolbyIO/fix
Browse files Browse the repository at this point in the history
Version 3.6.0
  • Loading branch information
FabienLavocat authored Feb 19, 2023
2 parents f627b9a + 8b0b5c1 commit a87997b
Show file tree
Hide file tree
Showing 28 changed files with 290 additions and 298 deletions.
220 changes: 163 additions & 57 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,66 @@ python3 -m pip install --upgrade dolbyio-rest-apis

## Logging

You can change the log level by using the Python (logging)[https://docs.python.org/3/library/logging.html] library.
You can change the log level by using the Python [logging](https://docs.python.org/3/library/logging.html) library.

```python
import logging

logging.basicConfig(level="DEBUG")
logging.basicConfig(level='DEBUG')
```

## Communications Examples

### Authenticate
## Authentication

To get an access token that will be used by the client SDK for an end user to open a session against dolby.io, use the following code:
In order to make API calls for most operations of the **Communications APIs** and **Media APIs**, you must get an access token using this API:

```python
import asyncio
from dolbyio_rest_apis.communications import authentication
from dolbyio_rest_apis import authentication

APP_KEY = "YOUR_APP_KEY"
APP_SECRET = "YOUR_APP_SECRET"
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

loop = asyncio.get_event_loop()

task = authentication.get_api_token(APP_KEY, APP_SECRET)
at = loop.run_until_complete(task)

print(f"Access Token: {at.access_token}")
print(f'API Token: {at.access_token}')
```

You can write an async function like that:
## Communications Examples

### Get a client access token

To get an access token that will be used by the client SDK for an end user to open a session against dolby.io, use the following code:

```python
import asyncio
from dolbyio_rest_apis.communications import authentication

APP_KEY = "YOUR_APP_KEY"
APP_SECRET = "YOUR_APP_SECRET"
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

async def get_client_access_token():
at = await authentication.get_client_access_token(APP_KEY, APP_SECRET)
print(f"Access Token: {at.access_token}")
loop = asyncio.get_event_loop()

task = authentication.get_api_token(APP_KEY, APP_SECRET)
at = loop.run_until_complete(task)

print(f'Access Token: {at.access_token}')
```

To get an access token that will be used by your server to perform backend operations like creating a conference, use the following code.
Because most of the APIs are asynchronous, you can write an async function like that:

```python
import asyncio
from dolbyio_rest_apis.communications import authentication

APP_KEY = "YOUR_APP_KEY"
APP_SECRET = "YOUR_APP_SECRET"
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

loop = asyncio.get_event_loop()
task = authentication.get_api_access_token(APP_KEY, APP_SECRET)
at = loop.run_until_complete(task)
async def get_client_access_token():
at = await authentication.get_client_access_token(APP_KEY, APP_SECRET)
print(f'Access Token: {at.access_token}')

print(f"Access Token: {at.access_token}")
```

### Create a conference
Expand All @@ -82,29 +86,37 @@ To create a Dolby Voice conference, you first must retrieve an API Access Token,

```python
import asyncio
from dolbyio_rest_apis import authentication
from dolbyio_rest_apis.communications import conference
from dolbyio_rest_apis.communications.models import Participant, Permission, VideoCodec

access_token = "" # Retrieve an API Access Token
owner_id = "" # Identifier of the owner of the conference
alias = "" # Conference alias
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

owner_id = '' # Identifier of the owner of the conference
alias = '' # Conference alias

participants = [
Participant("hostA", [Permission.JOIN, Permission.SEND_AUDIO, Permission.SEND_VIDEO], notify=True),
Participant("listener1", [Permission.JOIN], notify=False),
Participant('hostA', [Permission.JOIN, Permission.SEND_AUDIO, Permission.SEND_VIDEO], notify=True),
Participant('listener1', [Permission.JOIN], notify=False),
]

loop = asyncio.get_event_loop()

# Request an API token
task = authentication.get_api_token(APP_KEY, APP_SECRET)
at = loop.run_until_complete(task)

task = conference.create_conference(
access_token,
at.access_token,
owner_id,
alias,
video_codec=VideoCodec.VP8,
participants=participants
)
conf = loop.run_until_complete(task)

print(f"Conference created: {conf.id}")
print(f'Conference created: {conf.id}')
```

## Real-time Streaming Examples
Expand All @@ -116,13 +128,14 @@ import asyncio
from dolbyio_rest_apis.streaming import publish_token
from dolbyio_rest_apis.streaming.models.publish_token import CreatePublishToken, CreateUpdatePublishTokenStream

api_secret = "" # Retrieve your API Secret from the dashboard
API_SECRET = '' # Retrieve your API Secret from the dashboard

create_token = CreatePublishToken('my_token')
create_token.streams.append(CreateUpdatePublishTokenStream('feed1', False))

loop = asyncio.get_event_loop()
task = publish_token.create(api_secret, create_token)

task = publish_token.create(API_SECRET, create_token)
token = loop.run_until_complete(task)

print(token)
Expand All @@ -131,60 +144,153 @@ print(token)
### Create a subscribe token

```python
const dolbyio = require('@dolbyio/dolbyio-rest-apis-client');
import asyncio
from dolbyio_rest_apis.streaming import subscribe_token
from dolbyio_rest_apis.streaming.models.subscribe_token import CreateSubscribeToken, CreateUpdateSubscribeTokenStream

API_SECRET = '' # Retrieve your API Secret from the dashboard

create_token = CreateSubscribeToken('my_token')
create_token.streams.append(CreateUpdateSubscribeTokenStream('feed1', False))

loop = asyncio.get_event_loop()

const subscribeToken = await dolbyio.streaming.subscribeToken.create('api_secret', {
label: 'My token',
streams: [
{
streamName: 'feedA',
},
],
});
console.log(subscribeToken);
task = publish_token.create(API_SECRET, create_token)
token = loop.run_until_complete(task)

print(token)
```

## Media Examples

### Media Input and Output
Here is an example on how to upload a file to the Dolby.io temporary cloud storage, enhance that file and download the result.

### Get an API token

Upload a media file to the temporary Dolby.io cloud storage for processing:
Get the App Key and Secret from the Dolby.io dashboard and use the following code in your python script.

```python
import asyncio
from dolbyio_rest_apis import authentication

APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

loop = asyncio.get_event_loop()

task = authentication.get_api_token(APP_KEY, APP_SECRET, 2 * 60 * 60) # 2 hours
at = loop.run_until_complete(task)
print(f'API token: {at.access_token}')
```

### Upload a file for processing

Add the following `import` to your script.

```python
from dolbyio_rest_apis.media import io
```

ACCESS_TOKEN = "YOUR_API_TOKEN"
Using the API token, start by requesting an upload URL.

```python
# Temporary storage URL that will be used as reference for the job processing
input_url = 'dlb://in/file.mp4'

# Get an Upload URL
task = io.get_upload_url(
access_token=ACCESS_TOKEN,
dlb_url='dlb://in/file.mp4'
access_token=at.access_token,
dlb_url=input_url,
)
upload_url = loop.run_until_complete(task)
print(f'Upload URL: {upload_url}')
```

print(f"Upload URL: {upload_url}")
Upload a media file to the Dolby.io temporary cloud storage for processing:

```python
# Location of the original file on the local machine
IN_FILE_PATH = '/path/to/original_file.mp4'

# Upload the file
task = io.upload_file(
upload_url=upload_url,
file_path='/path/to/file.mp4'
file_path=IN_FILE_PATH,
)
loop.run_until_complete(task)
```

Download a file that was processed by the API:
### Start an enhance job

Add the following `import` to your script.

```python
import asyncio
from dolbyio_rest_apis.media import io
import json
from dolbyio_rest_apis.media import enhance
```

Generate a job description and send it to Dolby.io.

```python
# Temporary storage URL where the service will write the result file to
output_url = 'dlb://out/file.mp4'

# Job description
job = {
'input': input_url,
'output': output_url,
'content': {
'type': 'podcast',
},
}
job_str = json.dumps(job, indent=4)
print(job_str)

# Start the enhance job and get a job ID back
task = enhance.start(at.access_token, job_str)
job_id = loop.run_until_complete(task)
print(f'Job ID: {job_id}')
```

### Wait for the job to complete

Add the following `import` to your script.

```python
import sys
import time
```

Get the job status and wait until it is completed.

ACCESS_TOKEN = "YOUR_API_TOKEN"
```python
task = enhance.get_results(at.access_token, job_id)
result = loop.run_until_complete(task)
while result.status in ( 'Pending', 'Running' ):
print(f'Job status is {result.status}, taking a 5 second break...')
time.sleep(5)

task = enhance.get_results(at.access_token, job_id)
result = loop.run_until_complete(task)

if result.status != 'Success':
print('There was a problem with processing the file')
print(json.dumps(result, indent=4))
sys.exit(1)
```

### Download a processed file

At this stage, the file has been processed and written to the temporary storage so we can download it.

```python
# Where to download the file on the local machine
OUT_FILE_PATH = '/path/to/processed_file.mp4'

task = io.download_file(
access_token=ACCESS_TOKEN,
dlb_url='dlb://out/file.mp4',
file_path='/path/to/processed_file.mp4'
access_token=at.access_token,
dlb_url=output_url,
file_path=OUT_FILE_PATH,
)
loop.run_until_complete(task)
```
1 change: 0 additions & 1 deletion client/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ aiohttp>=3.7.4
aiofiles>=0.7.0
aiohttp-retry>=2.4.6
certifi>=2022.12.7
Deprecated
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""
dolbyio_rest_apis.media.platform
dolbyio_rest_apis.authentication
~~~~~~~~~~~~~~~
This module contains the functions to work with the Platform APIs.
This module contains the functions to work with the authentication API.
"""

from dolbyio_rest_apis.core.helpers import add_if_not_none
from dolbyio_rest_apis.media.internal.http_context import MediaHttpContext
from dolbyio_rest_apis.media.models.access_token import AccessToken
from dolbyio_rest_apis.core.urls import get_api_url
from dolbyio_rest_apis.communications.internal.http_context import CommunicationsHttpContext
from .models import AccessToken

async def get_api_token(
app_key: str,
Expand All @@ -16,10 +17,9 @@ async def get_api_token(
) -> AccessToken:
r"""
To make any API call, you must acquire a JWT (JSON Web Token) format API token.
Make sure to use this API against https://api.dolby.io/v1.
Note: Even though the OAuth terminology is used in the following APIs, they are not OAuth compliant.
See: https://docs.dolby.io/communications-apis/reference/get-api-token
See: https://docs.dolby.io/media-apis/reference/get-api-token
Args:
Expand All @@ -42,11 +42,11 @@ async def get_api_token(
}
add_if_not_none(data, 'expires_in', expires_in)

async with MediaHttpContext() as http_context:
async with CommunicationsHttpContext() as http_context:
json_response = await http_context.requests_post_basic_auth(
app_key=app_key,
app_secret=app_secret,
url='https://api.dolby.io/v1/auth/token',
url=f'{get_api_url()}/auth/token',
data=data
)

Expand Down
Loading

0 comments on commit a87997b

Please sign in to comment.