예제 #1
0
 /**
  * @param array $data
  *
  * @return array|string|null
  *
  * @throws \Exception
  */
 public function send($data = null)
 {
     $request = new FormRequest($this->method, $this->resource, $this->host);
     if ($data) {
         $request->addFields($data);
     }
     try {
         $this->logger->addDebug('Request: ' . $request->getUrl());
         /** @var Buzz\Message\Response $response */
         $response = $this->client->send($request);
         $this->logger->addDebug('Response: ' . $response->getStatusCode() . ' ' . substr($response->getContent(), 0, 300) . PHP_EOL . var_export($this->client->getClient()->getInfo(), true));
     } catch (\Exception $e) {
         switch ($e->getCode()) {
             case 28:
                 $code = 504;
                 break;
             default:
                 $code = $e->getCode() >= 100 ? $e->getCode() : 500;
         }
         $this->logger->addCritical(PHP_EOL . __METHOD__ . sprintf('[%s/%s] %s', $e->getCode(), $code, $e->getMessage()));
         throw new WebGateException($e->getMessage(), $code, $e);
     }
     if ($response->getStatusCode() != 200) {
         throw new WebGateException(json_decode($response->getContent(), true), $response->getStatusCode());
     }
     return json_decode($response->getContent(), true);
 }
 /**
  * @param  Request  $request
  * @return Response
  */
 public function request(Request $request)
 {
     $buzzRequest = $this->prepareRequest($request);
     /** @var BuzzResponse $buzzResponse */
     $buzzResponse = $this->browser->send($buzzRequest);
     return new Response((string) $buzzResponse->getProtocolVersion(), $buzzResponse->getStatusCode(), $buzzResponse->getReasonPhrase(), HeaderConverter::convertRawToAssociative($buzzResponse->getHeaders()), $buzzResponse->getContent());
 }
예제 #3
0
 /**
  * Executes a http request.
  *
  * @param Request $request The http request.
  *
  * @return Response The http response.
  */
 public function execute(Request $request)
 {
     $method = $request->getMethod();
     $endpoint = $request->getEndpoint();
     $params = $request->getParams();
     $headers = $request->getHeaders();
     try {
         if ($method === 'GET') {
             $buzzResponse = $this->client->call($endpoint . '?' . http_build_query($params), $method, $headers, array());
         } else {
             $buzzRequest = new FormRequest();
             $buzzRequest->fromUrl($endpoint);
             $buzzRequest->setMethod($method);
             $buzzRequest->setHeaders($headers);
             foreach ($params as $key => $value) {
                 if ($value instanceof Image) {
                     $value = new FormUpload($value->getData());
                 }
                 $buzzRequest->setField($key, $value);
             }
             $buzzResponse = new BuzzResponse();
             $this->client->send($buzzRequest, $buzzResponse);
         }
     } catch (RequestException $e) {
         throw new Exception($e->getMessage());
     }
     return static::convertResponse($request, $buzzResponse);
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 protected function sendInternalRequest(InternalRequestInterface $internalRequest)
 {
     $this->browser->getClient()->setTimeout($this->getConfiguration()->getTimeout());
     $this->browser->getClient()->setMaxRedirects(0);
     $request = $this->browser->getMessageFactory()->createRequest($internalRequest->getMethod(), $uri = (string) $internalRequest->getUri());
     $request->setProtocolVersion($internalRequest->getProtocolVersion());
     $request->setHeaders($this->prepareHeaders($internalRequest, false));
     $data = $this->browser->getClient() instanceof AbstractCurl ? $this->prepareContent($internalRequest) : $this->prepareBody($internalRequest);
     $request->setContent($data);
     try {
         $response = $this->browser->send($request);
     } catch (\Exception $e) {
         throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
     }
     return $this->getConfiguration()->getMessageFactory()->createResponse($response->getStatusCode(), (string) $response->getProtocolVersion(), HeadersNormalizer::normalize($response->getHeaders()), BodyNormalizer::normalize($response->getContent(), $internalRequest->getMethod()));
 }
예제 #5
0
 /**
  * @param FormRequest $request
  * @return ApiData
  */
 private function sendRequest(FormRequest $request)
 {
     /**
      * @var Response $response
      */
     $response = $this->browser->send($request);
     return new ApiData($response, $this->reader->read($response));
 }
예제 #6
0
 /**
  * Fetch from the API
  * @param string $endpoint Fetch from the endpoint
  * @param array $params Any parameters
  * @return \stdClass|\stdClass[]
  */
 public static function fetch($endpoint, array $params = [])
 {
     $client = self::getClient();
     $browser = new Browser($client);
     $method = Request::METHOD_GET;
     foreach (self::$postEndpoints as $check) {
         if (preg_match("/^{$check}\$/", $endpoint)) {
             $method = Request::METHOD_POST;
         }
     }
     $request = new FormRequest($method, $endpoint, self::BASE_URL);
     $request->setFields($params);
     $response = new Response();
     $listener = new JsonListener();
     $browser->addListener($listener);
     $response = $browser->send($request, $response);
     return $response->getContent();
 }
 /**
  * @param $referenceFull
  *
  * @return Response
  *
  * @throws MediaStorageClientApiException
  */
 public function getMedia($referenceFull)
 {
     if (empty($referenceFull)) {
         throw new MediaStorageClientApiException('Param referenceFull is empty');
     }
     try {
         $request = new FormRequest();
         $request->setMethod(FormRequest::METHOD_GET);
         $request->setHost($this->getApiUrlConfig('base_url'));
         $url = $this->getApiUrlConfig('get_media_by_reference_full_url') . '/' . ltrim($referenceFull, '/');
         $request->setResource($url);
         $this->logger->debug('Send ' . $this->getApiUrlConfig('base_url') . $url);
         /** @var Response $response */
         $response = $this->client->send($request, null);
         $this->logger->debug('Response: ' . $response->getStatusCode() . ' ' . substr($response->getContent(), 0, 300));
     } catch (\Exception $e) {
         throw new MediaStorageClientApiException($e->getMessage());
     }
     return $response;
 }
예제 #8
0
 /**
  * Возвращает результаты запроса.
  *
  * @return mixed
  * @throws \Api\Exceptions\Exception
  */
 public function getResult()
 {
     $log = ['time' => microtime(true), 'uri' => sprintf('%s?%s', $this->uri, http_build_query($this->queryParameters)), 'code' => 200];
     $uri = sprintf('%s://%s/%s?%s', $this->api->getScheme(), $this->api->getDomain(), $this->uri, http_build_query(array_merge($this->queryParameters, ['appId' => $this->api->getAppId(), 'appKey' => $this->api->getAppKey()])));
     $browser = new Browser(new Curl());
     $request = new FormRequest($this->method, $uri);
     if ($this->method == RequestInterface::METHOD_POST && !empty($this->postParameters)) {
         foreach ($this->postParameters as $key => $value) {
             $request->setField($key, $value);
         }
     }
     /** @var \Buzz\Message\Response $result */
     $result = $browser->send($request);
     $exception = null;
     if ($result->isClientError() || $result->isServerError()) {
         switch ($result->getStatusCode()) {
             case 404:
                 $exception = new Http(sprintf('API returned 404 on request to «%s»: %s', $this->uri, $result->getContent()), 404);
                 break;
             default:
                 $exception = new Http(sprintf('API returned 500 on request to «%s»: %s', $this->uri, $result->getContent()), 500);
                 break;
         }
     }
     if (null !== $exception) {
         $log['time'] = microtime(true) - $log['time'];
         $log['code'] = $exception->getCode();
         $this->api->appendLog($log);
         throw $exception;
     }
     $json = json_decode($result->getContent(), true);
     $error = json_last_error();
     if ($error !== JSON_ERROR_NONE) {
         throw new Json(sprintf('%s on %s', $this->jsonError($error), $this->uri));
     }
     $log['time'] = microtime(true) - $log['time'];
     $this->api->appendLog($log);
     return $json;
 }