Commit 90ead8f9 authored by catch's avatar catch

Issue #1912594 by krishworks, Berdir: Convert drupal_http_request() usage in...

Issue #1912594 by krishworks, Berdir: Convert drupal_http_request() usage in system.module to Guzzle.
parent 8320fdc2
......@@ -13,6 +13,8 @@
use Drupal\system\Plugin\block\block\SystemMenuBlock;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Exception\RequestException;
/**
* Maximum age of temporary files in seconds.
......@@ -3667,7 +3669,7 @@ function theme_system_compact_link() {
}
/**
* Attempts to get a file using drupal_http_request and to store it locally.
* Attempts to get a file using Guzzle HTTP client and to store it locally.
*
* @param $url
* The URL of the file to grab.
......@@ -3708,12 +3710,22 @@ function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $repl
$path = $destination;
}
}
$result = drupal_http_request($url);
if ($result->code != 200) {
drupal_set_message(t('HTTP error @errorcode occurred when trying to fetch @remote.', array('@errorcode' => $result->code, '@remote' => $url)), 'error');
try {
$data = Drupal::httpClient()
->get($url)
->send()
->getBody(TRUE);
$local = $managed ? file_save_data($data, $path, $replace) : file_unmanaged_save_data($data, $path, $replace);
}
catch (BadResponseException $exception) {
$response = $exception->getResponse();
drupal_set_message(t('Failed to fetch file due to HTTP error "%error"', array('%error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase())), 'error');
return FALSE;
}
catch (RequestException $exception) {
drupal_set_message(t('Failed to fetch file due to error "%error"', array('%error' => $exception->getMessage())), 'error');
return FALSE;
}
$local = $managed ? file_save_data($result->data, $path, $replace) : file_unmanaged_save_data($result->data, $path, $replace);
if (!$local) {
drupal_set_message(t('@remote could not be saved to @path.', array('@remote' => $url, '@path' => $path)), 'error');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment