private function createRequestForApiCall(ApiCallInterface $apiCall)
 {
     $request = $this->guzzleClient->createRequest($apiCall->getMethod(), $apiCall->getPath(), $apiCall->getHeaders(), $apiCall->getBody());
     $query = $request->getQuery();
     $query->merge($apiCall->getQuery());
     $query->set('api_key', $this->apiKey);
     return $request;
 }
 /**
  * @param string $endpoint
  * @param string $content
  * @param array $headers
  * @param array $files
  *
  * @return Response
  */
 public function send($endpoint, $content, array $headers = array(), array $files = array())
 {
     $request = $this->client->createRequest(RequestInterface::POST, $endpoint, $headers, $content, array('exceptions' => false));
     if ($files && $request instanceof EntityEnclosingRequestInterface) {
         $request->addPostFiles($files);
     }
     $response = $request->send();
     return new Response($response->getStatusCode(), $response->getBody(true));
 }
 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);
 }
Exemplo n.º 4
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);
        }
    }
 /**
  * Creates a request.
  *
  * @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
  * @param callable|null                                       $success         The success callable.
  * @param callable|null                                       $error           The error callable.
  *
  * @return \Ivory\HttpAdapter\Message\RequestInterface The request.
  */
 private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
 {
     $request = $this->client->createRequest($internalRequest->getMethod(), (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $this->prepareContent($internalRequest), array('exceptions' => false, 'allow_redirects' => false, 'timeout' => $this->getConfiguration()->getTimeout(), 'connect_timeout' => $this->getConfiguration()->getTimeout()));
     $request->setProtocolVersion($internalRequest->getProtocolVersion());
     if (is_callable($success)) {
         $messageFactory = $this->getConfiguration()->getMessageFactory();
         $request->getEventDispatcher()->addListener('request.success', function (Event $event) use($messageFactory, $success, $internalRequest) {
             $response = $messageFactory->createResponse($event['response']->getStatusCode(), $event['response']->getProtocolVersion(), $event['response']->getHeaders()->toArray(), BodyNormalizer::normalize(function () use($event) {
                 $resource = $event['response']->getBody()->getStream();
                 $event['response']->getBody()->detachStream();
                 return $resource;
             }, $internalRequest->getMethod()));
             $response = $response->withParameter('request', $internalRequest);
             call_user_func($success, $response);
         });
     }
     if (is_callable($error)) {
         $httpAdapterName = $this->getName();
         $request->getEventDispatcher()->addListener('request.exception', function (Event $event) use($error, $internalRequest, $httpAdapterName) {
             $exception = HttpAdapterException::cannotFetchUri($event['exception']->getRequest()->getUrl(), $httpAdapterName, $event['exception']->getMessage());
             $exception->setRequest($internalRequest);
             call_user_func($error, $exception);
         });
     }
     return $request;
 }
Exemplo n.º 6
0
 /**
  * @param $method
  * @param $fullUrl
  * @param array $query
  * @param string $body
  * @param array $headers
  * @param array $options
  * @return RequestInterface
  */
 public function prepareRequest($method, $fullUrl, array $query = array(), $body = '', array $headers = array(), array $options = array())
 {
     $query["sign"] = $this->getSignature($body);
     $headers["Authorization"] = "TOKEN " . $this->apiToken;
     $fullUrl = $this->formatUrl($fullUrl, $query);
     $request = $this->client->createRequest($method, $fullUrl, $headers, $body, $options);
     return $request;
 }
 /**
  * @param $url
  * @return int|null
  */
 private function getHttpStatus($url)
 {
     $request = $this->httpClient->createRequest('GET', $url);
     try {
         return $request->send()->getStatusCode();
     } catch (RequestException $e) {
     }
     return null;
 }
Exemplo n.º 8
0
 /**
  * @param string $httpMethod
  * @param string $path
  */
 protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
 {
     $path = $this->options['api_version'] . '/' . $path;
     if ($httpMethod === 'GET' && $body) {
         $path .= false === strpos($path, '?') ? '?' : '&';
         $path .= utf8_encode(http_build_query($body, '', '&'));
     }
     return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $body, $options);
 }
Exemplo n.º 9
0
 /**
  * get all actions
  *
  * @param ClientCommand $command
  *
  * @return Response|CommandResponse|DecryptedCommandResponse|StatusResponse
  */
 public function send(ClientCommand $command)
 {
     $request = $this->client->createRequest($command->method(), $this->client->getBaseUrl() . $command->uri($this->cryptographyEngine), $this->headers($command->apiVersion(), $command->format(), $command->headers()), $command->body($this->cryptographyEngine), $this->config->getHttpClientConfig());
     try {
         $response = $request->send();
     } catch (ClientErrorResponseException $exception) {
         $response = $exception->getResponse();
     } catch (ServerErrorResponseException $exception) {
         $response = $exception->getResponse();
     }
     return $command->response($response, $this->cryptographyEngine);
 }
Exemplo n.º 10
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.º 11
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.º 12
0
 /**
  * Create request
  *
  * @param string $method  HTTP method
  * @param string $url     URL
  * @param array  $headers HTTP headers
  *
  * @return RequestInterface
  */
 protected function createRequest($method, $url, array $headers = array())
 {
     return $this->client->createRequest($method, $url, $headers);
 }
Exemplo n.º 13
0
 /**
  * Create a request object to send to the server
  *
  * @param string $httpMethod
  * @param string $path
  * @param mixed  $body
  * @param array  $options
  *
  * @return \Guzzle\Http\Message\RequestInterface
  */
 protected function createRequest($httpMethod, $path, $body = null, array $options = array())
 {
     return $this->client->createRequest($httpMethod, $path, $this->headers, $body, $options);
 }