src/Graviton/ProxyBundle/Controller/ProxyController.php
public function proxyAction(Request $request)
    {
        $api = $this->decideApiAndEndpoint($request->getUri());
        $this->registerProxySources($api['apiName']);
        $this->apiLoader->addOptions($api);

        $url = new Uri($this->apiLoader->getEndpoint($api['endpoint'], true));

        if ($url->getScheme() === null) {
            $url = $url->withScheme($request->getScheme());
        }

        $response = null;
        try {
            $newRequest = Request::create(
                (string) $url,
                $request->getMethod(),
                [],
                [],
                [],
                [],
                $request->getContent(false)
            );
            $newRequest->headers->add($request->headers->all());
            $newRequest->query->add($request->query->all());

            $newRequest = $this->transformationHandler->transformRequest(
                $api['apiName'],
                $api['endpoint'],
                $request,
                $newRequest
            );

            $psrRequest = $this->psrHttpFactory->createRequest($newRequest);

            $psrRequestUri = $psrRequest->getUri();
            $psrRequestUriPort = $url->getPort();
            if (is_numeric($psrRequestUriPort) || is_null($psrRequestUriPort)) {
                $psrRequestUri = $psrRequestUri->withPort($psrRequestUriPort);
            }

            $psrRequest = $psrRequest->withUri($psrRequestUri);

            $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url));
            $response = $this->httpFoundationFactory->createResponse($psrResponse);
            $this->cleanResponseHeaders($response->headers);
            $this->transformationHandler->transformResponse(
                $api['apiName'],
                $api['endpoint'],
                $response,
                clone $response
            );
        } catch (ClientException $e) {
            $response = (new HttpFoundationFactory())->createResponse($e->getResponse());
        } catch (ServerException $serverException) {
            $response = (new HttpFoundationFactory())->createResponse($serverException->getResponse());
        } catch (TransformationException $e) {
            $message = json_encode(
                ['code' => 404, 'message' => 'HTTP 404 Not found']
            );

            throw new NotFoundHttpException($message, $e);
        } catch (RequestException $e) {
            $message = json_encode(
                ['code' => 404, 'message' => 'HTTP 404 Not found']
            );

            throw new NotFoundHttpException($message, $e);
        }

        return $response;
    }
public function proxyAction(Request $request)
    {
        $api = $this->decideApiAndEndpoint($request->getUri());
        $this->registerProxySources($api['apiName']);
        $this->apiLoader->addOptions($api);

        $url = new Uri($this->apiLoader->getEndpoint($api['endpoint'], true));

        if ($url->getScheme() === null) {
            $url = $url->withScheme($request->getScheme());
        }

        $response = null;
        try {
            $newRequest = Request::create(
                (string) $url,
                $request->getMethod(),
                [],
                [],
                [],
                [],
                $request->getContent(false)
            );
            $newRequest->headers->add($request->headers->all());
            $newRequest->query->add($request->query->all());

            $newRequest = $this->transformationHandler->transformRequest(
                $api['apiName'],
                $api['endpoint'],
                $request,
                $newRequest
            );

            $psrRequest = $this->psrHttpFactory->createRequest($newRequest);

            $psrRequestUri = $psrRequest->getUri();
            $psrRequestUriPort = $url->getPort();
            if (is_numeric($psrRequestUriPort) || is_null($psrRequestUriPort)) {
                $psrRequestUri = $psrRequestUri->withPort($psrRequestUriPort);
            }

            $psrRequest = $psrRequest->withUri($psrRequestUri);

            $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url));
            $response = $this->httpFoundationFactory->createResponse($psrResponse);
            $this->cleanResponseHeaders($response->headers);
            $this->transformationHandler->transformResponse(
                $api['apiName'],
                $api['endpoint'],
                $response,
                clone $response
            );
        } catch (ClientException $e) {
            $response = (new HttpFoundationFactory())->createResponse($e->getResponse());
        } catch (ServerException $serverException) {
            $response = (new HttpFoundationFactory())->createResponse($serverException->getResponse());
        } catch (TransformationException $e) {
            $message = json_encode(
                ['code' => 404, 'message' => 'HTTP 404 Not found']
            );

            throw new NotFoundHttpException($message, $e);
        } catch (RequestException $e) {
            $message = json_encode(
                ['code' => 404, 'message' => 'HTTP 404 Not found']
            );

            throw new NotFoundHttpException($message, $e);
        }

        return $response;
    }
public function proxyAction ( Request $ request ) { $ api = $ this -> decideApiAndEndpoint ( $ request -> getUri ( ) ) ; $ this -> registerProxySources ( $ api [ 'apiName' ] ) ; $ this -> apiLoader -> addOptions ( $ api ) ; $ url = new Uri ( $ this -> apiLoader -> getEndpoint ( $ api [ 'endpoint' ] , true ) ) ; if ( $ url -> getScheme ( ) === null ) { $ url = $ url -> withScheme ( $ request -> getScheme ( ) ) ; } $ response = null ; try { $ newRequest = Request :: create ( ( string ) $ url , $ request -> getMethod ( ) , [ ] , [ ] , [ ] , [ ] , $ request -> getContent ( false ) ) ; $ newRequest -> headers -> add ( $ request -> headers -> all ( ) ) ; $ newRequest -> query -> add ( $ request -> query -> all ( ) ) ; $ newRequest = $ this -> transformationHandler -> transformRequest ( $ api [ 'apiName' ] , $ api [ 'endpoint' ] , $ request , $ newRequest ) ; $ psrRequest = $ this -> psrHttpFactory -> createRequest ( $ newRequest ) ; $ psrRequestUri = $ psrRequest -> getUri ( ) ; $ psrRequestUriPort = $ url -> getPort ( ) ; if ( is_numeric ( $ psrRequestUriPort ) || is_null ( $ psrRequestUriPort ) ) { $ psrRequestUri = $ psrRequestUri -> withPort ( $ psrRequestUriPort ) ; } $ psrRequest = $ psrRequest -> withUri ( $ psrRequestUri ) ; $ psrResponse = $ this -> proxy -> forward ( $ psrRequest ) -> to ( $ this -> getHostWithScheme ( $ url ) ) ; $ response = $ this -> httpFoundationFactory -> createResponse ( $ psrResponse ) ; $ this -> cleanResponseHeaders ( $ response -> headers ) ; $ this -> transformationHandler -> transformResponse ( $ api [ 'apiName' ] , $ api [ 'endpoint' ] , $ response , clone $ response ) ; } catch ( ClientException $ e ) { $ response = ( new HttpFoundationFactory ( ) ) -> createResponse ( $ e -> getResponse ( ) ) ; } catch ( ServerException $ serverException ) { $ response = ( new HttpFoundationFactory ( ) ) -> createResponse ( $ serverException -> getResponse ( ) ) ; } catch ( TransformationException $ e ) { $ message = json_encode ( [ 'code' => 404 , 'message' => 'HTTP 404 Not found' ] ) ; throw new NotFoundHttpException ( $ message , $ e ) ; } catch ( RequestException $ e ) { $ message = json_encode ( [ 'code' => 404 , 'message' => 'HTTP 404 Not found' ] ) ; throw new NotFoundHttpException ( $ message , $ e ) ; } return $ response ; }
action for routing all requests directly to the third party API

@param Request $request request

@return \Psr\Http\Message\ResponseInterface|Response
action for routing all requests directly to the third party API