diff --git a/src/Connector.php b/src/Connector.php index 69c6170..b8d59fa 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -19,11 +19,10 @@ class Connector extends ControllerBase { /** * Constructs a Connector object. * - * @param \Drupal\google_vision\GoogleVisionAPI $google_VisionAPI. + * @param \Drupal\google_vision\GoogleVisionAPI $google_vision. */ - public function __construct(GoogleVisionAPI $google_VisionAPI) { - - $this->googleVisionAPI = $google_VisionAPI; + public function __construct(GoogleVisionAPI $google_vision) { + $this->googleVisionAPI = $google_vision; } /** @@ -34,7 +33,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function labelDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLabels($filepath); return $req; } @@ -47,7 +45,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function landmarkDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLandmark($filepath); return $req; } @@ -60,7 +57,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function logoDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForLogo($filepath); return $req; } @@ -73,7 +69,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function safeSearchDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForSafeSearch($filepath); return $req; } @@ -86,7 +81,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function opticalCharacterRecognition($filepath) { - $req = $this->googleVisionAPI->makeRequestForOCR($filepath); return $req; } @@ -99,7 +93,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function faceDetection($filepath) { - $req = $this->googleVisionAPI->makeRequestForFaceDetection($filepath); return $req; } @@ -112,7 +105,6 @@ class Connector extends ControllerBase { * @return array $req. */ public function imageAttributes($filepath) { - $req = $this->googleVisionAPI->makeRequestForImageAttributes($filepath); return $req; } @@ -121,7 +113,6 @@ class Connector extends ControllerBase { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('google_vision.api') ); } diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php index 5603862..abd4a85 100755 --- a/src/GoogleVisionAPI.php +++ b/src/GoogleVisionAPI.php @@ -31,6 +31,11 @@ class GoogleVisionAPI { protected $apiKey; /** + * The base url of the Google Cloud Vision API. + */ + const APIEndpoint = 'https://vision.googleapis.com/v1/images:annotate?key='; + + /** * Construct a GoogleVisionAPI object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -52,7 +57,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForLabels($filepath) { if (!$this->apiKey) { @@ -80,7 +85,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -95,7 +100,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForLandmark($filepath) { if (!$this->apiKey) { @@ -123,7 +128,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -138,7 +143,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForLogo($filepath) { if (!$this->apiKey) { @@ -166,7 +171,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -181,7 +186,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForSafeSearch($filepath) { if (!$this->apiKey) { @@ -209,7 +214,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -224,7 +229,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForOCR($filepath) { if (!$this->apiKey) { @@ -252,7 +257,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -267,7 +272,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForFaceDetection($filepath) { if (!$this->apiKey) { @@ -295,7 +300,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], @@ -310,7 +315,7 @@ class GoogleVisionAPI { * * @param string $filepath. * - * @return Array. + * @return Array|bool. */ public function makeRequestForImageAttributes($filepath) { if (!$this->apiKey) { @@ -338,7 +343,7 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $url = APIEndpoint . $this->apiKey; $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'],