示例#1
1
 /**
  * {@inheritDoc}
  */
 public static function fromCommand(CommandInterface $command, Response $response)
 {
     $errors = json_decode($response->getBody(true), true);
     $type = array_get($errors, 'error.type', null);
     $code = array_get($errors, 'error.code', null);
     $message = array_get($errors, 'error.message', null);
     $class = '\\Apache\\Usergrid\\Api\\Exception\\' . studly_case($type) . 'Exception';
     if (class_exists($class)) {
         $exception = new $class($message, $response->getStatusCode());
     } else {
         $exception = new static($message, $response->getStatusCode());
     }
     $exception->setErrorType($type);
     $exception->setResponse($response);
     $exception->setRequest($command->getRequest());
     return $exception;
 }
 /**
  * {@inheritDoc}
  */
 public static function fromCommand(CommandInterface $command, Response $response)
 {
     $errors = json_decode($response->getBody(true), true);
     $type = $errors['error']['type'];
     $message = isset($errors['error']['message']) ? $errors['error']['message'] : 'Unknown error';
     $code = isset($errors['error']['code']) ? $errors['error']['code'] : null;
     // We can trigger very specific exception based on the type and code
     if ($type === 'card_error') {
         $exception = new CardErrorException($message, $response->getStatusCode());
     } elseif ($type === 'invalid_request_error' && $code === 'rate_limit') {
         $exception = new ApiRateLimitException($message, $response->getStatusCode());
     } else {
         $exception = new static($message, $response->getStatusCode());
     }
     $exception->setRequest($command->getRequest());
     $exception->setResponse($response);
     return $exception;
 }
 private function addMd5(CommandInterface $command)
 {
     $request = $command->getRequest();
     if ($body = $request->getBody()) {
         if (false !== ($md5 = $body->getContentMd5(true, true))) {
             $request->setHeader('Content-MD5', $md5);
         }
     }
 }
示例#4
0
 private function addMd5(CommandInterface $command)
 {
     $request = $command->getRequest();
     if ($body = $request->getBody()) {
         if (false === ($md5 = $body->getContentMd5(true, true))) {
             throw new RuntimeException('Unable to add a MD5 checksum');
         }
         $request->setHeader('Content-MD5', $md5);
     }
 }
示例#5
0
 public function parse(CommandInterface $command)
 {
     $response = $command->getRequest()->getResponse();
     if ($contentType = $command['command.expects']) {
         $response->setHeader('Content-Type', $contentType);
     } else {
         $contentType = (string) $response->getHeader('Content-Type');
     }
     return $this->handleParsing($command, $response, $contentType);
 }
 public function parse(CommandInterface $command)
 {
     $response = $command->getRequest()->getResponse();
     // Account for hard coded content-type values specified in service descriptions
     if ($contentType = $command['command.expects']) {
         $response->setHeader('Content-Type', $contentType);
     } else {
         $contentType = (string) $response->getHeader('Content-Type');
     }
     return $this->handleParsing($command, $response, $contentType);
 }
 /**
  * {@inheritdoc}
  *
  * @param CommandInterface $command
  * @return array|MultiResultResponse|SingleResultResponse|Response|mixed
  */
 public function parse(CommandInterface $command)
 {
     $response = $command->getRequest()->getResponse();
     if ($response === null) {
         return null;
     }
     $responseArray = $this->parseResponseIntoArray($response);
     // If there is no Body, just return the Response
     if (!$response->getBody()) {
         return $response;
     }
     // Handle multi-result responses
     if (array_key_exists('results', $responseArray)) {
         return $this->createMultiResultResponse($response);
     }
     return $this->createSingleResultResponse($response);
 }
 /**
  * Get the Exception that caused the given $command to fail
  *
  * @param CommandInterface $command Failed command
  *
  * @return \Exception|null
  */
 public function getExceptionForFailedCommand(CommandInterface $command)
 {
     return $this->getExceptionForFailedRequest($command->getRequest());
 }
 /**
  * {@inheritdoc}
  */
 public function parse(CommandInterface $command)
 {
     $response = $command->getRequest()->getResponse();
     $contentType = (string) $response->getHeader('Content-Type');
     return $this->handleParsing($command, $response, $contentType);
 }
 /**
  * Execute a command and return the response
  *
  * @param CommandInterface|CommandSet|array $command Command or set to execute
  *
  * @return mixed Returns the result of the executed command's
  *       {@see CommandInterface::getResult} method if a CommandInterface is
  *       passed, or the CommandSet itself if a CommandSet is passed
  * @throws InvalidArgumentException if an invalid command is passed
  * @throws CommandSetException if a set contains commands associated
  *      with other clients
  */
 public function execute($command)
 {
     if ($command instanceof CommandInterface) {
         $command->setClient($this)->prepare();
         $this->dispatch('command.before_send', array('command' => $command));
         $command->getRequest()->send();
         $this->dispatch('command.after_send', array('command' => $command));
         return $command->getResult();
     } else {
         if ($command instanceof CommandSet) {
             foreach ($command as $c) {
                 if ($c->getClient() && $c->getClient() !== $this) {
                     throw new CommandSetException('Attempting to run a mixed-Client CommandSet from a ' . 'Client context.  Run the set using CommandSet::execute() ');
                 }
                 $c->setClient($this);
             }
             return $command->execute();
         } else {
             if (is_array($command)) {
                 return $this->execute(new CommandSet($command));
             }
         }
     }
     throw new InvalidArgumentException('Invalid command sent to ' . __METHOD__);
 }
 /**
  * {@inheritdoc}
  */
 public function parse(CommandInterface $command)
 {
     $content = $command->getRequest()->getResponse()->getBody(true);
     return json_decode(preg_replace('/^availableFeeds=/', '', $content), true);
 }