Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme/projectID detection #1391

Merged
merged 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Bigtable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ $ composer require google/cloud
This component supports both REST over HTTP/1.1 and gRPC. In order to take advantage of the benefits offered by gRPC (such as streaming methods)
please see our [gRPC installation guide](https://cloud.google.com/php/grpc).

### Notable Client Differences

The handwritten client offered by this package differs from the others in `google-cloud-php` in that it more directly wraps our generated clients.
This means some of the idioms and configuration options you are used to may differ slightly. The most notable differences are outlined below:

- A key file is now provided through the `credentials` configuration option as opposed to either `keyFile` or `keyFilePath`.
- There is now more granular control over retry logic. Please see [the `bigtable_client_config.json` file](https://github.com/googleapis/google-cloud-php/blob/master/Bigtable/src/V2/resources/bigtable_client_config.json)
for an example of the configuration which can be passed into the client at construction time.
- Exceptions triggered at the network level utilize the base class `Google\ApiCore\ApiException` as opposed to `Google\Cloud\Core\ServiceException`.
- The `authHttpHandler` and `httpHandler` client configuration options are now provided through `$credentialsConfig['authHttpHandler']`
and `$transportConfig['httpHandler']`, respectively. Additionally, please note the `httpHandler` should now return an implementation of [Guzzle's `PromiseInterface`](https://github.com/guzzle/promises/blob/master/src/PromiseInterface.php).

### Authentication

Please see our [Authentication guide](https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/AUTHENTICATION.md) for more information
on authenticating your client. Once authenticated, you'll be ready to start making requests.

When going through the authentication guide, please take note that the handwritten client for this package will more closely follow the conventions
outlined for the generated clients.

### Sample

```php
Expand Down
53 changes: 42 additions & 11 deletions Bigtable/src/BigtableClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Google\ApiCore\ValidationException;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Bigtable\V2\BigtableClient as GapicClient;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\ArrayTrait;

/**
* Google Cloud Bigtable is Google's NoSQL Big Data database service.
Expand All @@ -38,7 +38,7 @@
*/
class BigtableClient
{
use ClientTrait;
use ArrayTrait;

const VERSION = '0.5.3';

Expand All @@ -47,6 +47,11 @@ class BigtableClient
*/
private $gapicClient;

/**
* @var string
*/
private $projectId;

/**
* Create a Bigtable client.
*
Expand Down Expand Up @@ -93,23 +98,26 @@ class BigtableClient
* 'grpc' => [...],
* 'rest' => [...]
* ];
* See {@see Google\ApiCore\Transport\GrpcTransport} and
* {@see Google\ApiCore\Transport\RestTransport} for
* the supported options.
* See the `build` method on {@see Google\ApiCore\Transport\GrpcTransport}
* and {@see Google\ApiCore\Transport\RestTransport} for the
* supported options.
* }
* @throws ValidationException
*/
public function __construct(array $config = [])
{
if (!isset($config['transportConfig']['grpc']['stubOpts'])) {
// Workaround for large messages.
$config['transportConfig']['grpc']['stubOpts'] = [
'grpc.max_send_message_length' => -1,
'grpc.max_receive_message_length' => -1
];
$config['transportConfig']['grpc']['stubOpts'] = [];
}

$this->projectId = $this->detectProjectId($config);
// Workaround for large messages.
$config['transportConfig']['grpc']['stubOpts'] += [
'grpc.max_send_message_length' => -1,
'grpc.max_receive_message_length' => -1
];

$this->projectId = $this->pluck('projectId', $config, false)
?: $this->detectProjectId();
$this->gapicClient = new GapicClient($config);
}

Expand Down Expand Up @@ -141,4 +149,27 @@ public function table($instanceId, $tableId, array $options = [])
$options
);
}

/**
* Attempts to detect the project ID.
*
* @todo Add better support for detecting the project ID (check keyFile/GCE metadata server).
* @return string
* @throws ValidationException If a project ID cannot be detected.
*/
private function detectProjectId()
{
if (getenv('GOOGLE_CLOUD_PROJECT')) {
return getenv('GOOGLE_CLOUD_PROJECT');
}

if (getenv('GCLOUD_PROJECT')) {
return getenv('GCLOUD_PROJECT');
}

throw new ValidationException(
ajaaym marked this conversation as resolved.
Show resolved Hide resolved
'No project ID was provided, ' .
'and we were unable to detect a default project ID.'
);
}
}