Skip to content

Commit 790adcb

Browse files
feat(tasks): create http task with token (GoogleCloudPlatform#1112)
1 parent 060daf5 commit 790adcb

File tree

2 files changed

+111
-12
lines changed

2 files changed

+111
-12
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
21+
if ($argc < 6 || $argc > 7) {
22+
return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL SERVICE_ACCOUNT_EMAIL [PAYLOAD]\n", __FILE__);
23+
}
24+
list($_, $projectId, $locationId, $queueId, $url, $serviceAccountEmail) = $argv;
25+
$payload = isset($payload[6]) ? $payload[6] : null;
26+
27+
# [START cloud_tasks_create_http_task_with_token]
28+
use Google\Cloud\Tasks\V2\CloudTasksClient;
29+
use Google\Cloud\Tasks\V2\HttpMethod;
30+
use Google\Cloud\Tasks\V2\HttpRequest;
31+
use Google\Cloud\Tasks\V2\Task;
32+
use Google\Cloud\Tasks\V2\OidcToken;
33+
34+
/** Uncomment and populate these variables in your code */
35+
// $projectId = 'The Google project ID';
36+
// $locationId = 'The Location ID';
37+
// $queueId = 'The Cloud Tasks Queue ID';
38+
// $url = 'The full url path that the task request will be sent to.';
39+
// $serviceAccountEmail = 'The Cloud IAM service account to be used at the construction of the token';
40+
// $payload = 'The payload your task should carry to the task handler. Optional';
41+
42+
// Instantiate the client and queue name.
43+
$client = new CloudTasksClient();
44+
$queueName = $client->queueName($projectId, $locationId, $queueId);
45+
46+
// Add your service account email to construct the OIDC token
47+
// in order to add an authentication header to the request.
48+
$oidcToken = new OidcToken();
49+
$oidcToken->setServiceAccountEmail($serviceAccountEmail);
50+
51+
// Create an Http Request Object.
52+
$httpRequest = new HttpRequest();
53+
// The full url path that the task request will be sent to.
54+
$httpRequest->setUrl($url);
55+
// POST is the default HTTP method, but any HTTP method can be used.
56+
$httpRequest->setHttpMethod(HttpMethod::POST);
57+
//The oidcToken used to assert identity.
58+
$httpRequest->setOidcToken($oidcToken);
59+
// Setting a body value is only compatible with HTTP POST and PUT requests.
60+
if (isset($payload)) {
61+
$httpRequest->setBody($payload);
62+
}
63+
64+
// Create a Cloud Task object.
65+
$task = new Task();
66+
$task->setHttpRequest($httpRequest);
67+
68+
// Send request and print the task name.
69+
$response = $client->createTask($queueName, $task);
70+
printf('Created task %s' . PHP_EOL, $response->getName());
71+
72+
# [END cloud_tasks_create_http_task_with_token]

tasks/test/tasksTest.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
22
/**
3-
* Copyright 2019 Google LLC.
3+
* Copyright 2020 Google LLC.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -27,27 +27,54 @@ class TasksTest extends TestCase
2727
{
2828
use TestTrait;
2929

30-
public function testCreateHttpTask()
30+
private static $queue;
31+
private static $location;
32+
33+
public static function setUpBeforeClass()
3134
{
32-
$queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
33-
$location = $this->requireEnv('CLOUD_TASKS_LOCATION');
35+
self::$queue = self::requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
36+
self::$location = self::requireEnv('CLOUD_TASKS_LOCATION');
37+
}
3438

39+
public function testCreateHttpTask()
40+
{
3541
$output = $this->runSnippet('create_http_task', [
36-
$location,
37-
$queue,
38-
'http://example.com/taskhandler',
42+
self::$location,
43+
self::$queue,
44+
'https://example.com/taskhandler',
45+
'Task Details',
46+
]);
47+
48+
$taskNamePrefix = $this->getTaskNamePrefix();
49+
$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
50+
$this->assertContains($expectedOutput, $output);
51+
}
52+
53+
public function testCreateHttpTaskWithToken()
54+
{
55+
$output = $this->runSnippet('create_http_task_with_token', [
56+
self::$location,
57+
self::$queue,
58+
'https://example.com/taskhandler',
59+
3960
'Task Details',
4061
]);
41-
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
42-
self::$projectId,
43-
$location,
44-
$queue
45-
);
4662

63+
$taskNamePrefix = $this->getTaskNamePrefix();
4764
$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
4865
$this->assertContains($expectedOutput, $output);
4966
}
5067

68+
private function getTaskNamePrefix()
69+
{
70+
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
71+
self::$projectId,
72+
self::$location,
73+
self::$queue
74+
);
75+
return $taskNamePrefix;
76+
}
77+
5178
private function runSnippet($sampleName, $params = [])
5279
{
5380
$argv = array_merge([0, self::$projectId], array_values($params));

0 commit comments

Comments
 (0)