Http.php 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?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;

/**
 * Retrieve data over an HTTP connection for migration.
 *
13
 * Example:
14 15 16 17 18 19 20 21 22 23 24 25
 *
 * @code
 * source:
 *   plugin: url
 *   data_fetcher_plugin: http
 *   headers:
 *     Accept: application/json
 *     User-Agent: Internet Explorer 6
 *     Authorization-Key: secret
 *     Arbitrary-Header: foobarbaz
 * @endcode
 *
26 27 28 29 30 31 32 33
 * @DataFetcher(
 *   id = "http",
 *   title = @Translation("HTTP")
 * )
 */
class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterface {

  /**
34
   * The HTTP client.
35 36 37 38 39 40 41 42 43 44 45 46
   *
   * @var \GuzzleHttp\Client
   */
  protected $httpClient;

  /**
   * The request headers.
   *
   * @var array
   */
  protected $headers = [];

47 48 49 50 51 52 53
  /**
   * The data retrieval client.
   *
   * @var \Drupal\migrate_plus\AuthenticationPluginInterface
   */
  protected $authenticationPlugin;

54 55 56 57 58 59
  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->httpClient = \Drupal::httpClient();
60

61 62
    // Ensure there is a 'headers' key in the configuration.
    $configuration += ['headers' => []];
63
    $this->setRequestHeaders($configuration['headers']);
64 65
  }

66 67 68 69 70 71 72 73 74 75 76 77 78
  /**
   * Returns the initialized authentication plugin.
   *
   * @return \Drupal\migrate_plus\AuthenticationPluginInterface
   *   The authentication plugin.
   */
  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;
  }

79 80 81 82 83 84 85 86 87 88 89
  /**
   * {@inheritdoc}
   */
  public function setRequestHeaders(array $headers) {
    $this->headers = $headers;
  }

  /**
   * {@inheritdoc}
   */
  public function getRequestHeaders() {
90
    return !empty($this->headers) ? $this->headers : [];
91 92 93 94 95 96 97
  }

  /**
   * {@inheritdoc}
   */
  public function getResponse($url) {
    try {
98 99 100 101 102
      $options = ['headers' => $this->getRequestHeaders()];
      if (!empty($this->configuration['authentication'])) {
        $options = array_merge($options, $this->getAuthenticationPlugin()->getAuthenticationOptions());
      }
      $response = $this->httpClient->get($url, $options);
103 104 105 106 107
      if (empty($response)) {
        throw new MigrateException('No response at ' . $url . '.');
      }
    }
    catch (RequestException $e) {
108
      throw new MigrateException('Error message: ' . $e->getMessage() . ' at ' . $url . '.');
109 110 111 112 113 114 115 116 117 118 119 120 121
    }
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function getResponseContent($url) {
    $response = $this->getResponse($url);
    return $response->getBody();
  }

}