Exemplo n.º 1
0
 public function testUri()
 {
     $client = new App_Rest_Client('http://framework.zend.com/rest/');
     $uri = $client->getUri();
     $this->assertTrue($uri instanceof Zend_Uri_Http);
     $this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
     $client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
     $uri = $client->getUri();
     $this->assertTrue($uri instanceof Zend_Uri_Http);
     $this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
     $client->setUri('http://framework.zend.com/xmlrpc/');
     $uri = $client->getUri();
     $this->assertTrue($uri instanceof Zend_Uri_Http);
     $this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
 }
Exemplo n.º 2
0
 /**
  * Method caller
  * @param  string    $method
  * @param  array     $args
  * @throws Exception
  */
 public function callMethod($method, $args)
 {
     $this->log("Calling rest method: " . $method, Zend_Log::INFO);
     if ($restMethod = $this->getRestMethod($method)) {
         //Start plugins
         $this->setupPlugins($method);
         //Plugins: Call prepareArguments hook
         $rawArguments = $this->prepareArguments(isset($args) ? $args : null);
         //Plugins: Call preparePath hook
         $path = $this->preparePath($restMethod);
         //Creating Rest Client
         $restClient = new App_Rest_Client($path);
         //Plugins: Call setupHttpClient hook
         $this->setupHttpClient($restClient->getHttpClient());
         $query = null;
         if (isset($rawArguments['$query'])) {
             $query = $rawArguments['$query'];
             unset($rawArguments['$query']);
         }
         //Prepare arguments with output processor
         $arguments = $this->processOutputData($restMethod, $rawArguments);
         $this->processRequest($restClient->getUri()->getPath(), $arguments);
         try {
             $start = microtime(TRUE);
             if (!$this->getUseMocks()) {
                 //Call RPC
                 $this->_lastResult = $restClient->{'rest' . $restMethod->method}($restClient->getUri()->getPath(), $arguments, $query);
             } else {
                 if (!isset($restMethod->mock)) {
                     throw new Exception("No mock config for method '{$method}'.");
                 } else {
                     //Create a mock result
                     $this->_lastResult = $this->_createMock($restMethod, $restClient->getUri()->getPath(), $arguments, $rawArguments, $query);
                 }
             }
             $start = microtime(TRUE) - $start;
             $this->_lastResultTimeSpent = $start;
             $url = $restClient->getUri()->getPath();
             if ($start > 0.2) {
                 $this->log("Too much time spent ({$url}): {$start}", Zend_Log::WARN);
             }
         } catch (Exception $e) {
             $this->log($e, Zend_Log::CRIT);
             //Plugins: Call processException hook
             $this->processException($e);
         }
         //Plugins: Call processResponse hook
         $this->processResponse($this->_lastResult);
         //Plugins: Call shutdownHttpClient hook
         $this->shutdownHttpClient($restClient->getHttpClient());
         try {
             //Parse response with input processor
             $result = $this->processInputData($restMethod, $this->_lastResult);
         } catch (Exception $e) {
             $this->log($e, Zend_Log::CRIT);
             //Plugins: Call processException hook
             $this->processException($e);
             throw $e;
         }
         //Plugins: Call processResponseData hook
         $this->processResponseData($result);
         return $result;
     } else {
         $this->log('Method does not exist: ' . $method, Zend_Log::CRIT);
         throw new Exception('Method does not exist: ' . $method);
     }
 }