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

Partial veneer GAPIC refresh #600

Merged
merged 8 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
234 changes: 3 additions & 231 deletions src/Vision/V1/ImageAnnotatorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,238 +30,10 @@

namespace Google\Cloud\Vision\V1;

use Google\GAX\AgentHeaderDescriptor;
use Google\GAX\ApiCallable;
use Google\GAX\CallSettings;
use Google\GAX\GrpcConstants;
use Google\GAX\GrpcCredentialsHelper;

/**
* Service Description: Service that performs Google Cloud Vision API detection tasks over client
* images, such as face, landmark, logo, label, and text detection. The
* ImageAnnotator service returns detected entities from the images.
*
* EXPERIMENTAL: this client library class has not yet been declared beta. This class may change
* more frequently than those which have been declared beta or 1.0, including changes which break
* backwards compatibility.
*
* This class provides the ability to make remote calls to the backing service through method
* calls that map to API methods. Sample code to get started:
*
* ```
* try {
* $imageAnnotatorClient = new ImageAnnotatorClient();
* $requests = [];
* $response = $imageAnnotatorClient->batchAnnotateImages($requests);
* } finally {
* $imageAnnotatorClient->close();
* }
* ```
*
* @experimental
* {@inheritdoc}
*/
class ImageAnnotatorClient
class ImageAnnotatorClient extends ImageAnnotatorGapicClient
{
/**
* The default address of the service.
*/
const SERVICE_ADDRESS = 'vision.googleapis.com';

/**
* The default port of the service.
*/
const DEFAULT_SERVICE_PORT = 443;

/**
* The default timeout for non-retrying methods.
*/
const DEFAULT_TIMEOUT_MILLIS = 30000;

/**
* The name of the code generator, to be included in the agent header.
*/
const CODEGEN_NAME = 'gapic';

/**
* The code generator version, to be included in the agent header.
*/
const CODEGEN_VERSION = '0.0.5';

private $grpcCredentialsHelper;
private $imageAnnotatorStub;
private $scopes;
private $defaultCallSettings;
private $descriptors;

private static function getGapicVersion()
{
if (file_exists(__DIR__.'/../VERSION')) {
return trim(file_get_contents(__DIR__.'/../VERSION'));
} elseif (class_exists('\Google\Cloud\ServiceBuilder')) {
return \Google\Cloud\ServiceBuilder::VERSION;
} else {
return;
}
}

// TODO(garrettjones): add channel (when supported in gRPC)
/**
* Constructor.
*
* @param array $options {
* Optional. Options for configuring the service API wrapper.
*
* @type string $serviceAddress The domain name of the API remote host.
* Default 'vision.googleapis.com'.
* @type mixed $port The port on which to connect to the remote host. Default 443.
* @type \Grpc\ChannelCredentials $sslCreds
* A `ChannelCredentials` for use with an SSL-enabled channel.
* Default: a credentials object returned from
* \Grpc\ChannelCredentials::createSsl()
* @type array $scopes A string array of scopes to use when acquiring credentials.
* Default the scopes for the Google Cloud Vision API.
* @type array $retryingOverride
* An associative array of string => RetryOptions, where the keys
* are method names (e.g. 'createFoo'), that overrides default retrying
* settings. A value of null indicates that the method in question should
* not retry.
* @type int $timeoutMillis The timeout in milliseconds to use for calls
* that don't use retries. For calls that use retries,
* set the timeout in RetryOptions.
* Default: 30000 (30 seconds)
* @type \Google\Auth\CredentialsLoader $credentialsLoader
* A CredentialsLoader object created using the
* Google\Auth library.
* }
* @experimental
*/
public function __construct($options = [])
{
$defaultOptions = [
'serviceAddress' => self::SERVICE_ADDRESS,
'port' => self::DEFAULT_SERVICE_PORT,
'scopes' => [
'https://www.googleapis.com/auth/cloud-platform',
],
'retryingOverride' => null,
'timeoutMillis' => self::DEFAULT_TIMEOUT_MILLIS,
'libName' => null,
'libVersion' => null,
];
$options = array_merge($defaultOptions, $options);

$gapicVersion = $options['libVersion'] ?: self::getGapicVersion();

$headerDescriptor = new AgentHeaderDescriptor([
'libName' => $options['libName'],
'libVersion' => $options['libVersion'],
'gapicVersion' => $gapicVersion,
]);

$defaultDescriptors = ['headerDescriptor' => $headerDescriptor];
$this->descriptors = [
'batchAnnotateImages' => $defaultDescriptors,
];

$clientConfigJsonString = file_get_contents(__DIR__.'/resources/image_annotator_client_config.json');
$clientConfig = json_decode($clientConfigJsonString, true);
$this->defaultCallSettings =
CallSettings::load(
'google.cloud.vision.v1.ImageAnnotator',
$clientConfig,
$options['retryingOverride'],
GrpcConstants::getStatusCodeNames(),
$options['timeoutMillis']
);

$this->scopes = $options['scopes'];

$createStubOptions = [];
if (array_key_exists('sslCreds', $options)) {
$createStubOptions['sslCreds'] = $options['sslCreds'];
}
$grpcCredentialsHelperOptions = array_diff_key($options, $defaultOptions);
$this->grpcCredentialsHelper = new GrpcCredentialsHelper($this->scopes, $grpcCredentialsHelperOptions);

$createImageAnnotatorStubFunction = function ($hostname, $opts) {
return new ImageAnnotatorGrpcClient($hostname, $opts);
};
if (array_key_exists('createImageAnnotatorStubFunction', $options)) {
$createImageAnnotatorStubFunction = $options['createImageAnnotatorStubFunction'];
}
$this->imageAnnotatorStub = $this->grpcCredentialsHelper->createStub(
$createImageAnnotatorStubFunction,
$options['serviceAddress'],
$options['port'],
$createStubOptions
);
}

/**
* Run image detection and annotation for a batch of images.
*
* Sample code:
* ```
* try {
* $imageAnnotatorClient = new ImageAnnotatorClient();
* $requests = [];
* $response = $imageAnnotatorClient->batchAnnotateImages($requests);
* } finally {
* $imageAnnotatorClient->close();
* }
* ```
*
* @param AnnotateImageRequest[] $requests Individual image annotation requests for this batch.
* @param array $optionalArgs {
* Optional.
*
* @type \Google\GAX\RetrySettings $retrySettings
* Retry settings to use for this call. If present, then
* $timeoutMillis is ignored.
* @type int $timeoutMillis
* Timeout to use for this call. Only used if $retrySettings
* is not set.
* }
*
* @return \Google\Cloud\Vision\V1\BatchAnnotateImagesResponse
*
* @throws \Google\GAX\ApiException if the remote call fails
* @experimental
*/
public function batchAnnotateImages($requests, $optionalArgs = [])
{
$request = new BatchAnnotateImagesRequest();
$request->setRequests($requests);

$mergedSettings = $this->defaultCallSettings['batchAnnotateImages']->merge(
new CallSettings($optionalArgs)
);
$callable = ApiCallable::createApiCall(
$this->imageAnnotatorStub,
'BatchAnnotateImages',
$mergedSettings,
$this->descriptors['batchAnnotateImages']
);

return $callable(
$request,
[],
['call_credentials_callback' => $this->createCredentialsCallback()]);
}

/**
* Initiates an orderly shutdown in which preexisting calls continue but new
* calls are immediately cancelled.
*
* @experimental
*/
public function close()
{
$this->imageAnnotatorStub->close();
}

private function createCredentialsCallback()
{
return $this->grpcCredentialsHelper->createCallCredentialsCallback();
}
// This class is intentionally empty, and is intended to hold manual additions to the generated {@see ImageAnnotatorClientImpl} class.
}
Loading