Skip to content

Commit cd47297

Browse files
arithmetic1728busunkim96
authored andcommitted
feat!: migrate to use microgen (#38)
* feat!: migrate to use microgen * Update UPGRADING.md Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 7048f21 commit cd47297

File tree

4 files changed

+80
-84
lines changed

4 files changed

+80
-84
lines changed

cloud-tasks/snippets/create_http_task.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
import argparse
1818

1919

20-
def create_http_task(project,
21-
queue,
22-
location,
23-
url,
24-
payload=None,
25-
in_seconds=None,
26-
task_name=None):
20+
def create_http_task(
21+
project, queue, location, url, payload=None, in_seconds=None, task_name=None
22+
):
2723
# [START cloud_tasks_create_http_task]
2824
"""Create a task for a given queue with an arbitrary payload."""
2925

@@ -47,23 +43,23 @@ def create_http_task(project,
4743

4844
# Construct the request body.
4945
task = {
50-
'http_request': { # Specify the type of request.
51-
'http_method': 'POST',
52-
'url': url # The full url path that the task will be sent to.
53-
}
46+
"http_request": { # Specify the type of request.
47+
"http_method": tasks_v2.HttpMethod.POST,
48+
"url": url, # The full url path that the task will be sent to.
49+
}
5450
}
5551
if payload is not None:
5652
if isinstance(payload, dict):
5753
# Convert dict to JSON string
5854
payload = json.dumps(payload)
5955
# specify http content-type to application/json
60-
task['http_request']['headers'] = {'Content-type': 'application/json'}
56+
task["http_request"]["headers"] = {"Content-type": "application/json"}
6157

6258
# The API expects a payload of type bytes.
6359
converted_payload = payload.encode()
6460

6561
# Add the payload to the request.
66-
task['http_request']['body'] = converted_payload
62+
task["http_request"]["body"] = converted_payload
6763

6864
if in_seconds is not None:
6965
# Convert "seconds from now" into an rfc3339 datetime string.
@@ -74,65 +70,65 @@ def create_http_task(project,
7470
timestamp.FromDatetime(d)
7571

7672
# Add the timestamp to the tasks.
77-
task['schedule_time'] = timestamp
73+
task["schedule_time"] = timestamp
7874

7975
if task_name is not None:
8076
# Add the name to tasks.
81-
task['name'] = task_name
77+
task["name"] = task_name
8278

8379
# Use the client to build and send the task.
84-
response = client.create_task(parent, task)
80+
response = client.create_task(request={"parent": parent, "task": task})
8581

86-
print('Created task {}'.format(response.name))
82+
print("Created task {}".format(response.name))
8783
# [END cloud_tasks_create_http_task]
8884
return response
8985

9086

91-
if __name__ == '__main__':
87+
if __name__ == "__main__":
9288
parser = argparse.ArgumentParser(
9389
description=create_http_task.__doc__,
94-
formatter_class=argparse.RawDescriptionHelpFormatter)
90+
formatter_class=argparse.RawDescriptionHelpFormatter,
91+
)
9592

9693
parser.add_argument(
97-
'--project',
98-
help='Project of the queue to add the task to.',
99-
required=True,
94+
"--project", help="Project of the queue to add the task to.", required=True,
10095
)
10196

10297
parser.add_argument(
103-
'--queue',
104-
help='ID (short name) of the queue to add the task to.',
98+
"--queue",
99+
help="ID (short name) of the queue to add the task to.",
105100
required=True,
106101
)
107102

108103
parser.add_argument(
109-
'--location',
110-
help='Location of the queue to add the task to.',
111-
required=True,
104+
"--location", help="Location of the queue to add the task to.", required=True,
112105
)
113106

114107
parser.add_argument(
115-
'--url',
116-
help='The full url path that the request will be sent to.',
108+
"--url",
109+
help="The full url path that the request will be sent to.",
117110
required=True,
118111
)
119112

120113
parser.add_argument(
121-
'--payload',
122-
help='Optional payload to attach to the push queue.'
114+
"--payload", help="Optional payload to attach to the push queue."
123115
)
124116

125117
parser.add_argument(
126-
'--in_seconds', type=int,
127-
help='The number of seconds from now to schedule task attempt.'
118+
"--in_seconds",
119+
type=int,
120+
help="The number of seconds from now to schedule task attempt.",
128121
)
129122

130-
parser.add_argument(
131-
'--task_name',
132-
help='Task name of the task to create'
133-
)
123+
parser.add_argument("--task_name", help="Task name of the task to create")
134124
args = parser.parse_args()
135125

136126
create_http_task(
137-
args.project, args.queue, args.location, args.url,
138-
args.payload, args.in_seconds, args.task_name)
127+
args.project,
128+
args.queue,
129+
args.location,
130+
args.url,
131+
args.payload,
132+
args.in_seconds,
133+
args.task_name,
134+
)

cloud-tasks/snippets/create_http_task_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@
2020

2121
import create_http_task
2222

23-
TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
24-
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
25-
TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}'
23+
TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
24+
TEST_LOCATION = os.getenv("TEST_QUEUE_LOCATION", "us-central1")
25+
TEST_QUEUE_NAME = f"my-queue-{uuid.uuid4().hex}"
2626

2727

2828
@pytest.fixture()
2929
def test_queue():
3030
client = tasks_v2.CloudTasksClient()
31-
parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION)
31+
parent = f"projects/{TEST_PROJECT_ID}/locations/{TEST_LOCATION}"
3232
queue = {
3333
# The fully qualified path to the queue
34-
'name': client.queue_path(
35-
TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
34+
"name": client.queue_path(TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
3635
}
37-
q = client.create_queue(parent, queue)
36+
q = client.create_queue(request={"parent": parent, "queue": queue})
3837

3938
yield q
4039

41-
client.delete_queue(q.name)
40+
client.delete_queue(request={"name": q.name})
4241

4342

4443
def test_create_http_task(test_queue):
45-
url = 'https://example.com/task_handler'
44+
url = "https://example.com/task_handler"
4645
result = create_http_task.create_http_task(
47-
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url)
46+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url
47+
)
4848
assert TEST_QUEUE_NAME in result.name

cloud-tasks/snippets/create_http_task_with_token.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
import datetime
1818

1919

20-
def create_http_task(project,
21-
queue,
22-
location,
23-
url,
24-
service_account_email,
25-
payload=None,
26-
in_seconds=None,
27-
task_name=None):
20+
def create_http_task(
21+
project,
22+
queue,
23+
location,
24+
url,
25+
service_account_email,
26+
payload=None,
27+
in_seconds=None,
28+
task_name=None,
29+
):
2830
# [START cloud_tasks_create_http_task_with_token]
2931
"""Create a task for a given queue with an arbitrary payload."""
3032

@@ -47,21 +49,19 @@ def create_http_task(project,
4749

4850
# Construct the request body.
4951
task = {
50-
'http_request': { # Specify the type of request.
51-
'http_method': 'POST',
52-
'url': url, # The full url path that the task will be sent to.
53-
'oidc_token': {
54-
'service_account_email': service_account_email
55-
}
56-
}
52+
"http_request": { # Specify the type of request.
53+
"http_method": tasks_v2.HttpMethod.POST,
54+
"url": url, # The full url path that the task will be sent to.
55+
"oidc_token": {"service_account_email": service_account_email},
56+
}
5757
}
5858

5959
if payload is not None:
6060
# The API expects a payload of type bytes.
6161
converted_payload = payload.encode()
6262

6363
# Add the payload to the request.
64-
task['http_request']['body'] = converted_payload
64+
task["http_request"]["body"] = converted_payload
6565

6666
if in_seconds is not None:
6767
# Convert "seconds from now" into an rfc3339 datetime string.
@@ -72,15 +72,17 @@ def create_http_task(project,
7272
timestamp.FromDatetime(d)
7373

7474
# Add the timestamp to the tasks.
75-
task['schedule_time'] = timestamp
75+
task["schedule_time"] = timestamp
7676

7777
if task_name is not None:
7878
# Add the name to tasks.
79-
task['name'] = task_name
79+
task["name"] = task_name
8080

8181
# Use the client to build and send the task.
82-
response = client.create_task(parent, task)
82+
response = client.create_task(request={"parent": parent, "task": task})
8383

84-
print('Created task {}'.format(response.name))
84+
print("Created task {}".format(response.name))
8585
return response
86+
87+
8688
# [END cloud_tasks_create_http_task_with_token]

cloud-tasks/snippets/create_http_task_with_token_test.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,32 @@
2020

2121
import create_http_task_with_token
2222

23-
TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
24-
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
25-
TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}'
23+
TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
24+
TEST_LOCATION = os.getenv("TEST_QUEUE_LOCATION", "us-central1")
25+
TEST_QUEUE_NAME = f"my-queue-{uuid.uuid4().hex}"
2626
TEST_SERVICE_ACCOUNT = (
27-
'test-run-invoker@python-docs-samples-tests.iam.gserviceaccount.com')
27+
"test-run-invoker@python-docs-samples-tests.iam.gserviceaccount.com"
28+
)
2829

2930

3031
@pytest.fixture()
3132
def test_queue():
3233
client = tasks_v2.CloudTasksClient()
33-
parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION)
34+
parent = f"projects/{TEST_PROJECT_ID}/locations/{TEST_LOCATION}"
3435
queue = {
3536
# The fully qualified path to the queue
36-
'name': client.queue_path(
37-
TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
37+
"name": client.queue_path(TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
3838
}
39-
q = client.create_queue(parent, queue)
39+
q = client.create_queue(request={"parent": parent, "queue": queue})
4040

4141
yield q
4242

43-
client.delete_queue(q.name)
43+
client.delete_queue(request={"name": q.name})
4444

4545

4646
def test_create_http_task_with_token(test_queue):
47-
url = 'https://example.com/task_handler'
48-
result = create_http_task_with_token.create_http_task(TEST_PROJECT_ID,
49-
TEST_QUEUE_NAME,
50-
TEST_LOCATION,
51-
url,
52-
TEST_SERVICE_ACCOUNT)
47+
url = "https://example.com/task_handler"
48+
result = create_http_task_with_token.create_http_task(
49+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url, TEST_SERVICE_ACCOUNT
50+
)
5351
assert TEST_QUEUE_NAME in result.name

0 commit comments

Comments
 (0)