From 14a57cdee1e18045baadea063d55d5d6db73fda5 Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Fri, 16 Aug 2024 14:05:54 +0200 Subject: [PATCH] Allow passing custom cloud tasks client options --- README.md | 21 +++++++++++++++++++++ config/cloud-tasks.php | 5 +++++ src/CloudTasksServiceProvider.php | 2 +- tests/CloudTasksApiTest.php | 18 ++++++++++++++++++ tests/Support/gcloud-key-dummy.json | 5 +++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/Support/gcloud-key-dummy.json diff --git a/README.md b/README.md index 9c043d5..0e3d221 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,27 @@ CloudTasksQueue::configureWorkerOptionsUsing(function (IncomingTask $task) { }); ``` +#### Use a custom credentials file + +Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file: + +```php +'client_options' => [ + 'credentials' => '/path/to/credentials.json', +] +``` + + +#### Modify CloudTasksClient options + +Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file: + +```php +'client_options' => [ + // custom options here +] +``` + ### How it works and differences Using Cloud Tasks as a Laravel queue driver is fundamentally different than other Laravel queue drivers, like Redis. diff --git a/config/cloud-tasks.php b/config/cloud-tasks.php index 4a77375..5a9b3a5 100644 --- a/config/cloud-tasks.php +++ b/config/cloud-tasks.php @@ -8,4 +8,9 @@ // If the application only dispatches jobs 'disable_task_handler' => env('CLOUD_TASKS_DISABLE_TASK_HANDLER', false), + + // Optionally, pass custom options to the Cloud Tasks API client + 'client_options' => [ + // 'credentials' => '/path/to/custom/credentials.json', + ], ]; diff --git a/src/CloudTasksServiceProvider.php b/src/CloudTasksServiceProvider.php index d6fbe1e..bc95afe 100644 --- a/src/CloudTasksServiceProvider.php +++ b/src/CloudTasksServiceProvider.php @@ -26,7 +26,7 @@ public function boot(): void private function registerClient(): void { $this->app->singleton(CloudTasksClient::class, function () { - return new CloudTasksClient(); + return new CloudTasksClient(config('cloud-tasks.client_options', [])); }); $this->app->singleton('cloud-tasks.worker', function (Application $app) { diff --git a/tests/CloudTasksApiTest.php b/tests/CloudTasksApiTest.php index 2dd6e2a..00c8b8e 100644 --- a/tests/CloudTasksApiTest.php +++ b/tests/CloudTasksApiTest.php @@ -42,6 +42,24 @@ protected function setUp(): void } + #[Test] + public function custom_client_options_can_be_added() + { + // Arrange + config()->set('cloud-tasks.client_options', [ + 'credentials' => __DIR__.'/Support/gcloud-key-dummy.json', + ]); + + // Act + $export = var_export(app(CloudTasksClient::class), true); + + // Assert + + // CloudTasksClient makes it a bit difficult to read its properties, so this will have to do... + $this->assertStringContainsString('info@stackkit.io', $export); + $this->assertStringContainsString('PRIVATE KEY', $export); + } + #[Test] public function test_create_task() { diff --git a/tests/Support/gcloud-key-dummy.json b/tests/Support/gcloud-key-dummy.json new file mode 100644 index 0000000..95530c7 --- /dev/null +++ b/tests/Support/gcloud-key-dummy.json @@ -0,0 +1,5 @@ +{ + "type": "service_account", + "client_email": "info@stackkit.io", + "private_key": "PRIVATE KEY" +}