Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate_plus / data_fetcher / Http.php
index 7402126a9b091424dcbbddd4f0db0819177c19ba..db4c423a691fd9114f4a84d76e6d6d892bd86fcd 100755 (executable)
@@ -10,6 +10,19 @@ use GuzzleHttp\Exception\RequestException;
 /**
  * Retrieve data over an HTTP connection for migration.
  *
+ * Example:
+ *
+ * @code
+ * source:
+ *   plugin: url
+ *   data_fetcher_plugin: http
+ *   headers:
+ *     Accept: application/json
+ *     User-Agent: Internet Explorer 6
+ *     Authorization-Key: secret
+ *     Arbitrary-Header: foobarbaz
+ * @endcode
+ *
  * @DataFetcher(
  *   id = "http",
  *   title = @Translation("HTTP")
@@ -18,7 +31,7 @@ use GuzzleHttp\Exception\RequestException;
 class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterface {
 
   /**
-   * The HTTP Client
+   * The HTTP client.
    *
    * @var \GuzzleHttp\Client
    */
@@ -44,6 +57,10 @@ class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterf
   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     $this->httpClient = \Drupal::httpClient();
+
+    // Ensure there is a 'headers' key in the configuration.
+    $configuration += ['headers' => []];
+    $this->setRequestHeaders($configuration['headers']);
   }
 
   /**
@@ -88,7 +105,7 @@ class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterf
       }
     }
     catch (RequestException $e) {
-      throw new MigrateException('Error message: ' . $e->getMessage() . ' at ' . $url .'.');
+      throw new MigrateException('Error message: ' . $e->getMessage() . ' at ' . $url . '.');
     }
     return $response;
   }