Exemplo n.º 1
0
 public function __invoke(CommandInterface $command, ResponseInterface $response)
 {
     $output = $this->api->getOperation($command->getName())->getOutput();
     $result = [];
     if ($payload = $output['payload']) {
         $this->extractPayload($payload, $output, $response, $result);
     }
     foreach ($output->getMembers() as $name => $member) {
         switch ($member['location']) {
             case 'header':
                 $this->extractHeader($name, $member, $response, $result);
                 break;
             case 'headers':
                 $this->extractHeaders($name, $member, $response, $result);
                 break;
             case 'statusCode':
                 $this->extractStatus($name, $response, $result);
                 break;
         }
     }
     if (!$payload) {
         // if no payload was found, then parse the contents of the body
         $this->payload($response, $output, $result);
     }
     return new Result($result);
 }
 public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context)
 {
     foreach ($this->buffered as $param) {
         $this->visitWithValue($command[$param->getName()], $param, $operation);
     }
     $this->buffered = [];
     $additional = $operation->getAdditionalParameters();
     if ($additional && $additional->getLocation() == $this->locationName) {
         foreach ($command->toArray() as $key => $value) {
             if (!$operation->hasParam($key)) {
                 $additional->setName($key);
                 $this->visitWithValue($value, $additional, $operation);
             }
         }
         $additional->setName(null);
     }
     // If data was found that needs to be serialized, then do so
     $xml = null;
     if ($this->writer) {
         $xml = $this->finishDocument($this->writer);
     } elseif ($operation->getData('xmlAllowEmpty')) {
         // Check if XML should always be sent for the command
         $writer = $this->createRootElement($operation);
         $xml = $this->finishDocument($writer);
     }
     if ($xml) {
         $request->setBody(Stream::factory($xml));
         // Don't overwrite the Content-Type if one is set
         if ($this->contentType && !$request->hasHeader('Content-Type')) {
             $request->setHeader('Content-Type', $this->contentType);
         }
     }
     $this->writer = null;
 }
Exemplo n.º 3
0
 public function __invoke(CommandInterface $command, ResponseInterface $response)
 {
     $output = $this->api->getOperation($command->getName())->getOutput();
     $xml = $response->xml();
     if ($this->honorResultWrapper && $output['resultWrapper']) {
         $xml = $xml->{$output['resultWrapper']};
     }
     return new Result($this->xmlParser->parse($output, $xml));
 }
 public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context)
 {
     $additional = $operation->getAdditionalParameters();
     if ($additional && $additional->getLocation() == $this->locationName) {
         foreach ($command->toArray() as $key => $value) {
             if (!$operation->hasParam($key)) {
                 $request->setHeader($key, $additional->filter($value));
             }
         }
     }
 }
 private function createPresignedUrl(AwsClientInterface $client, CommandInterface $cmd)
 {
     $newCmd = $client->getCommand('CopySnapshot', $cmd->toArray());
     $newCmd->getEmitter()->detach($this);
     // Serialize a request for the CopySnapshot operation.
     $request = $client->initTransaction($newCmd)->request;
     // Create the new endpoint for the target endpoint.
     $endpoint = EndpointProvider::resolve($this->endpointProvider, ['region' => $cmd['SourceRegion'], 'service' => 'ec2'])['endpoint'];
     // Set the request to hit the target endpoint.
     $request->setHost(Url::fromString($endpoint)->getHost());
     // Create a presigned URL for our generated request.
     $signer = new SignatureV4('ec2', $cmd['SourceRegion']);
     return $signer->createPresignedUrl(SignatureV4::convertPostToGet($request), $client->getCredentials(), '+1 hour');
 }
 public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context)
 {
     $additional = $operation->getAdditionalParameters();
     if ($additional && $additional->getLocation() == $this->locationName) {
         $body = $request->getBody();
         if (!$body instanceof PostBodyInterface) {
             throw new \RuntimeException('Must be a POST body interface');
         }
         foreach ($command->toArray() as $key => $value) {
             if (!$operation->hasParam($key)) {
                 $body->setField($key, $this->prepareValue($value, $additional));
             }
         }
     }
 }
 private function prepareEvent(ProcessEvent $event, $commandName, array $errors = [])
 {
     $this->command->expects($this->once())->method('getName')->will($this->returnValue($commandName));
     $description = $this->getMock('GuzzleHttp\\Command\\Guzzle\\DescriptionInterface');
     $operation = new Operation(['errorResponses' => $errors], $description);
     $description->expects($this->once())->method('getOperation')->with($commandName)->will($this->returnValue($operation));
     $this->serviceClient->expects($this->once())->method('getDescription')->will($this->returnValue($description));
 }
 public function after(CommandInterface $command, RequestInterface $request, Operation $operation, array $context)
 {
     $data = $this->jsonData;
     $this->jsonData = null;
     // Add additional parameters to the JSON document
     $additional = $operation->getAdditionalParameters();
     if ($additional && $additional->getLocation() == $this->locationName) {
         foreach ($command->toArray() as $key => $value) {
             if (!$operation->hasParam($key)) {
                 $data[$key] = $this->prepareValue($value, $additional);
             }
         }
     }
     // Don't overwrite the Content-Type if one is set
     if ($this->jsonContentType && !$request->hasHeader('Content-Type')) {
         $request->setHeader('Content-Type', $this->jsonContentType);
     }
     $request->setBody(Stream::factory(json_encode($data)));
 }
Exemplo n.º 9
0
 private function hashCommand(ServiceClientInterface $client, CommandInterface $command, EventInterface $event)
 {
     return get_class($client) . '::' . $command->getName() . ' (' . spl_object_hash($event) . ')';
 }
 public function __invoke(CommandInterface $command, ResponseInterface $response)
 {
     $operation = $this->api->getOperation($command->getName());
     return new Result($this->parser->parse($operation->getOutput(), $response->json()));
 }
Exemplo n.º 11
0
 /**
  * @param CommandInterface $command
  *
  * @return RequestInterface
  */
 public function commandToRequestTransformer(CommandInterface $command)
 {
     $name = $command->getName();
     if (!isset($this->api[$name])) {
         throw new CommandException('Command not found', $command);
     }
     $action = $this->api[$name];
     $prefix = '';
     if (isset($action['public']) && $command->hasParam('public')) {
         $prefix = '/public';
     }
     $prefixes = ['system' => '/systems/{system}', 'issuer' => '/issuers/{issuer}', 'program' => '/programs/{program}'];
     if (isset($action['admin_contexts'])) {
         $prefix .= implode('', array_intersect_key($prefixes, array_flip($action['admin_contexts']), array_merge($command->toArray(), $this->adminContext)));
     }
     $path = GuzzleHttp\uri_template($prefix . $action['path'], array_merge($command->toArray(), $this->adminContext));
     $headers = [];
     $body = null;
     if ($command->hasParam('body')) {
         $headers = ['Content-Type' => 'application/json'];
         $body = GuzzleHttp\json_encode($command['body']);
     }
     if ($command->hasParam('query')) {
         $path .= '?' . Psr7\build_query($command['query']);
     }
     return new Psr7\Request($action['method'], $path, $headers, $body);
 }
Exemplo n.º 12
0
 public function initTransaction(CommandInterface $command)
 {
     $trans = new CommandTransaction($this, $command);
     // Throwing in the init event WILL NOT emit an error event.
     $command->getEmitter()->emit('init', new InitEvent($trans));
     $trans->request = $this->serializeRequest($trans);
     if ($future = $command->getFuture()) {
         $trans->request->getConfig()->set('future', $future);
     }
     $trans->state = 'prepared';
     $prep = new PreparedEvent($trans);
     try {
         $command->getEmitter()->emit('prepared', $prep);
     } catch (\Exception $e) {
         $trans->exception = $e;
         $trans->exception = $this->createCommandException($trans);
     }
     // If the command failed in the prepare event or was intercepted, then
     // emit the process event now and skip hooking up the request.
     if ($trans->exception || $prep->isPropagationStopped()) {
         $this->emitProcess($trans);
         return $trans;
     }
     $trans->state = 'executing';
     // When a request completes, process the request at the command
     // layer.
     $trans->request->getEmitter()->on('end', function (EndEvent $e) use($trans) {
         $trans->response = $e->getResponse();
         if ($trans->exception = $e->getException()) {
             $trans->exception = $this->createCommandException($trans);
         }
         $this->emitProcess($trans);
     }, RequestEvents::LATE);
     return $trans;
 }