/**
  * {@inheritdoc}
  */
 public function configureUri(Uri $baseUri, $name, $id = null)
 {
     $basePath = $baseUri->getPath();
     $resourcePath = rtrim($basePath, '/') . '/' . $name;
     if ($id) {
         $resourcePath .= '/' . $id;
     }
     $baseUri->setPath($resourcePath);
     return $baseUri;
 }
Exemplo n.º 2
0
 /**
  * @param                    $method
  * @param                    $uri
  * @param array              $options
  * @param null|bool|\Closure $successCallback
  * @param null|bool|\Closure $failCallback
  *
  * @return bool|null|\StdClass
  * @throws Exception
  */
 public function request($method, $uri, $options = [], $successCallback = null, $failCallback = null)
 {
     if (!$successCallback && !$failCallback) {
         $json = null;
         // If there's no callbacks defined we assume this is a RESTful request
         Logger::getInstance()->debug($method . ' ' . $uri . ' ' . json_encode($options));
         $options = array_merge($this->defaultOptions, $options);
         $guzzle = new GuzzleHttp\Client(['base_uri' => self::BASE_URL_REST]);
         $res = $guzzle->request($method, $uri, $options);
         if ($res->getHeader('content-type')[0] == 'application/json') {
             $contents = $res->getBody()->getContents();
             Logger::getInstance()->debug($contents);
             $json = json_decode($contents);
         } else {
             throw new Exception("Server did not send JSON. Response was \"{$res->getBody()->getContents()}\"");
         }
         return $json;
     } else {
         // Use the STREAM API end point
         $Uri = new Uri(self::BASE_URL_STREAM);
         $Uri->setUserInfo($this->defaultOptions['auth'][0] . ':' . $this->defaultOptions['auth'][1]);
         $Uri->setPath($uri);
         if (isset($options['query'])) {
             $Uri->setQuery($options['query']);
         }
         $WSClient = new WebSocket\Client($Uri);
         Logger::getInstance()->debug($Uri);
         if (!$successCallback instanceof \Closure) {
             $successCallback = function ($response) {
                 $info = json_decode($response);
                 if (property_exists($info, 'output')) {
                     Logger::getInstance()->info(json_decode($response)->output);
                 } elseif ($info->type == 'error') {
                     // output error info
                     Logger::getInstance()->info($info->message);
                 }
             };
         }
         if (!$failCallback instanceof \Closure) {
             $failCallback = function (\Exception $exception) {
                 Logger::getInstance()->info($exception->getMessage());
             };
         }
         try {
             while ($response = $WSClient->receive()) {
                 $successCallback($response);
             }
         } catch (\Exception $e) {
             $failCallback($e);
         }
         return true;
     }
 }
Exemplo n.º 3
0
 /**
  * Call a remote REST web service URI and return the Zend_Http_Response object
  *
  * @param  string $path            The path to append to the URI
  * @throws Zend\Rest\Client\Exception\UnexpectedValueException
  * @return void
  */
 private final function _prepareRest($path)
 {
     // Get the URI object and configure it
     if (!$this->_uri instanceof Uri\Uri) {
         throw new Exception\UnexpectedValueException('URI object must be set before performing call');
     }
     $uri = $this->_uri->toString();
     if ($path[0] != '/' && $uri[strlen($uri) - 1] != '/') {
         $path = '/' . $path;
     }
     $this->_uri->setPath($path);
     /**
      * Get the HTTP client and configure it for the endpoint URI.  Do this 
      * each time as the Zend\Http\Client instance may be shared with other 
      * Zend\Service\AbstractService subclasses.
      */
     $this->getHttpClient()->resetParameters()->setUri($this->_uri);
 }
Exemplo n.º 4
0
 /**
  * Get clear uri.
  *
  * @static
  *
  * @param \Zend\Uri\Uri $uri
  *
  * @return boolean|\Zend\Uri\Uri False if uri is not auhorized
  */
 public static function clearUri(Uri $uri)
 {
     if ($uri->getScheme() !== 'http') {
         return false;
     }
     $query = $uri->getQueryAsArray();
     if (empty($query['v'])) {
         return false;
     }
     $uri->setQuery(array('v' => $query['v']));
     $uri->setHost('www.youtube.com');
     $uri->setPath('/watch');
     // clear
     $uri->setPort(0);
     $uri->setUserInfo('');
     $uri->setFragment('');
     return $uri;
 }
Exemplo n.º 5
0
 /**
  * Execute geocoding
  * 
  * @param  Request $request
  * @return Response
  */
 public function geocode(Request $request)
 {
     if (null === $request) {
         throw new Exception\InvalidArgumentException('request');
     }
     $uri = new Uri();
     $uri->setHost(self::GOOGLE_MAPS_APIS_URL);
     $uri->setPath(self::GOOGLE_GEOCODING_API_PATH);
     $urlParameters = $request->getUrlParameters();
     if (null === $urlParameters) {
         throw new Exception\RuntimeException('Invalid URL parameters');
     }
     $uri->setQuery($urlParameters);
     $client = $this->getHttpClient();
     $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl');
     $client->resetParameters();
     $uri->setScheme("https");
     $client->setUri($uri->toString());
     $stream = $client->send();
     $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY);
     $hydrator = new ArraySerializable();
     $response = new Response();
     $response->setRawBody($body);
     if (!isset($body['status'])) {
         throw new Exception\RuntimeException('Invalid status');
     }
     $response->setStatus($body['status']);
     if (!isset($body['results'])) {
         throw new Exception\RuntimeException('Invalid results');
     }
     $resultSet = new ResultSet();
     foreach ($body['results'] as $data) {
         $result = new Result();
         $hydrator->hydrate($data, $result);
         $resultSet->addElement($result);
     }
     $response->setResults($resultSet);
     return $response;
 }
Exemplo n.º 6
0
 /**
  * Data provider for invalid relative URI objects
  *
  * @return array
  */
 public function invalidRelativeUriObjectProvider()
 {
     // Empty URI is not valid
     $obj1 = new Uri();
     // Path cannot begin with '//'
     $obj2 = new Uri();
     $obj2->setPath('//path');
     // A object with port
     $obj3 = new Uri();
     $obj3->setPort(123);
     // A object with userInfo
     $obj4 = new Uri();
     $obj4->setUserInfo('shahar:password');
     // A object with scheme
     $obj5 = new Uri();
     $obj5->setScheme('https');
     // A object with host
     $obj6 = new Uri();
     $obj6->setHost('example.com');
     return array(array($obj1), array($obj2), array($obj3), array($obj4), array($obj5), array($obj6));
 }
Exemplo n.º 7
0
    /**
     * Data provider for invalid URI objects
     *
     * @return array
     */
    static public function invalidUriObjectProvider()
    {
        // Empty URI is not valid
        $obj1 = new Uri;

        // Path cannot begin with '//' if there is no authority part
        $obj2 = new Uri;
        $obj2->setPath('//path');

        // A port-only URI with no host
        $obj3 = new Uri;
        $obj3->setPort(123);

        // A userinfo-only URI with no host
        $obj4 = new Uri;
        $obj4->setUserInfo('shahar:password');

        return array(
            array($obj1),
            array($obj2),
            array($obj3),
            array($obj4)
        );
    }
Exemplo n.º 8
0
 /**
  * Set the path
  *
  * @param  string $path
  * @return \Zend\Uri\Uri
  */
 public function setPath($path)
 {
     $this->uri->setPath($path);
     return $this;
 }