|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 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 | + * https://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 | +/* |
| 19 | + * GENERATED CODE WARNING |
| 20 | + * This file was automatically generated - do not edit! |
| 21 | + */ |
| 22 | + |
| 23 | +require_once __DIR__ . '/../../../vendor/autoload.php'; |
| 24 | + |
| 25 | +// [START workspaceevents_v1beta_generated_SubscriptionsService_CreateSubscription_sync] |
| 26 | +use Google\ApiCore\ApiException; |
| 27 | +use Google\ApiCore\OperationResponse; |
| 28 | +use Google\Apps\Events\Subscriptions\V1beta\Client\SubscriptionsServiceClient; |
| 29 | +use Google\Apps\Events\Subscriptions\V1beta\CreateSubscriptionRequest; |
| 30 | +use Google\Apps\Events\Subscriptions\V1beta\NotificationEndpoint; |
| 31 | +use Google\Apps\Events\Subscriptions\V1beta\Subscription; |
| 32 | +use Google\Rpc\Status; |
| 33 | + |
| 34 | +/** |
| 35 | + * Creates a Google Workspace subscription. To learn how to use this |
| 36 | + * method, see [Create a Google Workspace |
| 37 | + * subscription](https://developers.google.com/workspace/events/guides/create-subscription). |
| 38 | + * |
| 39 | + * |
| 40 | + * @param string $subscriptionTargetResource Immutable. The Google Workspace resource that's monitored for |
| 41 | + * events, formatted as the [full resource |
| 42 | + * name](https://google.aip.dev/122#full-resource-names). To learn about |
| 43 | + * target resources and the events that they support, see [Supported Google |
| 44 | + * Workspace |
| 45 | + * events](https://developers.google.com/workspace/events#supported-events). |
| 46 | + * |
| 47 | + * A user can only authorize your app to create one subscription for a given |
| 48 | + * target resource. If your app tries to create another subscription with the |
| 49 | + * same user credentials, the request returns an `ALREADY_EXISTS` error. |
| 50 | + * @param string $subscriptionEventTypesElement Unordered list. Input for creating a subscription. Otherwise, |
| 51 | + * output only. One or more types of events to receive about the target |
| 52 | + * resource. Formatted according to the CloudEvents specification. |
| 53 | + * |
| 54 | + * The supported event types depend on the target resource of your |
| 55 | + * subscription. For details, see [Supported Google Workspace |
| 56 | + * events](https://developers.google.com/workspace/events/guides#supported-events). |
| 57 | + * |
| 58 | + * By default, you also receive events about the [lifecycle of your |
| 59 | + * subscription](https://developers.google.com/workspace/events/guides/events-lifecycle). |
| 60 | + * You don't need to specify lifecycle events for this field. |
| 61 | + * |
| 62 | + * If you specify an event type that doesn't exist for the target resource, |
| 63 | + * the request returns an HTTP `400 Bad Request` status code. |
| 64 | + */ |
| 65 | +function create_subscription_sample( |
| 66 | + string $subscriptionTargetResource, |
| 67 | + string $subscriptionEventTypesElement |
| 68 | +): void { |
| 69 | + // Create a client. |
| 70 | + $subscriptionsServiceClient = new SubscriptionsServiceClient(); |
| 71 | + |
| 72 | + // Prepare the request message. |
| 73 | + $subscriptionEventTypes = [$subscriptionEventTypesElement,]; |
| 74 | + $subscriptionNotificationEndpoint = new NotificationEndpoint(); |
| 75 | + $subscription = (new Subscription()) |
| 76 | + ->setTargetResource($subscriptionTargetResource) |
| 77 | + ->setEventTypes($subscriptionEventTypes) |
| 78 | + ->setNotificationEndpoint($subscriptionNotificationEndpoint); |
| 79 | + $request = (new CreateSubscriptionRequest()) |
| 80 | + ->setSubscription($subscription); |
| 81 | + |
| 82 | + // Call the API and handle any network failures. |
| 83 | + try { |
| 84 | + /** @var OperationResponse $response */ |
| 85 | + $response = $subscriptionsServiceClient->createSubscription($request); |
| 86 | + $response->pollUntilComplete(); |
| 87 | + |
| 88 | + if ($response->operationSucceeded()) { |
| 89 | + /** @var Subscription $result */ |
| 90 | + $result = $response->getResult(); |
| 91 | + printf('Operation successful with response data: %s' . PHP_EOL, $result->serializeToJsonString()); |
| 92 | + } else { |
| 93 | + /** @var Status $error */ |
| 94 | + $error = $response->getError(); |
| 95 | + printf('Operation failed with error data: %s' . PHP_EOL, $error->serializeToJsonString()); |
| 96 | + } |
| 97 | + } catch (ApiException $ex) { |
| 98 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Helper to execute the sample. |
| 104 | + * |
| 105 | + * This sample has been automatically generated and should be regarded as a code |
| 106 | + * template only. It will require modifications to work: |
| 107 | + * - It may require correct/in-range values for request initialization. |
| 108 | + * - It may require specifying regional endpoints when creating the service client, |
| 109 | + * please see the apiEndpoint client configuration option for more details. |
| 110 | + */ |
| 111 | +function callSample(): void |
| 112 | +{ |
| 113 | + $subscriptionTargetResource = '[TARGET_RESOURCE]'; |
| 114 | + $subscriptionEventTypesElement = '[EVENT_TYPES]'; |
| 115 | + |
| 116 | + create_subscription_sample($subscriptionTargetResource, $subscriptionEventTypesElement); |
| 117 | +} |
| 118 | +// [END workspaceevents_v1beta_generated_SubscriptionsService_CreateSubscription_sync] |
0 commit comments