Inheritance: extends lithium\core\Object
Esempio n. 1
0
 public function testCurlGet()
 {
     $service = new Service(array('classes' => array('socket' => '\\lithium\\net\\socket\\Curl')));
     $service->head();
     $expected = array('code' => 200, 'message' => 'OK');
     $result = $service->last->response->status;
     $this->assertEqual($expected, $result);
 }
 /**
  *
  * Retrieve Stackoverflow tags I am active on, equivalent to the users tags `me` endpoint.
  *
  * @param	int		$min	Stackoverflow `min`
  * @return	object	Returns an array user tags objects.
  */
 public static function me_tags($min = 5)
 {
     if ($cache = Cache::read('memcache', 'stackoverflow_me_tags')) {
         return $cache;
     }
     $service = new Service(array('scheme' => 'https', 'host' => 'api.stackexchange.com', 'timeout' => 2));
     // using `userid` instead of `me` so we don't need to auth
     $data = $service->get('/' . self::API_VERSION . '/users/' . self::USERID . '/tags?key=' . self::KEY . '&order=desc&min=' . $min . '&sort=popular&site=stackoverflow');
     $me_tags = self::response_to_object($data)->items;
     Cache::write('memcache', 'stackoverflow_me_tags', $me_tags, '+2 hours');
     return $me_tags;
 }
Esempio n. 3
0
 /**
  * List users through API
  * Play around with changing the value of `q` to see
  * how the sent signature is unique for each combination of query paramters
  *
  * @param int $userId Id of user to make API call as
  * @param string $q Add a `q` argument to the URL to see it change
  */
 public function consume($userId = false, $q = '')
 {
     if (!$userId) {
         $this->error("Missing userId");
     }
     $user = Model::first($userId);
     $signature = $user->sign(array($this->path, 'q' => $q));
     $this->header("Generating different signatures for different urls");
     $this->columns(array(array('Path', 'Username', 'Signature'), array('/', $user->username, $user->sign(array('/', 'q' => $q))), array($this->path, $user->username, $signature)));
     $service = new Service(array('host' => $this->host));
     $resp = $service->get($this->path, compact('q'), array('type' => 'json', 'headers' => array('X_USERNAME' => $user->username, 'X_SIGNATURE' => $signature)));
     print_r($resp);
 }
 public function reset()
 {
     parent::reset();
     $isJson = !empty($this->request->headers['Content-Type']) && $this->request->headers['Content-Type'] == 'application/json';
     if ($isJson) {
         $this->response->body = json_encode(array('some' => 'json'));
     }
 }
 public function send($method, $path = null, $data = array(), array $options = array())
 {
     $defaults = array('headers' => array('User-Agent' => 'Lava Surfboard'));
     $xml = new \SimpleXmlElement('<request method="' . $path . '"></request>');
     foreach ($data as $key => $val) {
         if ($val != null) {
             $xml->{$key} = $val;
         }
     }
     $xmlString = $xml->asXML();
     $data = str_replace("<?xml version=\"1.0\"?>\n", '<!--?xml version="1.0" encoding="utf-8"?-->', $xmlString);
     $path = '/api/2.1/xml-in';
     $response = parent::send($method, $path, $data, $options + $defaults);
     return new \SimpleXmlElement($response);
 }
Esempio n. 6
0
 /**
  * Send request with the given options and data. The token should be part of the options.
  *
  * @param string $method
  * @param string $path
  * @param array $data encoded for the request
  * @param array $options oauth parameters
  *              - headers : send parameters in the header. (default: true)
  *              - realm : the realm to authenticate. (default: app directory name)
  * @return mixed the response from api call
  */
 public function send($method, $path = null, $data = array(), array $options = array())
 {
     $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH));
     $options += $defaults + $this->_config;
     $url = $this->config($path);
     $oauth = $this->sign($options + compact('data', 'url', 'method'));
     if ($options['headers']) {
         $header = 'OAuth realm="' . $options['realm'] . '",';
         foreach ($oauth as $key => $val) {
             $header .= $key . '="' . rawurlencode($val) . '",';
             unset($oauth[$key]);
         }
         $options['headers'] = array('Authorization' => $header);
     }
     $options['host'] = $options['proxy'] ? $options['proxy'] : $options['host'];
     $response = parent::send($method, $url, $data + $oauth, $options);
     if (strpos($response, 'oauth_token=') !== false) {
         return $this->_decode($response);
     }
     return $response;
 }
Esempio n. 7
0
 public function testSendConfiguringConnection()
 {
     $http = new Service($this->_testConfig);
     $result = $http->send('get', 'some-path/stuff', array(), array('someKey' => 'someValue'));
     $config = array_pop($http->connection->configs);
     $this->assertEqual('someValue', $config['someKey']);
 }
Esempio n. 8
0
 public function testMagicMethod()
 {
     $http = new Service($this->_testConfig);
     $response = $http->patch('some-path/stuff');
     $expected = "http://localhost:80/some-path/stuff";
     $result = $http->last->request->to('url');
     $this->assertEqual($expected, $result);
     $response = $http->patch('some-path/stuff', array('someData' => 'someValue'), array('return' => 'response'));
     $result = $http->last->request;
     $this->assertEqual('PATCH', $result->method);
     $this->assertEqual('lithium\\net\\http\\Response', get_class($response));
 }
Esempio n. 9
0
 /**
  * Send request with the given options and data. The token should be part of the options.
  *
  * @param string $method
  * @param string $path
  * @param array $data encoded for the request
  * @param array $options oauth parameters
  *              - headers : send parameters in the header. (default: true)
  *              - realm : the realm to authenticate. (default: app directory name)
  * @return mixed the response from api call
  */
 public function send($method, $path = null, $data = array(), array $options = array())
 {
     self::_parseParams($options);
     $data += $options['params'];
     $defaults = array('headers' => true, 'realm' => basename(LITHIUM_APP_PATH));
     $url = $this->config($path);
     $options['host'] = $this->_config['proxy'] ?: $this->_config['host'];
     $response = parent::send($method, $url, $data);
     $hasToken = strpos($response, 'access_token=') !== false;
     $isJson = strpos($response, '"data":') !== false;
     if ($hasToken && !$isJson) {
         return $this->_decode($response);
     }
     return $response;
 }
Esempio n. 10
0
 public function testDigestAuth()
 {
     $this->_testConfig += array('auth' => 'digest', 'username' => 'gwoo', 'password' => 'li3');
     $http = new Service($this->_testConfig);
     $response = $http->get('/http_auth/', array(), array('return' => 'response'));
     $this->assertEqual('success', $response->body());
 }
Esempio n. 11
0
 public function testRespondsTo()
 {
     $query = new Service();
     $this->assertTrue($query->respondsTo('foobarbaz'));
     $this->assertFalse($query->respondsTo(0));
 }
Esempio n. 12
0
 public function testCurlGet()
 {
     $service = new Service(array('classes' => array('socket' => '\\lithium\\net\\socket\\Curl')));
     $this->assertPattern('/localhost/', $service->get());
 }