/** * Execute a request against the Microsoft API. * * @param Translator $translator * The translator entity to get the settings from. * @param $path * The path that should be appended to the base uri, e.g. Translate or * GetLanguagesForTranslate. * @param $query * (Optional) Array of GET query arguments. * @param $headers * (Optional) Array of additional HTTP headers. * * @return \Psr\Http\Message\ResponseInterface * The HTTP response. */ protected function doRequest(Translator $translator, $path, array $query = array(), array $headers = array()) { $custom_url = $translator->getSetting('url'); $url = ($custom_url ? $custom_url : $this->translatorUrl) . '/' . $path; $test_token_url = FALSE; if ($custom_url) { $test_token_url = $custom_url . '/GetToken'; } // The current API uses 2 new parameters and an access token. $client_id = $translator->getSetting('client_id'); $client_secret = $translator->getSetting('client_secret'); $token = $this->getToken($client_id, $client_secret, $test_token_url); $request_url = Url::fromUri($url)->toString(); $request = new Request('GET', $request_url, $headers); $request = $request->withHeader('Authorization', 'Bearer ' . $token); $response = $this->client->send($request, ['query' => $query]); return $response; }
/** * Local method to do request to Google Translate service. * * @param Translator $translator * The translator entity to get the settings from. * @param string $action * Action to be performed [translate, languages, detect] * @param array $request_query * (Optional) Additional query params to be passed into the request. * @param array $options * (Optional) Additional options that will be passed into drupal_http_request(). * * @return array object * Unserialized JSON response from Google. * * @throws TMGMTException * - Invalid action provided * - Unable to connect to the Google Service * - Error returned by the Google Service */ protected function doRequest(Translator $translator, $action, array $request_query = array(), array $options = array()) { if (!in_array($action, $this->availableActions)) { throw new TMGMTException('Invalid action requested: @action', array('@action' => $action)); } $query_string = ''; // Translate action is requested without this argument. if ($action == 'translate') { $action = ''; } // Get custom URL for testing purposes, if available. $custom_url = $translator->getSetting('url'); $url = ($custom_url ? $custom_url : $this->translatorUrl) . '/' . $action; // Prepare Guzzle Object. $request = new Request('GET', $url); // Build the query. $query_string .= '&key=' . $translator->getSetting('api_key'); if (isset($request_query['q'])) { foreach ($request_query['q'] as $source_text) { $query_string .= '&q=' . urlencode($source_text); } } if (isset($request_query['source'])) { $query_string .= '&source=' . $request_query['source']; } if (isset($request_query['target'])) { $query_string .= '&target=' . $request_query['target']; } // Send the request with the query. try { $response = $this->client->send($request, ['query' => $query_string]); } catch (BadResponseException $e) { $error = json_decode($e->getResponse()->getBody(), TRUE); throw new TMGMTException('Google Translate service returned following error: @error', ['@error' => $error['error']['message']]); } // Process the JSON result into array. return json_decode($response->getBody(), TRUE); }