Skip to content

Commit

Permalink
docs(samples): add code samples (#21)
Browse files Browse the repository at this point in the history
* docs(samples): add code samples

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix region tags

* enforce Python type hints

* use build specific projects for samples testing

* Remove project number env variable. Remove cloud storage dependency. Clean up resources > 3 hours old (instead of 2 days old).

* Remove project number in favor of project ID. Avoid infinite loops in responses to LROs.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 28, 2022
0 parents commit 3468482
Show file tree
Hide file tree
Showing 24 changed files with 1,647 additions and 0 deletions.
45 changes: 45 additions & 0 deletions video/live-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Live Stream API Python Samples

This directory contains samples for the Live Stream API. Use this API to
transcode live, linear video streams into a variety of formats. The Live Stream
API benefits broadcasters, production companies, businesses, and individuals
looking to transform their live video content for use across a variety of user
devices. For more information, see the
[Live Stream API documentation](https://cloud.google.com/livestream/).

## Setup

To run the samples, you need to first follow the steps in
[Before you begin](https://cloud.google.com/livestream/docs/how-to/before-you-begin).

For more information on authentication, refer to the
[Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started).

## Install Dependencies

1. Clone python-video-live-stream and change directories to the sample directory
you want to use.

$ git clone https://github.com/googleapis/python-video-live-stream.git

1. Install [pip](https://pip.pypa.io/) and
[virtualenv](https://virtualenv.pypa.io/) if you do not already have them. You
may want to refer to the
[Python Development Environment Setup Guide](https://cloud.google.com/python/setup)
for Google Cloud Platform for instructions.

1. Create a virtualenv. Samples are compatible with Python 3.6+.

$ virtualenv env
$ source env/bin/activate

1. Install the dependencies needed to run the samples.

$ pip install -r requirements.txt

## Testing

Make sure to enable the Live Stream API on the test project. Set the following
environment variable:

* `GOOGLE_CLOUD_PROJECT`
93 changes: 93 additions & 0 deletions video/live-stream/channel_event_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright 2022 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

import pytest

import create_channel
import create_channel_event
import create_input
import delete_channel
import delete_channel_event
import delete_input
import get_channel_event
import list_channel_events
import start_channel
import stop_channel

project_name = os.environ["GOOGLE_CLOUD_PROJECT"]
location = "us-central1"
input_id = f"python-test-input-{uuid.uuid4()}"
channel_id = f"python-test-channel-{uuid.uuid4()}"
event_id = f"python-test-event-{uuid.uuid4()}"
output_bucket_name = f"python-test-bucket-{uuid.uuid4()}"
output_uri = f"gs://{output_bucket_name}/channel-test/"


def test_channel_event_operations(capsys: pytest.fixture) -> None:

# Set up

channel_name_project_id = (
f"projects/{project_name}/locations/{location}/channels/{channel_id}"
)
event_name_project_id = f"projects/{project_name}/locations/{location}/channels/{channel_id}/events/{event_id}"

create_input.create_input(project_name, location, input_id)

create_channel.create_channel(
project_name, location, channel_id, input_id, output_uri
)
out, _ = capsys.readouterr()
assert channel_name_project_id in out

start_channel.start_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Started channel" in out

# Tests

create_channel_event.create_channel_event(
project_name, location, channel_id, event_id
)
out, _ = capsys.readouterr()
assert event_name_project_id in out

get_channel_event.get_channel_event(project_name, location, channel_id, event_id)
out, _ = capsys.readouterr()
assert event_name_project_id in out

list_channel_events.list_channel_events(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert event_name_project_id in out

delete_channel_event.delete_channel_event(
project_name, location, channel_id, event_id
)
out, _ = capsys.readouterr()
assert "Deleted channel event" in out

# Clean up

stop_channel.stop_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Stopped channel" in out

delete_channel.delete_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Deleted channel" in out

delete_input.delete_input(project_name, location, input_id)
132 changes: 132 additions & 0 deletions video/live-stream/channel_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Copyright 2022 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import uuid

from google.api_core.exceptions import FailedPrecondition, NotFound
import pytest

import create_channel
import create_input
import delete_channel
import delete_channel_event
import delete_input
import get_channel
import list_channel_events
import list_channels
import start_channel
import stop_channel
import update_channel
import utils

project_name = os.environ["GOOGLE_CLOUD_PROJECT"]
location = "us-central1"
input_id = f"python-test-input-{uuid.uuid4()}"
updated_input_id = f"python-test-up-input-{uuid.uuid4()}"
channel_id = f"python-test-channel-{uuid.uuid4()}"
output_bucket_name = f"python-test-bucket-{uuid.uuid4()}"
output_uri = f"gs://{output_bucket_name}/channel-test/"


def test_channel_operations(capsys: pytest.fixture) -> None:

# Clean up old resources in the test project
channel_responses = list_channels.list_channels(project_name, location)

for response in channel_responses:
next_channel_id = response.name.rsplit("/", 1)[-1]
input_attachments = response.input_attachments
if utils.is_resource_stale(response.create_time):
try:
event_responses = list_channel_events.list_channel_events(
project_name, location, next_channel_id
)
for response in event_responses:
next_event_id = response.name.rsplit("/", 1)[-1]
try:
delete_channel_event.delete_channel_event(
project_name, location, next_channel_id, next_event_id
)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
try:
stop_channel.stop_channel(project_name, location, next_channel_id)
except FailedPrecondition as e:
print(f"Ignoring FailedPrecondition, details: {e}")
try:
delete_channel.delete_channel(
project_name, location, next_channel_id
)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")

for input_attachment in input_attachments:
next_input_id = input_attachment.input.rsplit("/", 1)[-1]
try:
delete_input.delete_input(project_name, location, next_input_id)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")

# Set up

channel_name_project_id = (
f"projects/{project_name}/locations/{location}/channels/{channel_id}"
)

create_input.create_input(project_name, location, input_id)
create_input.create_input(project_name, location, updated_input_id)

# Tests

create_channel.create_channel(
project_name, location, channel_id, input_id, output_uri
)
out, _ = capsys.readouterr()
assert channel_name_project_id in out

list_channels.list_channels(project_name, location)
out, _ = capsys.readouterr()
assert channel_name_project_id in out

response = update_channel.update_channel(
project_name, location, channel_id, updated_input_id
)
out, _ = capsys.readouterr()
assert channel_name_project_id in out
for input_attachment in response.input_attachments:
assert "updated-input" in input_attachment.key

get_channel.get_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert channel_name_project_id in out

start_channel.start_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Started channel" in out

stop_channel.stop_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Stopped channel" in out

delete_channel.delete_channel(project_name, location, channel_id)
out, _ = capsys.readouterr()
assert "Deleted channel" in out

# Clean up

delete_input.delete_input(project_name, location, input_id)
delete_input.delete_input(project_name, location, updated_input_id)
Loading

0 comments on commit 3468482

Please sign in to comment.