You are here

protected function HttpController::getHttpResponse in JSON-RPC 2.x

Same name and namespace in other branches
  1. 8 src/Controller/HttpController.php \Drupal\jsonrpc\Controller\HttpController::getHttpResponse()

Map RPC response(s) to an HTTP response.

Parameters

\Drupal\jsonrpc\Object\Response[] $rpc_responses: The RPC responses.

bool $is_batched_response: True if the response is batched.

Return value

\Drupal\Core\Cache\CacheableResponseInterface The cacheable HTTP version of the RPC response(s).

Throws

\Drupal\jsonrpc\Exception\JsonRpcException

1 call to HttpController::getHttpResponse()
HttpController::resolve in src/Controller/HttpController.php
Resolves an RPC request over HTTP.

File

src/Controller/HttpController.php, line 229

Class

HttpController
The main front controller.

Namespace

Drupal\jsonrpc\Controller

Code

protected function getHttpResponse(array $rpc_responses, $is_batched_response) {
  try {
    $serialized = $this
      ->serializeRpcResponse($rpc_responses, $is_batched_response);
    $http_response = CacheableJsonResponse::fromJsonString($serialized, Response::HTTP_OK);

    // Varies the response based on the 'query' parameter.
    $cache_context = (new CacheableMetadata())
      ->setCacheContexts([
      'url.query_args:query',
    ]);
    $http_response
      ->addCacheableDependency($cache_context);

    // Adds the cacheability information of the RPC response(s) to the HTTP
    // response.
    return array_reduce($rpc_responses, function (CacheableResponseInterface $http_response, $response) {
      return $http_response
        ->addCacheableDependency($response);
    }, $http_response);
  } catch (\Exception|\TypeError $e) {
    throw JsonRpcException::fromPrevious($e, FALSE, $this->handler
      ->supportedVersion());
  }
}