Http.php in Migrate Plus 8.2
File
src/Plugin/migrate_plus/data_fetcher/Http.php
View source
<?php
namespace Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate_plus\DataFetcherPluginBase;
use GuzzleHttp\Exception\RequestException;
class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterface {
protected $httpClient;
protected $headers = [];
protected $authenticationPlugin;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->httpClient = \Drupal::httpClient();
}
public function getAuthenticationPlugin() {
if (!isset($this->authenticationPlugin)) {
$this->authenticationPlugin = \Drupal::service('plugin.manager.migrate_plus.authentication')
->createInstance($this->configuration['authentication']['plugin'], $this->configuration['authentication']);
}
return $this->authenticationPlugin;
}
public function setRequestHeaders(array $headers) {
$this->headers = $headers;
}
public function getRequestHeaders() {
return !empty($this->headers) ? $this->headers : [];
}
public function getResponse($url) {
try {
$options = [
'headers' => $this
->getRequestHeaders(),
];
if (!empty($this->configuration['authentication'])) {
$options = array_merge($options, $this
->getAuthenticationPlugin()
->getAuthenticationOptions());
}
$response = $this->httpClient
->get($url, $options);
if (empty($response)) {
throw new MigrateException('No response at ' . $url . '.');
}
} catch (RequestException $e) {
throw new MigrateException('Error message: ' . $e
->getMessage() . ' at ' . $url . '.');
}
return $response;
}
public function getResponseContent($url) {
$response = $this
->getResponse($url);
return $response
->getBody();
}
}
Classes
Name |
Description |
Http |
Retrieve data over an HTTP connection for migration. |