private function forwardInternal()

in modules/quanthub_sdmx_proxy/src/Controller/Forwarder.php [126:171]


  private function forwardInternal(Request $request, $api_url): Response {
    $headers = [];
    foreach ($request->headers->keys() as $key) {
      if (($key == 'content-type') || str_starts_with($key, 'accept') || str_starts_with($key, 'quanthub')) {
        $headers[$key] = $request->headers->get($key);
      }
    }
    $headers['authorization'] = 'Bearer ' . $this->userInfo->getToken();

    $options = [
      'headers' => $headers,
    ];
    if ($body = $request->getContent()) {
      $options['body'] = $body;
    }

    try {
      $uri = $request->query->get('uri');
      $psr7_response = $this->client->request($request->getMethod(), $api_url . $uri, $options);
      return $this->foundationFactory->createResponse($psr7_response);
    }
    catch (GuzzleException $exception) {
      // Get the original response.
      $response = $exception->getResponse();
      if (!is_null($response) && !is_null($response->getBody())) {
        // Get the info returned from the remote server.
        $response_info = $response->getBody()->getContents();
        // Using FormattableMarkup allows for the use of <pre/> tags,
        // giving a more readable log item.
        $message = new FormattableMarkup('API connection error. Error details are as follows:<pre>@response</pre>', ['@response' => print_r(json_decode($response_info), TRUE)]);
        // Log the error.
        $this->loggerFactory->get('quanthub_sdmx_proxy')->warning($message);
        return $this->foundationFactory->createResponse($exception->getResponse());
      }
      else {
        $this->loggerFactory->get('quanthub_sdmx_proxy')->warning($exception->getMessage());
        throw $exception;
      }
    }
    catch (ClientException $exception) {
      return $this->foundationFactory->createResponse($exception->getResponse());
    }
    catch (ServerException $exception) {
      return $this->foundationFactory->createResponse($exception->getResponse());
    }
  }