diff --git a/client/src/Plugin/QueueWorker/CollectClientQueueWorker.php b/client/src/Plugin/QueueWorker/CollectClientQueueWorker.php index d62c4b3..37d84be 100644 --- a/client/src/Plugin/QueueWorker/CollectClientQueueWorker.php +++ b/client/src/Plugin/QueueWorker/CollectClientQueueWorker.php @@ -230,7 +230,7 @@ class CollectClientQueueWorker extends QueueWorkerBase implements ContainerFacto // Special support for CollectJSON field definition. try { foreach ($this->getTransferObjects($item) as $item_values) { - $response = $this->httpClient->request('POST', $url, array_merge($item_values, $options)); + $response = $this->httpClient->post($url, array_merge($item_values, $options)); if ($response->getStatusCode() != 201) { throw new \Exception('Expected status code 201 Created, got ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . ' when submitting item to ' . $url . '.', 0); } @@ -281,12 +281,12 @@ class CollectClientQueueWorker extends QueueWorkerBase implements ContainerFacto */ protected function parseResponse(ResponseInterface $response, $property) { try { - if (!$response->getBody()) { + if (!$body = $response->getBody()) { return FALSE; } // Decode received data. - $json = json_decode($response->getBody()->getContents(), TRUE); + $json = json_decode($body->getContents(), TRUE); if ($property == 'uuid') { return isset($json['data'][$property]) ? $json['data'][$property] : FALSE; diff --git a/client/tests/src/Unit/Plugin/QueueWorker/QueueWorkerTest.php b/client/tests/src/Unit/Plugin/QueueWorker/QueueWorkerTest.php index 777b591..43008f5 100644 --- a/client/tests/src/Unit/Plugin/QueueWorker/QueueWorkerTest.php +++ b/client/tests/src/Unit/Plugin/QueueWorker/QueueWorkerTest.php @@ -71,7 +71,7 @@ class QueueWorkerTest extends UnitTestCase { protected function setUp() { $this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface'); $this->logger = $this->getMock('Drupal\Core\Logger\LoggerChannelInterface'); - $this->httpClient = $this->getMock('GuzzleHttp\ClientInterface'); + $this->httpClient = $this->getMock('GuzzleHttp\Client', ['post']); $this->serializer = $this->getMock('\Symfony\Component\Serializer\SerializerInterface'); $this->config = $this->getMockBuilder('Drupal\Core\Config\Config') ->disableOriginalConstructor() @@ -217,8 +217,8 @@ EOF; $location = 'http://server.collect.dev/collect/api/v1/submissions/f67c88c9-1608-4f47-8291-4e35411af61f'; $response = $this->getMock('Psr\Http\Message\ResponseInterface'); $this->httpClient->expects($this->once()) - ->method('request') - ->with('POST', $url, $options) + ->method('post') + ->with($url, $options) ->will($this->returnValue($response)); $response->expects($this->once()) diff --git a/collect.services.yml b/collect.services.yml index 81a2e93..c5c9ef4 100644 --- a/collect.services.yml +++ b/collect.services.yml @@ -25,4 +25,4 @@ services: arguments: ['@request_stack', '@serializer', '@entity.manager', '@plugin.manager.collect.model'] collect.fetch_web_resource: class: Drupal\collect\FetchWebResource - arguments: ['@http_client_factory', '@entity.manager', '@plugin.manager.collect.model'] + arguments: ['@http_client', '@entity.manager', '@plugin.manager.collect.model'] diff --git a/src/FetchWebResource.php b/src/FetchWebResource.php index 39d86e8..7c82f24 100644 --- a/src/FetchWebResource.php +++ b/src/FetchWebResource.php @@ -11,7 +11,7 @@ use Drupal\collect\Entity\Container; use Drupal\collect\Model\ModelManagerInterface; use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Http\ClientFactory; +use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Request; @@ -31,11 +31,11 @@ class FetchWebResource { const MIMETYPE = 'application/json'; /** - * The HTTP client to fetch the web resource with. + * The http client. * - * @var \Drupal\Core\Http\ClientFactory $httpClientFactory + * @var \GuzzleHttp\ClientInterface */ - protected $httpClientFactory; + protected $httpClient; /** * The entity manager service. @@ -54,15 +54,15 @@ class FetchWebResource { /** * Set up a new FetchWebResource instance. * - * @param \Drupal\Core\Http\ClientFactory $http_client_factory + * @param \GuzzleHttp\ClientInterface $http_client * A Guzzle client object. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. * @param \Drupal\collect\Model\ModelManagerInterface $model_manager * The injected model manager. */ - public function __construct(ClientFactory $http_client_factory, EntityManagerInterface $entity_manager, ModelManagerInterface $model_manager) { - $this->httpClientFactory = $http_client_factory; + public function __construct(ClientInterface $http_client, EntityManagerInterface $entity_manager, ModelManagerInterface $model_manager) { + $this->httpClient = $http_client; $this->entityManager = $entity_manager; $this->modelManager = $model_manager; } @@ -85,16 +85,15 @@ class FetchWebResource { $schema_uri = static::SCHEMA_URI; } // Create a new GET request. - $request = (new Request('GET', $url)) - ->withHeader('Accept', $accept_header) - ->withHeader('Accept-Charset', 'UTF-8, *'); + $request = new Request('GET', $url, [ + 'Accept' => $accept_header, + 'Accept-Charset' => 'UTF-8, *' + ]); // Get request headers. $request_headers = $request->getHeaders(); - // Get a http client. - $http_client = $this->httpClientFactory->fromOptions(); try { - // Send an HTTP request and get a response. - $response = $http_client->send($request); + // Send a request to the server and get a response. + $response = $this->httpClient->send($request); } catch (RequestException $re) { if ($re->hasResponse()) {