Skip to content

Commit 3468482

Browse files
irataxypartheagcf-owl-bot[bot]
authored
docs(samples): add code samples (#21)
* 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>
0 parents  commit 3468482

24 files changed

+1647
-0
lines changed

video/live-stream/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Live Stream API Python Samples
2+
3+
This directory contains samples for the Live Stream API. Use this API to
4+
transcode live, linear video streams into a variety of formats. The Live Stream
5+
API benefits broadcasters, production companies, businesses, and individuals
6+
looking to transform their live video content for use across a variety of user
7+
devices. For more information, see the
8+
[Live Stream API documentation](https://cloud.google.com/livestream/).
9+
10+
## Setup
11+
12+
To run the samples, you need to first follow the steps in
13+
[Before you begin](https://cloud.google.com/livestream/docs/how-to/before-you-begin).
14+
15+
For more information on authentication, refer to the
16+
[Authentication Getting Started Guide](https://cloud.google.com/docs/authentication/getting-started).
17+
18+
## Install Dependencies
19+
20+
1. Clone python-video-live-stream and change directories to the sample directory
21+
you want to use.
22+
23+
$ git clone https://github.com/googleapis/python-video-live-stream.git
24+
25+
1. Install [pip](https://pip.pypa.io/) and
26+
[virtualenv](https://virtualenv.pypa.io/) if you do not already have them. You
27+
may want to refer to the
28+
[Python Development Environment Setup Guide](https://cloud.google.com/python/setup)
29+
for Google Cloud Platform for instructions.
30+
31+
1. Create a virtualenv. Samples are compatible with Python 3.6+.
32+
33+
$ virtualenv env
34+
$ source env/bin/activate
35+
36+
1. Install the dependencies needed to run the samples.
37+
38+
$ pip install -r requirements.txt
39+
40+
## Testing
41+
42+
Make sure to enable the Live Stream API on the test project. Set the following
43+
environment variable:
44+
45+
* `GOOGLE_CLOUD_PROJECT`
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
import pytest
19+
20+
import create_channel
21+
import create_channel_event
22+
import create_input
23+
import delete_channel
24+
import delete_channel_event
25+
import delete_input
26+
import get_channel_event
27+
import list_channel_events
28+
import start_channel
29+
import stop_channel
30+
31+
project_name = os.environ["GOOGLE_CLOUD_PROJECT"]
32+
location = "us-central1"
33+
input_id = f"python-test-input-{uuid.uuid4()}"
34+
channel_id = f"python-test-channel-{uuid.uuid4()}"
35+
event_id = f"python-test-event-{uuid.uuid4()}"
36+
output_bucket_name = f"python-test-bucket-{uuid.uuid4()}"
37+
output_uri = f"gs://{output_bucket_name}/channel-test/"
38+
39+
40+
def test_channel_event_operations(capsys: pytest.fixture) -> None:
41+
42+
# Set up
43+
44+
channel_name_project_id = (
45+
f"projects/{project_name}/locations/{location}/channels/{channel_id}"
46+
)
47+
event_name_project_id = f"projects/{project_name}/locations/{location}/channels/{channel_id}/events/{event_id}"
48+
49+
create_input.create_input(project_name, location, input_id)
50+
51+
create_channel.create_channel(
52+
project_name, location, channel_id, input_id, output_uri
53+
)
54+
out, _ = capsys.readouterr()
55+
assert channel_name_project_id in out
56+
57+
start_channel.start_channel(project_name, location, channel_id)
58+
out, _ = capsys.readouterr()
59+
assert "Started channel" in out
60+
61+
# Tests
62+
63+
create_channel_event.create_channel_event(
64+
project_name, location, channel_id, event_id
65+
)
66+
out, _ = capsys.readouterr()
67+
assert event_name_project_id in out
68+
69+
get_channel_event.get_channel_event(project_name, location, channel_id, event_id)
70+
out, _ = capsys.readouterr()
71+
assert event_name_project_id in out
72+
73+
list_channel_events.list_channel_events(project_name, location, channel_id)
74+
out, _ = capsys.readouterr()
75+
assert event_name_project_id in out
76+
77+
delete_channel_event.delete_channel_event(
78+
project_name, location, channel_id, event_id
79+
)
80+
out, _ = capsys.readouterr()
81+
assert "Deleted channel event" in out
82+
83+
# Clean up
84+
85+
stop_channel.stop_channel(project_name, location, channel_id)
86+
out, _ = capsys.readouterr()
87+
assert "Stopped channel" in out
88+
89+
delete_channel.delete_channel(project_name, location, channel_id)
90+
out, _ = capsys.readouterr()
91+
assert "Deleted channel" in out
92+
93+
delete_input.delete_input(project_name, location, input_id)

video/live-stream/channel_test.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
from google.api_core.exceptions import FailedPrecondition, NotFound
19+
import pytest
20+
21+
import create_channel
22+
import create_input
23+
import delete_channel
24+
import delete_channel_event
25+
import delete_input
26+
import get_channel
27+
import list_channel_events
28+
import list_channels
29+
import start_channel
30+
import stop_channel
31+
import update_channel
32+
import utils
33+
34+
project_name = os.environ["GOOGLE_CLOUD_PROJECT"]
35+
location = "us-central1"
36+
input_id = f"python-test-input-{uuid.uuid4()}"
37+
updated_input_id = f"python-test-up-input-{uuid.uuid4()}"
38+
channel_id = f"python-test-channel-{uuid.uuid4()}"
39+
output_bucket_name = f"python-test-bucket-{uuid.uuid4()}"
40+
output_uri = f"gs://{output_bucket_name}/channel-test/"
41+
42+
43+
def test_channel_operations(capsys: pytest.fixture) -> None:
44+
45+
# Clean up old resources in the test project
46+
channel_responses = list_channels.list_channels(project_name, location)
47+
48+
for response in channel_responses:
49+
next_channel_id = response.name.rsplit("/", 1)[-1]
50+
input_attachments = response.input_attachments
51+
if utils.is_resource_stale(response.create_time):
52+
try:
53+
event_responses = list_channel_events.list_channel_events(
54+
project_name, location, next_channel_id
55+
)
56+
for response in event_responses:
57+
next_event_id = response.name.rsplit("/", 1)[-1]
58+
try:
59+
delete_channel_event.delete_channel_event(
60+
project_name, location, next_channel_id, next_event_id
61+
)
62+
except NotFound as e:
63+
print(f"Ignoring NotFound, details: {e}")
64+
try:
65+
stop_channel.stop_channel(project_name, location, next_channel_id)
66+
except FailedPrecondition as e:
67+
print(f"Ignoring FailedPrecondition, details: {e}")
68+
try:
69+
delete_channel.delete_channel(
70+
project_name, location, next_channel_id
71+
)
72+
except NotFound as e:
73+
print(f"Ignoring NotFound, details: {e}")
74+
except NotFound as e:
75+
print(f"Ignoring NotFound, details: {e}")
76+
77+
for input_attachment in input_attachments:
78+
next_input_id = input_attachment.input.rsplit("/", 1)[-1]
79+
try:
80+
delete_input.delete_input(project_name, location, next_input_id)
81+
except NotFound as e:
82+
print(f"Ignoring NotFound, details: {e}")
83+
84+
# Set up
85+
86+
channel_name_project_id = (
87+
f"projects/{project_name}/locations/{location}/channels/{channel_id}"
88+
)
89+
90+
create_input.create_input(project_name, location, input_id)
91+
create_input.create_input(project_name, location, updated_input_id)
92+
93+
# Tests
94+
95+
create_channel.create_channel(
96+
project_name, location, channel_id, input_id, output_uri
97+
)
98+
out, _ = capsys.readouterr()
99+
assert channel_name_project_id in out
100+
101+
list_channels.list_channels(project_name, location)
102+
out, _ = capsys.readouterr()
103+
assert channel_name_project_id in out
104+
105+
response = update_channel.update_channel(
106+
project_name, location, channel_id, updated_input_id
107+
)
108+
out, _ = capsys.readouterr()
109+
assert channel_name_project_id in out
110+
for input_attachment in response.input_attachments:
111+
assert "updated-input" in input_attachment.key
112+
113+
get_channel.get_channel(project_name, location, channel_id)
114+
out, _ = capsys.readouterr()
115+
assert channel_name_project_id in out
116+
117+
start_channel.start_channel(project_name, location, channel_id)
118+
out, _ = capsys.readouterr()
119+
assert "Started channel" in out
120+
121+
stop_channel.stop_channel(project_name, location, channel_id)
122+
out, _ = capsys.readouterr()
123+
assert "Stopped channel" in out
124+
125+
delete_channel.delete_channel(project_name, location, channel_id)
126+
out, _ = capsys.readouterr()
127+
assert "Deleted channel" in out
128+
129+
# Clean up
130+
131+
delete_input.delete_input(project_name, location, input_id)
132+
delete_input.delete_input(project_name, location, updated_input_id)

0 commit comments

Comments
 (0)