Skip to content
Merged
72 changes: 72 additions & 0 deletions tasks/src/create_http_task_with_token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright 2020 Google LLC.
*
* 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.
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if ($argc < 6 || $argc > 7) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL SERVICE_ACCOUNT_EMAIL [PAYLOAD]\n", __FILE__);
}
list($_, $projectId, $locationId, $queueId, $url, $serviceAccountEmail) = $argv;
$payload = isset($payload[6]) ? $payload[6] : null;

# [START cloud_tasks_create_http_task_with_token]
use Google\Cloud\Tasks\V2\CloudTasksClient;
use Google\Cloud\Tasks\V2\HttpMethod;
use Google\Cloud\Tasks\V2\HttpRequest;
use Google\Cloud\Tasks\V2\Task;
use Google\Cloud\Tasks\V2\OidcToken;

/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $locationId = 'The Location ID';
// $queueId = 'The Cloud Tasks Queue ID';
// $url = 'The full url path that the task request will be sent to.';
// $serviceAccountEmail = 'The Cloud IAM service account to be used at the construction of the token';
// $payload = 'The payload your task should carry to the task handler. Optional';

// Instantiate the client and queue name.
$client = new CloudTasksClient();
$queueName = $client->queueName($projectId, $locationId, $queueId);

// Add your service account email to construct the OIDC token
// in order to add an authentication header to the request.
$oidcToken = new OidcToken();
$oidcToken->setServiceAccountEmail($serviceAccountEmail);

// Create an Http Request Object.
$httpRequest = new HttpRequest();
// The full url path that the task request will be sent to.
$httpRequest->setUrl($url);
// POST is the default HTTP method, but any HTTP method can be used.
$httpRequest->setHttpMethod(HttpMethod::POST);
//The oidcToken used to assert identity.
$httpRequest->setOidcToken($oidcToken);
// Setting a body value is only compatible with HTTP POST and PUT requests.
if (isset($payload)) {
$httpRequest->setBody($payload);
}

// Create a Cloud Task object.
$task = new Task();
$task->setHttpRequest($httpRequest);

// Send request and print the task name.
$response = $client->createTask($queueName, $task);
printf('Created task %s' . PHP_EOL, $response->getName());

# [END cloud_tasks_create_http_task_with_token]
51 changes: 39 additions & 12 deletions tasks/test/tasksTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2019 Google LLC.
* Copyright 2020 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,27 +27,54 @@ class TasksTest extends TestCase
{
use TestTrait;

public function testCreateHttpTask()
private static $queue;
private static $location;

public static function setUpBeforeClass()
{
$queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
$location = $this->requireEnv('CLOUD_TASKS_LOCATION');
self::$queue = self::requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
self::$location = self::requireEnv('CLOUD_TASKS_LOCATION');
}

public function testCreateHttpTask()
{
$output = $this->runSnippet('create_http_task', [
$location,
$queue,
'http://example.com/taskhandler',
self::$location,
self::$queue,
'https://example.com/taskhandler',
'Task Details',
]);

$taskNamePrefix = $this->getTaskNamePrefix();
$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
$this->assertContains($expectedOutput, $output);
}

public function testCreateHttpTaskWithToken()
{
$output = $this->runSnippet('create_http_task_with_token', [
self::$location,
self::$queue,
'https://example.com/taskhandler',
'[email protected]',
'Task Details',
]);
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
self::$projectId,
$location,
$queue
);

$taskNamePrefix = $this->getTaskNamePrefix();
$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
$this->assertContains($expectedOutput, $output);
}

private function getTaskNamePrefix()
{
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
self::$projectId,
self::$location,
self::$queue
);
return $taskNamePrefix;
}

private function runSnippet($sampleName, $params = [])
{
$argv = array_merge([0, self::$projectId], array_values($params));
Expand Down