db4c423a691fd9114f4a84d76e6d6d892bd86fcd
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate_plus / data_fetcher / Http.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate_plus\DataFetcherPluginBase;
8 use GuzzleHttp\Exception\RequestException;
9
10 /**
11  * Retrieve data over an HTTP connection for migration.
12  *
13  * Example:
14  *
15  * @code
16  * source:
17  *   plugin: url
18  *   data_fetcher_plugin: http
19  *   headers:
20  *     Accept: application/json
21  *     User-Agent: Internet Explorer 6
22  *     Authorization-Key: secret
23  *     Arbitrary-Header: foobarbaz
24  * @endcode
25  *
26  * @DataFetcher(
27  *   id = "http",
28  *   title = @Translation("HTTP")
29  * )
30  */
31 class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterface {
32
33   /**
34    * The HTTP client.
35    *
36    * @var \GuzzleHttp\Client
37    */
38   protected $httpClient;
39
40   /**
41    * The request headers.
42    *
43    * @var array
44    */
45   protected $headers = [];
46
47   /**
48    * The data retrieval client.
49    *
50    * @var \Drupal\migrate_plus\AuthenticationPluginInterface
51    */
52   protected $authenticationPlugin;
53
54   /**
55    * {@inheritdoc}
56    */
57   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
58     parent::__construct($configuration, $plugin_id, $plugin_definition);
59     $this->httpClient = \Drupal::httpClient();
60
61     // Ensure there is a 'headers' key in the configuration.
62     $configuration += ['headers' => []];
63     $this->setRequestHeaders($configuration['headers']);
64   }
65
66   /**
67    * Returns the initialized authentication plugin.
68    *
69    * @return \Drupal\migrate_plus\AuthenticationPluginInterface
70    *   The authentication plugin.
71    */
72   public function getAuthenticationPlugin() {
73     if (!isset($this->authenticationPlugin)) {
74       $this->authenticationPlugin = \Drupal::service('plugin.manager.migrate_plus.authentication')->createInstance($this->configuration['authentication']['plugin'], $this->configuration['authentication']);
75     }
76     return $this->authenticationPlugin;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function setRequestHeaders(array $headers) {
83     $this->headers = $headers;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function getRequestHeaders() {
90     return !empty($this->headers) ? $this->headers : [];
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getResponse($url) {
97     try {
98       $options = ['headers' => $this->getRequestHeaders()];
99       if (!empty($this->configuration['authentication'])) {
100         $options = array_merge($options, $this->getAuthenticationPlugin()->getAuthenticationOptions());
101       }
102       $response = $this->httpClient->get($url, $options);
103       if (empty($response)) {
104         throw new MigrateException('No response at ' . $url . '.');
105       }
106     }
107     catch (RequestException $e) {
108       throw new MigrateException('Error message: ' . $e->getMessage() . ' at ' . $url . '.');
109     }
110     return $response;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function getResponseContent($url) {
117     $response = $this->getResponse($url);
118     return $response->getBody();
119   }
120
121 }