|
| 1 | +# Migrating Google Cloud Clients to the New Surface |
| 2 | + |
| 3 | +**Document Summary** |
| 4 | + |
| 5 | + * Google Cloud PHP Client Libraries are releasing new major versions (v2) to |
| 6 | + introduce new surface changes. |
| 7 | + * The PHP Team at Google has developed a tool to automatically upgrade clients |
| 8 | + (see [`ClientUpgradeFixer`][client_upgrade_fixer]). |
| 9 | + |
| 10 | +## WHAT are the new Cloud Clients? |
| 11 | + |
| 12 | +The new Cloud Clients are in the namespace `\Client\`, whereas the previous |
| 13 | +clients are one directory above with the same name. For example: |
| 14 | + |
| 15 | +```php |
| 16 | +// This is the "new" client |
| 17 | +use Google\Cloud\AutoMl\V1\Client\AutoMlClient; |
| 18 | + |
| 19 | +// This is the deprecated client (marked with @deprecated) |
| 20 | +use Google\Cloud\AutoMl\V1\AutoMlClient; |
| 21 | +``` |
| 22 | + |
| 23 | +The main difference is that RPC methods which used to take a varying number of |
| 24 | +required arguments plus an array of optional arguments, now only take a |
| 25 | +_single_ `Request` object: |
| 26 | + |
| 27 | +```php |
| 28 | +// Create a client. |
| 29 | +$autoMlClient = new AutoMlClient(); |
| 30 | + |
| 31 | +// Prepare the request message. |
| 32 | +$dataset = new Dataset(); |
| 33 | +$updateMask = new FieldMask(); |
| 34 | +$request = (new UpdateDatasetRequest()) |
| 35 | + ->setDataset($dataset) |
| 36 | + ->setUpdateMask($updateMask); |
| 37 | + |
| 38 | +// Call the API and handle any network failures. |
| 39 | +try { |
| 40 | + /** @var Dataset $response */ |
| 41 | + $response = $autoMlClient->updateDataset($request); |
| 42 | + printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString()); |
| 43 | +} catch (ApiException $ex) { |
| 44 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### RPCs use CallOptions |
| 49 | + |
| 50 | +The new surface RPC methods take an optional array of |
| 51 | +[CallOptions][call_options] as the second argument. These are similar to how |
| 52 | +the `$optionalArgs` were used in the previous surface, but the new `CallOptions` |
| 53 | +_only_ contain options for the call itself, whereas the previous `$optionalArgs` |
| 54 | +also held the optional fields for the request body: |
| 55 | + |
| 56 | +```php |
| 57 | +// To set call-time options, such as headers, timeouts, and retry options, |
| 58 | +// pass them in as the second argument |
| 59 | +$callOptions = ['timeoutMillis' => 20]; |
| 60 | +$response = $autoMlClient->updateDataset($request, $callOptions); |
| 61 | +``` |
| 62 | + |
| 63 | +[call_options]: https://github.com/googleapis/gax-php/blob/main/src/Options/CallOptions.php |
| 64 | + |
| 65 | +### Requests have static `::build` methods |
| 66 | + |
| 67 | +Using Request objects directly can make it more difficult to quickly draft out |
| 68 | +the necessary code to deliver an RPC. To mitigate this friction, a static |
| 69 | +`::build` method is now generated when one or more |
| 70 | +[method signature annotations](https://google.aip.dev/client-libraries/4232) |
| 71 | +exist on the RPC. |
| 72 | + |
| 73 | +Any request which has recommended parameters defined in its proto will include a |
| 74 | +`::build` method, so that these parameters are easily discoverable: |
| 75 | + |
| 76 | +```php |
| 77 | +// Create the RPC request using the static "::build" method |
| 78 | +$request = UpdateDatasetRequest::build($dataset, $updateMask); |
| 79 | +$response = $autoMlClient->updateDataset($request); |
| 80 | +``` |
| 81 | + |
| 82 | +## HOW should I upgrade? |
| 83 | + |
| 84 | +The changes are mostly straightforward, and at a minimum require the following: |
| 85 | + |
| 86 | + - Update Google Cloud clients to use the new client namespace by appending |
| 87 | + `\Client` to the existing namespace. |
| 88 | + - Update RPC calls to accept the corresponding `Request` object. |
| 89 | + |
| 90 | +**NOTE**: Client streaming calls do not require a `Request` object. |
| 91 | + |
| 92 | +### Automatically upgrade code using the `ClientUpgradeFixer` tool |
| 93 | + |
| 94 | +To help migrate code to the new client surface, we've written a |
| 95 | +[ClientUpgradeFixer][client_upgrade_fixer] to scan code and upgrade it to match |
| 96 | +the new client surface. This tool is not guaranteed to work, so be sure to test |
| 97 | +everything that it changes thoroughly. Read more about how to install and run |
| 98 | +the tool in its [README][client_upgrade_fixer]. |
| 99 | + |
| 100 | +The ClientUpgradeFixer uses [PHP Coding Standards Fixer][cs_fixer] to upgrade |
| 101 | +code to use the new client surface: |
| 102 | + |
| 103 | +```bash |
| 104 | +# run the CS fixer for that directory using the config above |
| 105 | +vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.google.php --dry-run --diff /path/to/my/project |
| 106 | +``` |
| 107 | + |
| 108 | +This will output a diff of the changes. Remove `--dry-run` from the above |
| 109 | +command to apply the changes automatically. |
| 110 | + |
| 111 | + |
| 112 | +```diff |
| 113 | +-use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 114 | ++use Google\Cloud\Dlp\V2\Client\DlpServiceClient; |
| 115 | ++use Google\Cloud\Dlp\V2\CreateDlpJobRequest; |
| 116 | + use Google\Cloud\Dlp\V2\InspectConfig; |
| 117 | + use Google\Cloud\Dlp\V2\InspectJobConfig; |
| 118 | + use Google\Cloud\Dlp\V2\Likelihood; |
| 119 | ++use Google\Cloud\Dlp\V2\ListInfoTypesRequest; |
| 120 | + use Google\Cloud\Dlp\V2\StorageConfig; |
| 121 | + |
| 122 | + // Instantiate a client. |
| 123 | + $dlp = new DlpServiceClient(); |
| 124 | + |
| 125 | + // optional args array (variable) |
| 126 | +-$infoTypes = $dlp->listInfoTypes($parent); |
| 127 | ++$request = (new ListInfoTypesRequest()); |
| 128 | ++$infoTypes = $dlp->listInfoTypes($request); |
| 129 | + |
| 130 | + // optional args array (inline array) |
| 131 | +-$job = $dlp->createDlpJob($parent, ['jobId' => 'abc', 'locationId' => 'def']); |
| 132 | ++$request2 = (new CreateDlpJobRequest()) |
| 133 | ++ ->setParent($parent) |
| 134 | ++ ->setJobId('abc') |
| 135 | ++ ->setLocationId('def'); |
| 136 | ++$job = $dlp->createDlpJob($request2); |
| 137 | +``` |
| 138 | + |
| 139 | +[cs_fixer]: https://cs.symfony.com/ |
| 140 | +[client_upgrade_fixer]: https://github.com/GoogleCloudPlatform/php-tools/blob/main/src/Fixers/ClientUpgradeFixer/README.md |
| 141 | + |
| 142 | +## Feedback |
| 143 | + |
| 144 | +Your feedback is important to us! Please continue to provide us with any |
| 145 | +thoughts and questions in the [Issues][google-cloud-issues] section of this |
| 146 | +repository. |
| 147 | + |
| 148 | +[google-cloud-issues]: https://github.com/googleapis/google-cloud-php/issues |
0 commit comments