Skip to content

Commit

Permalink
docs(samples): add code samples (#17)
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

* 🦉 Updates from OwlBot post-processor

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

* Rename *_ad_tag_details.py files for consistency

* add commit to trigger gh actions

* another commit to trigger samples tests

* add tests for live session ad tag details - requires custom manifest

* change slate URI to non-gcs uri

* Use project ID rather than project number per DPE style

* Change slate location for testing since we are going over the 3 slate quota limit

* add noxfile_config.py; enforce type hints

* add line break

* use GOOGLE_CLOUD_PROJECT in noxfile_config.py

* 🦉 Updates from OwlBot post-processor

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

* fix linter errors

* 🦉 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 authored May 27, 2022
1 parent 0e84031 commit bf845fb
Show file tree
Hide file tree
Showing 30 changed files with 2,347 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/google-cloud-video-stitcher/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ Windows
Next Steps
~~~~~~~~~~

- See the `Samples`_.
- Read the `Client Library Documentation`_ for Video Stitcher
to see other available methods on the client.
- Read the `Video Stitcher Product documentation`_ to learn
more about the product and see How-to Guides.
- View this `README`_ to see the full list of Cloud
APIs that we cover.

.. _Samples: https://github.com/googleapis/python-video-stitcher/blob/main/samples/snippets/README.md
.. _Video Stitcher Product documentation: https://cloud.google.com/video-stitcher/docs
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst
45 changes: 45 additions & 0 deletions packages/google-cloud-video-stitcher/samples/snippets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Video Stitcher API Python Samples

This directory contains samples for the Video Stitcher API. Use this API to
generate dynamic content for delivery to client devices. You can call the Video
Stitcher API from your servers to dynamically insert ads into video-on-demand
and live streams for your users. For more information, see the
[Video Stitcher API documentation](https://cloud.google.com/video-stitcher/).

## Setup

To run the samples, you need to first follow the steps in
[Before you begin](https://cloud.google.com/video-stitcher/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-stitcher and change directories to the sample directory
you want to use.

$ git clone https://github.com/googleapis/python-video-stitcher.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 Video Stitcher API on the test project. Set the
following environment variables:

* `GOOGLE_CLOUD_PROJECT`
* `GOOGLE_CLOUD_PROJECT_NUMBER`
142 changes: 142 additions & 0 deletions packages/google-cloud-video-stitcher/samples/snippets/cdn_key_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# 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 NotFound
import pytest

import create_cdn_key
import delete_cdn_key
import get_cdn_key
import list_cdn_keys
import update_cdn_key

location = "us-central1"
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
gcdn_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"
akamai_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"

hostname = "cdn.example.com"
updated_hostname = "updated.example.com"

gcdn_key_name = "gcdn-key"
gcdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
updated_gcdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg=="
akamai_key = gcdn_private_key
updated_akamai_key = updated_gcdn_private_key


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

try:
delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
out, _ = capsys.readouterr()

try:
delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
out, _ = capsys.readouterr()

# GCDN CDN key tests

create_cdn_key.create_cdn_key(
project_id,
location,
gcdn_cdn_key_id,
hostname,
gcdn_key_name,
gcdn_private_key,
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out

list_cdn_keys.list_cdn_keys(project_id, location)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out

# Update the hostname only
response = update_cdn_key.update_cdn_key(
project_id, location, gcdn_cdn_key_id, updated_hostname
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert updated_hostname in response.hostname

# Update the private key; the private key value is not returned by the client
response = update_cdn_key.update_cdn_key(
project_id,
location,
gcdn_cdn_key_id,
hostname,
gcdn_key_name,
updated_gcdn_private_key,
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out

get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out

delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
out, _ = capsys.readouterr()
assert "Deleted CDN key" in out

# Akamai CDN key tests

create_cdn_key.create_cdn_key(
project_id,
location,
akamai_cdn_key_id,
hostname,
akamai_token_key=akamai_key,
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out

list_cdn_keys.list_cdn_keys(project_id, location)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out

# Update the hostname only
response = update_cdn_key.update_cdn_key(
project_id, location, akamai_cdn_key_id, updated_hostname
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out
assert updated_hostname in response.hostname

# Update the private key; the private key value is not returned by the client
response = update_cdn_key.update_cdn_key(
project_id,
location,
akamai_cdn_key_id,
hostname,
akamai_token_key=updated_akamai_key,
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out

get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out

delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id)
out, _ = capsys.readouterr()
assert "Deleted CDN key" in out
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python

# 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.

"""Google Cloud Video Stitcher sample for creating a CDN key. A CDN key is used
to retrieve protected media.
Example usage:
python create_cdn_key.py --project_id <project-id> --location <location> --cdn_key_id <cdn_key_id> \
--hostname <hostname> [--gcdn_keyname <name> --gcdn_private_key <secret> | --akamai_token_key <token-key>]
"""

# [START video_stitcher_create_cdn_key]

import argparse

from google.cloud.video import stitcher_v1
from google.cloud.video.stitcher_v1.services.video_stitcher_service import (
VideoStitcherServiceClient,
)


def create_cdn_key(
project_id: str,
location: str,
cdn_key_id: str,
hostname: str,
gcdn_keyname: str = None,
gcdn_private_key: str = None,
akamai_token_key: str = None,
) -> str:
"""Creates a Google Cloud or Akamai CDN key.
Args:
project_id: The GCP project ID.
location: The location in which to create the CDN key.
cdn_key_id: The user-defined CDN key ID.
hostname: The hostname to which this CDN key applies.
gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret.
gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key.
akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key."""

client = VideoStitcherServiceClient()

parent = f"projects/{project_id}/locations/{location}"

cdn_key = stitcher_v1.types.CdnKey(
name=cdn_key_id,
hostname=hostname,
)

if akamai_token_key is not None:
cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey(
token_key=akamai_token_key,
)
elif gcdn_keyname is not None:
cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey(
key_name=gcdn_keyname,
private_key=gcdn_private_key,
)

response = client.create_cdn_key(
parent=parent, cdn_key_id=cdn_key_id, cdn_key=cdn_key
)
print(f"CDN key: {response.name}")
return response


# [END video_stitcher_create_cdn_key]

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
parser.add_argument(
"--location",
help="The location in which to create the CDN key.",
default="us-central1",
)
parser.add_argument(
"--cdn_key_id",
help="The user-defined CDN key ID.",
required=True,
)
parser.add_argument(
"--hostname",
help="The hostname to which this CDN key applies.",
required=True,
)
parser.add_argument(
"--gcdn_keyname",
help="Applies to a Google Cloud CDN key. The base64-encoded string secret.",
)
parser.add_argument(
"--gcdn_private_key",
help="Applies to a Google Cloud CDN key. Public name of the key.",
)
parser.add_argument(
"--akamai_token_key",
help="Applies to an Akamai CDN key. The base64-encoded string token key.",
)
args = parser.parse_args()
create_cdn_key(
args.project_id,
args.location,
args.cdn_key_id,
args.hostname,
args.gcdn_keyname,
args.gcdn_private_key,
args.akamai_token_key,
)
Loading

0 comments on commit bf845fb

Please sign in to comment.