Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function load(ClassMetadata $metadata, ParserInterface $parser)
 {
     // Create a request with basic Auth
     $request = $this->client->get('repos/' . $this->owner . '/' . $this->repository . '/contents');
     try {
         // Send the request and get the response.
         $response = $request->send();
     } catch (ClientErrorResponseException $e) {
         $this->handleBadResponseExceptions($e);
     }
     $files = $response->json();
     $batch = array();
     foreach ($files as $file) {
         $batch[] = $this->client->get('repos/' . $this->owner . '/' . $this->repository . '/contents/' . $file['path']);
     }
     try {
         $responses = $this->client->send($batch);
     } catch (MultiTransferException $e) {
         $this->handleBadResponseExceptions($e->getFirst());
     }
     $content = array();
     foreach ($responses as $response) {
         $file = $response->json();
         $decodedContent = base64_decode($file['content']);
         $content[] = $parser->parse($metadata, $file['name'], $decodedContent);
     }
     return $content;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function doSendInternalRequests(array $internalRequests, $success, $error)
 {
     $requests = array();
     foreach ($internalRequests as $internalRequest) {
         $requests[] = $this->createRequest($internalRequest, $success, $error);
     }
     try {
         $this->client->send($requests);
     } catch (\Exception $e) {
     }
 }
Exemplo n.º 3
0
 /**
  * Immediately send all queued requests
  *
  * @param bool $async
  * @return $this
  */
 public function send($async = true)
 {
     // Guzzle 3 ignores async; use Guzzle 5 for that.
     try {
         $this->client->send($this->queue);
     } catch (MultiTransferException $exceptions) {
         foreach ($exceptions as $exception) {
             // TODO: handle exceptions gracefully
             throw $exception;
         }
     }
     return $this;
 }
 public function callApi(ApiCallInterface $apiCall)
 {
     $request = $this->createRequestForApiCall($apiCall);
     try {
         $response = $this->guzzleClient->send($request);
         if (!$response->isSuccessful()) {
             throw new RequestFailedException('Response not successful: ' . (string) $response);
         }
     } catch (RequestException $exception) {
         throw new RequestFailedException('Request failed: ' . $exception->getMessage(), null, $exception);
     }
     return $apiCall->createResponseDto($response->getBody());
 }
 public function download()
 {
     $progressFunctions = null;
     if ($this->getProgressFunction()) {
         $progressFunctions = $this->createProgressFunctions(array_fill(0, count($this->requests), 0));
     }
     $requests = array();
     foreach ($this->requests as $i => $r) {
         $requests[] = $request = $this->client->createRequest('GET', $r['url'], array(), null, array('save_to' => $r['file']));
         if ($progressFunctions !== null) {
             // Guzzle progress is too complex for my needs
             $request->getCurlOptions()->add(CURLOPT_NOPROGRESS, false)->add(CURLOPT_PROGRESSFUNCTION, $progressFunctions[$i]);
         }
     }
     $this->client->send($requests);
 }
 public function send()
 {
     if (!$this->client) {
         throw new RuntimeException('A client must be set on the request');
     }
     return $this->client->send($this);
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function load(ClassMetadata $metadata, ParserInterface $parser)
 {
     // Create a request with basic Auth
     $request = $this->client->get('metadata/dropbox/' . $this->path);
     try {
         // Send the request and get the response.
         $response = $request->send();
     } catch (ClientErrorResponseException $e) {
         $this->handleBadResponseExceptions($e);
     }
     $responseBody = $response->json();
     $batch = array();
     foreach ($responseBody['contents'] as $file) {
         $batch[] = $this->client->get('https://api-content.dropbox.com/1/files/dropbox' . $file['path']);
     }
     try {
         $responses = $this->client->send($batch);
     } catch (MultiTransferException $e) {
         $this->handleBadResponseExceptions($e->getFirst());
     }
     $content = array();
     foreach ($responses as $response) {
         $url = parse_url($response->getEffectiveUrl());
         $filename = basename($url['path']);
         $receivedContent = $response->getBody(true);
         $content[] = $parser->parse($metadata, $filename, $receivedContent);
     }
     return $content;
 }
Exemplo n.º 8
0
    /**
     * Sends all requests to each caching proxy server
     *
     * Requests are sent in parallel to minimise impact on performance.
     *
     * @param RequestInterface[] $requests Requests
     *
     * @throws ExceptionCollection
     */
    private function sendRequests(array $requests)
    {
        $allRequests = array();

        foreach ($requests as $request) {
            $headers = $request->getHeaders()->toArray();
            // Force to re-create Host header if empty, as Apache chokes on this. See #128 for discussion.
            if (empty($headers['Host'])) {
                unset( $headers['Host'] );
            }
            foreach ($this->servers as $server) {
                $proxyRequest = $this->client->createRequest(
                    $request->getMethod(),
                    $server . $request->getResource(),
                    $headers
                );
                $allRequests[] = $proxyRequest;
            }
        }

        try {
            $this->client->send($allRequests);
        } catch (GuzzleExceptionCollection $e) {
            $this->handleException($e);
        }
    }
Exemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function load(ClassMetadata $metadata, ParserInterface $parser, $retries = 0)
 {
     try {
         // Send the request and get the response.
         $response = $this->client->get('files/' . $this->folder . '/children')->send();
     } catch (ClientErrorResponseException $e) {
         if ($this->client->getDefaultOption('request.options/headers/Authorization') && $retries < 1) {
             $this->refreshToken();
             $retries++;
             return $this->load($metadata, $parser, $retries);
         }
         $this->handleBadResponseExceptions($e);
     }
     $json = $response->json();
     $files = $json['items'];
     $batch = array();
     foreach ($files as $file) {
         $batch[] = $this->client->get('files/' . $file['id']);
     }
     try {
         $responses = $this->client->send($batch);
     } catch (MultiTransferException $e) {
         $this->handleBadResponseExceptions($e->getFirst());
     }
     $content = array();
     foreach ($responses as $response) {
         $file = $response->json();
         $response = $this->client->get($file['downloadUrl'])->send();
         $downloadedContent = $response->getBody(true);
         $content[] = $parser->parse($metadata, $file['originalFilename'], $downloadedContent);
     }
     return $content;
 }
Exemplo n.º 10
0
 /**
  * {@inheritDoc}
  */
 public function request($path, $body = null, $httpMethod = 'GET', array $options = array())
 {
     $request = $this->createRequest($httpMethod, $path, $body, $options);
     try {
         $response = $this->client->send($request);
     } catch (RequestException $e) {
         throw new \LogicException($e->getMessage(), $e->getCode(), $e);
     }
     return $response;
 }
Exemplo n.º 11
0
 /**
  * {@inheritDoc}
  */
 public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
 {
     $request = $this->createRequest($httpMethod, $path, $body, $headers, $options);
     try {
         $response = $this->client->send($request);
     } catch (\LogicException $e) {
         throw new ErrorException($e->getMessage(), $e->getCode(), $e);
     } catch (\RuntimeException $e) {
         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
     }
     $this->lastRequest = $request;
     $this->lastResponse = $response;
     return $response;
 }
Exemplo n.º 12
0
 /**
  * @param string $method
  * @param string $relativeUrl
  * @param array $params
  * @return array
  * @throws Exception\BaseApiErrorResponseException
  * @throws Exception\RuntimeException
  * @throws Exception\RateLimitExceededException
  */
 public function request($method, $relativeUrl, array $params = [])
 {
     if (!$this->token instanceof AccessToken) {
         throw new RuntimeException('Not authorized yet.');
     }
     $request = $this->httpClient->createRequest($method, $relativeUrl, ['Authorization' => "Bearer {$this->token->accessToken}"], null, $params);
     try {
         $response = $this->httpClient->send($request);
         $body = json_decode($response->getBody(), true) or [];
         return $body;
     } catch (BadResponseException $e) {
         $body = json_decode($e->getResponse()->getBody(), true);
         switch ($body['error_description']) {
             case self::ACCESS_TOKEN_EXPIRED_MESSAGE:
                 $this->refresh();
                 return $this->request($method, $relativeUrl, $params);
             case self::RATE_LIMIT_EXCEEDED_MESSAGE:
                 throw new RateLimitExceededException(self::RATE_LIMIT_EXCEEDED_MESSAGE);
             default:
                 throw new BaseApiErrorResponseException($body, $e->getResponse()->getStatusCode());
         }
     }
 }
Exemplo n.º 13
0
 /**
  * Sends all requests to each caching proxy server.
  *
  * Requests are sent in parallel to minimise impact on performance.
  *
  * @param RequestInterface[] $requests Requests
  *
  * @throws ExceptionCollection
  */
 private function sendRequests(array $requests)
 {
     $allRequests = [];
     foreach ($requests as $request) {
         /* @var RequestInterface $request */
         $proxyRequest = $this->client->createRequest($request->getMethod(), $request->getUrl(), $request->getHeaders());
         $allRequests[] = $proxyRequest;
     }
     try {
         $this->client->send($allRequests);
     } catch (MultiTransferException $e) {
         $this->handleException($e);
     }
 }
Exemplo n.º 14
0
 /**
  * {@inheritDoc}
  */
 public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
 {
     //print_r ( $path);
     //$path = "https://api.trello.com/" . $path;
     //$path = "/members/me/boards";
     //exit;
     $request = $this->createRequest($httpMethod, $path, $body, $headers, $options);
     //print_r ( $request);
     try {
         $response = $this->client->send($request);
     } catch (\LogicException $e) {
         throw new ErrorException($e->getMessage(), $e->getCode(), $e);
     } catch (\RuntimeException $e) {
         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
     }
     $this->lastRequest = $request;
     $this->lastResponse = $response;
     return $response;
 }