Example #1
0
 /**
  * Prepare a request to be sent from the Client by adding client specific behaviors and properties to the request.
  *
  * @param RequestInterface $request Request to prepare for the client
  * @param array            $options Options to apply to the request
  *
  * @return RequestInterface
  */
 protected function prepareRequest(RequestInterface $request, array $options = array())
 {
     $request->setClient($this)->setEventDispatcher(clone $this->getEventDispatcher());
     if ($curl = $this->config[self::CURL_OPTIONS]) {
         $request->getCurlOptions()->overwriteWith(CurlHandle::parseCurlConfig($curl));
     }
     if ($params = $this->config[self::REQUEST_PARAMS]) {
         Version::warn('request.params is deprecated. Use request.options to add default request options.');
         $request->getParams()->overwriteWith($params);
     }
     if ($this->userAgent && !$request->hasHeader('User-Agent')) {
         $request->setHeader('User-Agent', $this->userAgent);
     }
     if ($defaults = $this->config[self::REQUEST_OPTIONS]) {
         $this->requestFactory->applyOptions($request, $defaults, RequestFactoryInterface::OPTIONS_AS_DEFAULTS);
     }
     if ($options) {
         $this->requestFactory->applyOptions($request, $options);
     }
     $this->dispatch('client.create_request', array('client' => $this, 'request' => $request));
     return $request;
 }
Example #2
0
 public function prepare()
 {
     if (!$this->isPrepared()) {
         if (!$this->client) {
             throw new CommandException('A client must be associated with the command before it can be prepared.');
         }
         // If no response processing value was specified, then attempt to use the highest level of processing
         if (!isset($this[self::RESPONSE_PROCESSING])) {
             $this[self::RESPONSE_PROCESSING] = self::TYPE_MODEL;
         }
         // Notify subscribers of the client that the command is being prepared
         $this->client->dispatch('command.before_prepare', array('command' => $this));
         // Fail on missing required arguments, and change parameters via filters
         $this->validate();
         // Delegate to the subclass that implements the build method
         $this->build();
         // Add custom request headers set on the command
         if ($headers = $this[self::HEADERS_OPTION]) {
             foreach ($headers as $key => $value) {
                 $this->request->setHeader($key, $value);
             }
         }
         // Add any curl options to the request
         if ($options = $this[Client::CURL_OPTIONS]) {
             $this->request->getCurlOptions()->overwriteWith(CurlHandle::parseCurlConfig($options));
         }
         // Set a custom response body
         if ($responseBody = $this[self::RESPONSE_BODY]) {
             $this->request->setResponseBody($responseBody);
         }
         $this->client->dispatch('command.after_prepare', array('command' => $this));
     }
     return $this->request;
 }