/** * @param Guzzle $guzzle * @param ClientDescriptionInterface $clientDescription */ public function __construct(Guzzle $guzzle, ClientDescriptionInterface $clientDescription, EventDispatcherInterface $eventDispatcher = null) { $this->guzzle = $guzzle; $this->clientDescription = $clientDescription; $this->eventDispatcher = $eventDispatcher ?: new EventDispatcher(); $this->attachListeners($this->eventDispatcher, $clientDescription->getSubscribedEvents()); }
/** * @param MethodDescriptionInterface $methodDescription * @param array $options * * @return $this|\GuzzleHttp\Psr7\MessageTrait|Request */ public function createRequest(MethodDescriptionInterface $methodDescription, array $options = []) { $headers = []; $body = null; $method = $methodDescription->getMethod(); if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])) { if ($data = $methodDescription->getRequestData($options)) { $format = $methodDescription->getRequestDataFormat(); switch ($format) { case MethodDescriptionInterface::FORMAT_JSON: $serializer = $this->clientDescription->getSerializer(); $body = \GuzzleHttp\Psr7\stream_for($serializer($data, $format, $methodDescription->getRequestDataContext())); $headers['Content-Type'] = 'application/json'; break; case MethodDescriptionInterface::FORMAT_XML: $serializer = $this->clientDescription->getSerializer(); $body = \GuzzleHttp\Psr7\stream_for($serializer($data, $format, $methodDescription->getRequestDataContext())); $headers['Content-Type'] = 'application/xml'; break; case MethodDescriptionInterface::FORMAT_MULTIPART_FORM_DATA: $elements = []; foreach ($data as $name => $value) { if ($value instanceof File) { $value = fopen($value->getRealPath(), 'r'); } $elements[] = ['name' => $name, 'contents' => $value]; } $body = new MultipartStream($elements); $headers['Content-Type'] = 'multipart/form-data; boundary=' . $body->getBoundary(); break; } } } $request = new Request($methodDescription->getMethod(), $this->resolveUri($methodDescription->getResource(), $methodDescription->getQuery($options), $options), $headers, $body); return $request; }