Exemplo n.º 1
0
 /**
  * Execute a request
  *
  * @param   var t either a string or a lang.Type - response type, defaults to webservices.rest.RestResponse
  * @param   webservices.rest.RestRequest request
  * @return  webservices.rest.RestResponse
  * @throws  lang.IllegalStateException if no connection is set
  */
 public function execute($t, $request = NULL)
 {
     if (1 === func_num_args()) {
         // Overloaded version with single argument
         $request = $t;
         $type = NULL;
     } else {
         if (is_string($t)) {
             // Overloaded version with string type
             $type = Type::forName($t);
         } else {
             if ($t instanceof Type) {
                 // Overloaded version with Type instance
                 $type = $t;
             } else {
                 throw new IllegalArgumentException('Given type is neither a Type nor a string, ' . xp::typeOf($request) . ' given');
             }
         }
     }
     if (!$request instanceof RestRequest) {
         throw new IllegalArgumentException('Given request is not a RestRequest, ' . xp::typeOf($request) . ' given');
     }
     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('/')));
     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 (IOException $e) {
         throw new RestException('Cannot send request', $e);
     }
     if ($type instanceof XPClass && $type->isSubclassOf('webservices.rest.RestResponse')) {
         $rr = $type->newInstance($response, $this->deserializerFor(this($response->header('Content-Type'), 0)));
     } else {
         $rr = new RestResponse($response, $this->deserializerFor(this($response->header('Content-Type'), 0)), $type);
     }
     $this->cat && $this->cat->debug('<<<', $response->toString(), $rr->contentCopy());
     return $rr;
 }
Exemplo n.º 2
0
 /**
  * Execute a request
  *
  * <code>
  *   $client->execute(new RestRequest('/', HttpConstants::GET));
  * </code>
  *
  * @param   var t either a string or a lang.Type - response type, defaults to webservices.rest.RestResponse
  * @param   webservices.rest.RestRequest request
  * @return  webservices.rest.RestResponse
  * @throws  lang.IllegalStateException if no connection is set
  */
 public function execute($t, $request = NULL)
 {
     if (1 === func_num_args()) {
         // Overloaded version with single argument
         $request = $t;
         $type = NULL;
     } else {
         if (is_string($t)) {
             // Overloaded version with string type
             $type = Type::forName($t);
         } else {
             if ($t instanceof Type) {
                 // Overloaded version with Type instance
                 $type = $t;
             } else {
                 throw new IllegalArgumentException('Given type is neither a Type nor a string, ' . xp::typeOf($request) . ' given');
             }
         }
     }
     if (!$request instanceof RestRequest) {
         throw new IllegalArgumentException('Given request is not a RestRequest, ' . xp::typeOf($request) . ' given');
     }
     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->serializerFor($request->getContentType())->serialize($this->marshalling->marshal($request->getPayload()))));
     } 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 (IOException $e) {
         throw new RestException('Cannot send request', $e);
     }
     $reader = new ResponseReader($this->deserializerFor(this($response->header('Content-Type'), 0), FALSE), $this->marshalling);
     if (NULL === $type) {
         $rr = new RestResponse($response, $reader);
     } else {
         if ($type instanceof XPClass && $type->isSubclassOf('webservices.rest.RestResponse')) {
             $rr = $type->newInstance($response, $reader);
         } else {
             $rr = new RestResponse($response, $reader, $type);
             // Deprecated!
         }
     }
     $this->cat && $this->cat->debug('<<<', $response->toString(), $rr->contentCopy());
     return $rr;
 }