diff --git a/google_vision.services.yml b/google_vision.services.yml index f165465..3e2457c 100755 --- a/google_vision.services.yml +++ b/google_vision.services.yml @@ -1,7 +1,7 @@ services: google_vision.api: class: Drupal\google_vision\GoogleVisionAPI - arguments: ['@config.factory'] + arguments: ['@config.factory', '@http_client'] google_vision.connector: class: Drupal\google_vision\Connector arguments: ['@google_vision.api'] diff --git a/src/Connector.php b/src/Connector.php index 28aaae2..69c6170 100755 --- a/src/Connector.php +++ b/src/Connector.php @@ -1,9 +1,5 @@ request = $image_Request; + $this->googleVisionAPI = $google_VisionAPI; } /** * Function to retrieve labels for given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function labelDetection($filepath) { - $req = $this->request->makeRequestForLabels($filepath); + $req = $this->googleVisionAPI->makeRequestForLabels($filepath); return $req; } /** * Function to detect landmarks within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function landmarkDetection($filepath) { - $req = $this->request->makeRequestForLandmark($filepath); + $req = $this->googleVisionAPI->makeRequestForLandmark($filepath); return $req; } /** * Function to detect logos of famous brands within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function logoDetection($filepath) { - $req = $this->request->makeRequestForLogo($filepath); + $req = $this->googleVisionAPI->makeRequestForLogo($filepath); return $req; } /** * Function to detect explicit content within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function safeSearchDetection($filepath) { - $req = $this->request->makeRequestForSafeSearch($filepath); + $req = $this->googleVisionAPI->makeRequestForSafeSearch($filepath); return $req; } /** * Function to retrieve texts within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function opticalCharacterRecognition($filepath) { - $req = $this->request->makeRequestForOCR($filepath); + $req = $this->googleVisionAPI->makeRequestForOCR($filepath); return $req; } /** * Function to detect faces from a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function faceDetection($filepath) { - $req = $this->request->makeRequestForFaceDetection($filepath); + $req = $this->googleVisionAPI->makeRequestForFaceDetection($filepath); return $req; } /** * Function to retrieve image attributes from a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return array $req. */ public function imageAttributes($filepath) { - $req = $this->request->makeRequestForImageAttributes($filepath); + $req = $this->googleVisionAPI->makeRequestForImageAttributes($filepath); return $req; } diff --git a/src/GoogleVisionAPI.php b/src/GoogleVisionAPI.php index ac6885d..5603862 100755 --- a/src/GoogleVisionAPI.php +++ b/src/GoogleVisionAPI.php @@ -1,9 +1,5 @@ configFactory = $config_factory; + $this->httpClient = $http_client; + $this->apiKey = $this->configFactory->get('google_vision.settings')->get('api_key'); } /** * Function to retrieve labels for given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForLabels($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -65,9 +80,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -79,14 +93,12 @@ class GoogleVisionAPI { /** * Function to detect landmarks within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForLandmark($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -111,9 +123,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -125,14 +136,12 @@ class GoogleVisionAPI { /** * Function to detect logos of famous brands within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForLogo($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -157,9 +166,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -171,14 +179,12 @@ class GoogleVisionAPI { /** * Function to detect explicit content within a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForSafeSearch($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -203,9 +209,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -217,14 +222,12 @@ class GoogleVisionAPI { /** * Function to retrieve texts for given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForOCR($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -249,9 +252,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -263,14 +265,12 @@ class GoogleVisionAPI { /** * Function to fetch faces from a given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForFaceDetection($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -295,9 +295,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]); @@ -309,14 +308,12 @@ class GoogleVisionAPI { /** * Function to retrieve image attributes for given image. * - * @param file URI $filepath. + * @param string $filepath. * * @return Array. */ public function makeRequestForImageAttributes($filepath) { - $config = $this->configFactory->get('google_vision.settings'); - $api_key = $config->get('api_key'); - if (!$api_key) { + if (!$this->apiKey) { return FALSE; } @@ -341,9 +338,8 @@ class GoogleVisionAPI { ], ]; - $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $api_key; - $client = \Drupal::httpClient(); - $response = $client->post($url, [ + $url = 'https://vision.googleapis.com/v1/images:annotate?key=' . $this->apiKey; + $response = $this->httpClient->post($url, [ RequestOptions::JSON => $data, RequestOptions::HEADERS => ['Content-Type' => 'application/json'], ]);