Skip to content

Commit 85ef128

Browse files
refactor: update typehints in scheduler samples (GoogleCloudPlatform#9971)
* refactor: update typehints in scheduler samples * update test * return updated response * fix test * fix test --------- Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com>
1 parent 7b1bd8c commit 85ef128

5 files changed

Lines changed: 77 additions & 59 deletions

File tree

scheduler/snippets/create_job.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START cloudscheduler_create_job]
1516

16-
def create_scheduler_job(project_id, location_id, service_id):
17-
"""Create a job with an App Engine target via the Cloud Scheduler API"""
18-
# [START cloudscheduler_create_job]
19-
from google.cloud import scheduler
17+
from google.cloud import scheduler
18+
from google.cloud.scheduler_v1 import Job
19+
20+
21+
def create_scheduler_job(project_id: str, location_id: str, service_id: str) -> Job:
22+
"""Create a job with an App Engine target via the Cloud Scheduler API.
23+
24+
Args:
25+
project_id: The Google Cloud project id.
26+
location_id: The location for the job.
27+
service_id: An unique service id for the job.
28+
29+
Returns:
30+
The created job.
31+
"""
2032

2133
# Create a client.
2234
client = scheduler.CloudSchedulerClient()
2335

24-
# TODO(developer): Uncomment and set the following variables
25-
# project_id = 'PROJECT_ID'
26-
# location_id = 'LOCATION_ID'
27-
# service_id = 'my-service'
28-
2936
# Construct the fully qualified location path.
3037
parent = f"projects/{project_id}/locations/{location_id}"
3138

@@ -45,31 +52,6 @@ def create_scheduler_job(project_id, location_id, service_id):
4552
response = client.create_job(request={"parent": parent, "job": job})
4653

4754
print(f"Created job: {response.name}")
48-
# [END cloudscheduler_create_job]
4955
return response
5056

51-
52-
def delete_scheduler_job(project_id, location_id, job_id):
53-
"""Delete a job via the Cloud Scheduler API"""
54-
# [START cloudscheduler_delete_job]
55-
from google.api_core.exceptions import GoogleAPICallError
56-
from google.cloud import scheduler
57-
58-
# Create a client.
59-
client = scheduler.CloudSchedulerClient()
60-
61-
# TODO(developer): Uncomment and set the following variables
62-
# project_id = 'PROJECT_ID'
63-
# location_id = 'LOCATION_ID'
64-
# job_id = 'JOB_ID'
65-
66-
# Construct the fully qualified job path.
67-
job = f"projects/{project_id}/locations/{location_id}/jobs/{job_id}"
68-
69-
# Use the client to send the job deletion request.
70-
try:
71-
client.delete_job(name=job)
72-
print("Job deleted.")
73-
except GoogleAPICallError as e:
74-
print("Error: %s" % e)
75-
# [END cloudscheduler_delete_job]
57+
# [END cloudscheduler_create_job]

scheduler/snippets/delete_job.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2023 Google LLC
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+
# [START cloudscheduler_delete_job]
16+
17+
from google.cloud import scheduler
18+
19+
20+
def delete_scheduler_job(project_id: str, location_id: str, job_id: str) -> None:
21+
"""Delete a job via the Cloud Scheduler API.
22+
23+
Args:
24+
project_id: The Google Cloud project id.
25+
location_id: The location for the job to delete.
26+
job_id: The id of the job to delete.
27+
"""
28+
29+
# Create a client.
30+
client = scheduler.CloudSchedulerClient()
31+
32+
# Construct the fully qualified job path.
33+
job = f"projects/{project_id}/locations/{location_id}/jobs/{job_id}"
34+
35+
# Use the client to send the job deletion request.
36+
client.delete_job(name=job)
37+
print("Job deleted.")
38+
39+
# [END cloudscheduler_delete_job]
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414

1515
import os
1616

17+
from _pytest.capture import CaptureFixture
18+
1719
import create_job
20+
import delete_job
1821

1922
TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2023
TEST_LOCATION = os.getenv("LOCATION_ID", "us-central1")
2124

2225

23-
def test_create_job(capsys):
24-
create_result = create_job.create_scheduler_job(
26+
def test_create_job(capsys: CaptureFixture):
27+
response = create_job.create_scheduler_job(
2528
TEST_PROJECT_ID, TEST_LOCATION, "my-service"
2629
)
27-
out, _ = capsys.readouterr()
28-
assert "Created job:" in out
30+
assert response.name
2931

30-
job_name = create_result.name.split("/")[-1]
31-
create_job.delete_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, job_name)
32+
job_name = response.name.split("/")[-1]
33+
delete_job.delete_scheduler_job(TEST_PROJECT_ID, TEST_LOCATION, job_name)
3234

3335
out, _ = capsys.readouterr()
3436
assert "Job deleted." in out

scheduler/snippets/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222

2323
# Define relative URI for job endpoint
2424
@app.route("/log_payload", methods=["POST"])
25-
def example_task_handler():
25+
def example_task_handler() -> str:
2626
"""Log the job payload."""
2727
payload = request.get_data(as_text=True) or "(empty payload)"
2828
print(f"Received job with payload: {payload}")
2929
return f"Printed job payload: {payload}"
3030

31-
3231
# [END cloudscheduler_app]
3332

3433

3534
@app.route("/")
36-
def hello():
35+
def hello() -> str:
3736
"""Basic index to verify app is serving."""
3837
return "Hello World!"
3938

scheduler/snippets/main_test.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from flask.testing import FlaskClient
1516
import pytest
1617

1718

1819
@pytest.fixture
19-
def app():
20+
def app() -> FlaskClient:
2021
import main
2122

2223
main.app.testing = True
2324
return main.app.test_client()
2425

2526

26-
def test_index(app):
27+
def test_index(app: FlaskClient) -> None:
2728
r = app.get("/")
2829
assert r.status_code == 200
2930

3031

31-
def test_log_payload(capsys, app):
32+
def test_log_payload(app: FlaskClient) -> None:
3233
payload = "test_payload"
34+
response = app.post("/log_payload", data=payload)
35+
assert response.status_code == 200
36+
assert payload in response.text
3337

34-
r = app.post("/log_payload", data=payload)
35-
assert r.status_code == 200
36-
37-
out, _ = capsys.readouterr()
38-
assert payload in out
39-
40-
41-
def test_empty_payload(capsys, app):
42-
r = app.post("/log_payload")
43-
assert r.status_code == 200
4438

45-
out, _ = capsys.readouterr()
46-
assert "empty payload" in out
39+
def test_empty_payload(app: FlaskClient) -> None:
40+
response = app.post("/log_payload")
41+
assert response.status_code == 200
42+
assert "empty payload" in response.text

0 commit comments

Comments
 (0)