You are here

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

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

Get the JSON RPC request objects for the given Request object.

Parameters

\Symfony\Component\HttpFoundation\Request $http_request: The HTTP request.

Return value

\Drupal\jsonrpc\Object\Request[] The JSON-RPC request or requests.

Throws

\Drupal\jsonrpc\Exception\JsonRpcException When there was an error handling the response.

2 calls to HttpController::getRpcRequests()
HttpController::preflight in src/Controller/HttpController.php
Resolves a preflight request for an RPC request.
HttpController::resolve in src/Controller/HttpController.php
Resolves an RPC request over HTTP.

File

src/Controller/HttpController.php, line 167

Class

HttpController
The main front controller.

Namespace

Drupal\jsonrpc\Controller

Code

protected function getRpcRequests(Request $http_request) {
  $version = $this->handler
    ->supportedVersion();
  try {
    if ($http_request
      ->getMethod() === Request::METHOD_POST) {
      $content = Json::decode($http_request
        ->getContent(FALSE));
    }
    elseif ($http_request
      ->getMethod() === Request::METHOD_GET) {
      $content = Json::decode($http_request->query
        ->get('query'));
    }
    elseif ($http_request
      ->getMethod() === Request::METHOD_OPTIONS) {

      // OPTIONS requests generated from a browser during a preflight will not
      // contain a body.
      $content = Json::decode($http_request->query
        ->get('query'));
    }
    $context = new Context([
      RpcRequestFactory::REQUEST_VERSION_KEY => $version,
    ]);
    $factory = new RpcRequestFactory($this->handler, $this->container, $this->validator);
    return $factory
      ->transform($content, $context);
  } catch (\Exception $e) {
    $id = isset($content) && is_object($content) && isset($content->id) ? $content->id : FALSE;
    throw JsonRpcException::fromPrevious($e, $id, $version);
  } catch (\TypeError $e) {
    $id = isset($content) && is_object($content) && isset($content->id) ? $content->id : FALSE;
    throw JsonRpcException::fromPrevious($e, $id, $version);
  }
}