示例#1
0
 /**
  * Execute a request
  *
  * @param  webservices.rest.RestRequest $request
  * @return webservices.rest.RestResponse
  * @throws lang.IllegalStateException if no connection is set
  */
 public function execute(RestRequest $request)
 {
     $url = $request->targetUrl($this->base);
     $host = $url->getHost();
     if (!isset($this->connections[$host])) {
         $target = clone $url;
         // Clone & remove query from URL as it will lead to duplication!
         $this->connections[$host] = $this->connectionTo->__invoke($target->setQuery(null));
         $this->connections[$host]->setConnectTimeout($this->timeouts['connect']);
         $this->connections[$host]->setTimeout($this->timeouts['read']);
     }
     $send = $this->connections[$host]->create(new HttpRequest());
     $send->addHeaders($this->headers);
     $send->addHeaders($request->headerList());
     $send->setMethod($request->getMethod());
     // Compose body
     // * Serialize payloads using the serializer for the given mimetype
     // * Use bodies as-is, e.g. file uploads
     // * If no body and no payload is set, use parameters
     if ($request->hasPayload()) {
         $query = $url->getQuery();
         $send->setTarget($url->getPath() . ($query ? '?' . $query : ''));
         $send->setParameters(new RequestData($this->marshalling->marshal($request->getPayload()), $this->serializerFor($request->getContentType())));
     } else {
         if ($request->hasBody()) {
             $query = $url->getQuery();
             $send->setTarget($url->getPath() . ($query ? '?' . $query : ''));
             $send->setParameters($request->getBody());
         } else {
             $send->setTarget($url->getPath());
             $send->setParameters($url->getParams());
         }
     }
     try {
         $this->cat && $this->cat->debug('>>>', $send->getRequestString());
         $response = $this->connections[$host]->send($send);
     } catch (\io\IOException $e) {
         throw new RestException('Cannot send request', $e);
     }
     $reader = new ResponseReader($this->deserializerFor($response->header('Content-Type')[0]), $this->marshalling);
     $result = new RestResponse($response, $reader);
     $this->cat && $this->cat->debug('<<<', $response->toString(), $result->contentCopy());
     return $result;
 }
 public function setMethod()
 {
     $fixture = new RestRequest();
     $fixture->setMethod(HttpConstants::GET);
     $this->assertEquals(HttpConstants::GET, $fixture->getMethod());
 }
示例#3
0
 /**
  * Execute a request
  *
  * @param  webservices.rest.RestRequest $request
  * @return webservices.rest.RestResponse
  * @throws lang.IllegalStateException if no connection is set
  */
 public function execute(RestRequest $request)
 {
     if (null === $this->connection) {
         throw new IllegalStateException('No connection set');
     }
     $send = $this->connection->create(new HttpRequest());
     $send->addHeaders($request->headerList());
     $send->setMethod($request->getMethod());
     $send->setTarget($request->getTarget($this->connection->getUrl()->getPath('/')));
     // Compose body
     // * Serialize payloads using the serializer for the given mimetype
     // * Use bodies as-is, e.g. file uploads
     // * If no body and no payload is set, use parameters
     if ($request->hasPayload()) {
         $send->setParameters(new RequestData($this->marshalling->marshal($request->getPayload()), $this->serializerFor($request->getContentType())));
     } else {
         if ($request->hasBody()) {
             $send->setParameters($request->getBody());
         } else {
             $send->setParameters($request->getParameters());
         }
     }
     try {
         $this->cat && $this->cat->debug('>>>', $send->getRequestString());
         $response = $this->connection->send($send);
     } catch (\io\IOException $e) {
         throw new RestException('Cannot send request', $e);
     }
     $reader = new ResponseReader($this->deserializerFor($response->header('Content-Type')[0]), $this->marshalling);
     $result = new RestResponse($response, $reader);
     $this->cat && $this->cat->debug('<<<', $response->toString(), $result->contentCopy());
     return $result;
 }