Exemplo n.º 1
0
 /**
  * @param $url
  * @param array $params
  *
  * @return \Psr\Http\Message\UriInterface
  */
 private function makeUri($url, $params = array())
 {
     $uri = \GuzzleHttp\uri_template($this->endpoint, array_merge($this->defaults, $params));
     $uri = new Psr7\Uri($uri);
     // All arguments must be urlencoded (as per RFC 1738).
     $query = Psr7\build_query($params, PHP_QUERY_RFC1738);
     $uri = $uri->withQuery($query);
     return Psr7\Uri::withQueryValue($uri, 'url', $url);
 }
 /**
  * @param mixed $id
  * @return object
  */
 public function find($id)
 {
     try {
         $url = \GuzzleHttp\uri_template($this->getUriTemplate(), ['id' => $id]);
         $response = $this->client->request('GET', $this->createUri($url));
         $data = json_decode($response->getBody(), true);
         return $this->createObject($data);
     } catch (ClientException $ex) {
         if ($ex->getResponse()->getStatusCode() === 404) {
             throw new NoResultException(sprintf('Could not find object with id %s', $id), 0, $ex);
         }
         throw $ex;
     }
 }
Exemplo n.º 3
0
 /**
  *
  * @dataProvider requestsProvider
  */
 public function testRequests($method, $uri, $query, $reqData, $reqHeaders, $respStatus, $respHeaders, $respData)
 {
     $expandedUri = $uri;
     if (is_array($uri)) {
         $expandedUri = \GuzzleHttp\uri_template($uri[0], $uri[1]);
     }
     $reqOpts = ['headers' => $reqHeaders, 'query' => $query];
     if (!empty($reqData)) {
         if (is_array($reqData) || is_object($reqData)) {
             $reqOpts['json'] = $reqData;
         } else {
             $reqOpts['body'] = $reqData;
         }
     }
     if (null !== $respData) {
         $rawRespData = $respData;
         if (is_array($respData) || is_object($respData)) {
             $rawRespData = json_encode($respData);
         }
         $expResp = new Response($respStatus, $respHeaders, $rawRespData);
     } else {
         $expResp = new Response($respStatus, $respHeaders);
     }
     $httpClient = $this->getMockHttpClient();
     $httpClient->expects($this->once())->method('request')->with($this->equalTo($method), $this->equalTo($expandedUri), $this->equalTo($reqOpts))->will($this->returnValue($expResp));
     $token = new AccessToken(['access_token' => 'accessToken1', 'expires_in' => 100]);
     $client = new Client($token);
     $client->setHttpClient($httpClient);
     $data = $client->exec($uri, $method, $query, $reqData, $reqHeaders);
     if ($respStatus === 204) {
         $this->assertEquals(true, $data);
     } else {
         $this->assertEquals($respData, $data);
     }
     $this->assertEquals($expResp, $client->getResponse());
 }
Exemplo n.º 4
0
 private function configureBaseUrl(&$config)
 {
     if (!isset($config['base_url'])) {
         $this->baseUrl = new Url('', '');
     } elseif (is_array($config['base_url'])) {
         $this->baseUrl = Url::fromString(\GuzzleHttp\uri_template($config['base_url'][0], $config['base_url'][1]));
         $config['base_url'] = (string) $this->baseUrl;
     } else {
         $this->baseUrl = Url::fromString($config['base_url']);
     }
 }
Exemplo n.º 5
0
 public function testExpandsTemplate()
 {
     $this->assertEquals('foo/123', \GuzzleHttp\uri_template('foo/{bar}', ['bar' => '123']));
 }
 /**
  * @param string $method  The HTTP method.
  * @param string|array $uri optional: string with an href, or an array with template info
  * @param array $headers optional: Associative array of HTTP headers
  * @param array $formData optional: Array of form data
  * @param string $body optional: body of the request
  *
  * @return Request The request.
  */
 protected function createRequest($method, $uri = null, $headers = array(), $formData = null, $body = null)
 {
     if (is_array($formData)) {
         $body = http_build_query($formData, null, '&');
         $headers['Content-Type'] = 'application/x-www-form-urlencoded';
     }
     if (is_array($uri)) {
         if (!isset($uri[1])) {
             $uri = $uri[0];
         } else {
             $uri = \GuzzleHttp\uri_template($uri[0], $uri[1]);
         }
     }
     return new Request($method, $uri, $headers, $body);
 }
Exemplo n.º 7
0
 /**
  * Executes a request
  *
  * @param string|array|GuzzleHttp\Psr7\Uri $uri String, url template array or URL object
  * @param string $method
  * @param array|GuzzleHttp\Query $query
  * @param string|array|object $data
  * @param array $headers
  * @return array|string|bool
  * @throws GuzzleHttp\Exception\RequestException
  */
 public function exec($uri, $method = 'GET', $query = null, $data = null, array $headers = [])
 {
     if (is_array($uri)) {
         $uri = \GuzzleHttp\uri_template($uri[0], $uri[1]);
     }
     $method = strtoupper($method);
     if (!isset($headers['Authorization']) && null !== $this->token) {
         $headers['Authorization'] = 'Bearer ' . $this->token->getToken();
     }
     $reqOptions = ['headers' => $headers];
     if (is_array($query) || $query instanceof Query) {
         $reqOptions['query'] = $query;
     }
     if ($data !== null) {
         $this->handleRequestData($method, $data, $reqOptions);
     }
     $resp = $this->tryRequest($method, $uri, $reqOptions);
     if ($resp->getStatusCode() === 204) {
         // No Content, just return true
         return true;
     }
     return $this->parseResponse($resp);
 }