7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\HttpFoundation\RequestStack;
9 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
10 use Drupal\common\JsonResponseTrait;
21 use JsonResponseTrait;
35 private $requestStack;
42 $this->requestStack = $requestStack;
48 public static function create(ContainerInterface $container) {
50 $requestStack = $container->get(
'request_stack');
65 $data = $this->datastoreService->summary($identifier);
66 return $this->getResponse($data);
68 catch (\Exception $e) {
69 $exception = new \Exception(
"A datastore for resource {$identifier} does not exist.");
70 return $this->getResponseFromException($exception, 404);
77 public function import() {
79 $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
80 $payload = json_decode($payloadJson);
82 if (isset($payload->resource_ids)) {
83 return $this->importMultiple($payload->resource_ids);
86 if (!isset($payload->resource_id)) {
87 return $this->getResponseFromException(
new \Exception(
"Invalid payload."));
91 $resourceId = $payload->resource_id;
92 $identifier = NULL; $version = NULL;
94 $results = $this->datastoreService->import($identifier, FALSE, $version);
95 return $this->getResponse($results);
97 catch (\Exception $e) {
98 return $this->getResponseFromException($e);
105 private function importMultiple(array $resourceIds) {
108 foreach ($resourceIds as $identifier) {
110 $results = $this->datastoreService->import($identifier, TRUE);
111 $responses[$identifier] = $results;
113 catch (\Exception $e) {
114 $responses[$identifier] = $e->getMessage();
118 return $this->getResponse($responses);
127 public function delete($identifier) {
129 $this->datastoreService->drop($identifier);
130 return $this->getResponse(
132 "identifier" => $identifier,
133 "message" =>
"The datastore for resource {$identifier} was successfully dropped.",
137 catch (\Exception $e) {
138 return $this->getResponseFromException($e);
149 $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
150 $payload = json_decode($payloadJson);
152 if (!isset($payload->resource_ids)) {
153 return $this->getResponseFromException(
new \Exception(
"Invalid payload."));
156 $identifiers = $payload->resource_ids;
159 foreach ($identifiers as $identifier) {
160 $responses[$identifier] = json_decode($this->
delete($identifier)->getContent());
163 return $this->getResponse($responses);
174 $data = $this->datastoreService->list();
175 return $this->getResponse($data);
177 catch (\Exception $e) {
178 return $this->getResponseFromException(
179 new \Exception(
"No importer data was returned. {$e->getMessage()}"),
193 $this->requestStack->getCurrentRequest(),
194 file_get_contents(__DIR__ .
"/../docs/query.json")
199 $result = $this->datastoreService->runQuery($datastoreQuery);
201 catch (\Exception $e) {
202 return $this->getResponseFromException($e, 400);
205 return $this->getResponse($result->{
"$"}, 200);
220 $this->prepareQueryResourcePayload($payloadJson, $identifier);
222 catch (\Exception $e) {
223 return $this->getResponseFromException(
224 new \Exception(
"Invalid query JSON: {$e->getMessage()}"),
231 $result = $this->datastoreService->runQuery($datastoreQuery);
233 catch (\Exception $e) {
234 return $this->getResponseFromException($e, 400);
237 return $this->getResponse($result->{
"$"}, 200);
247 $schema = json_decode(file_get_contents(__DIR__ .
"/../docs/query.json"), TRUE);
248 return $this->getResponse($schema, 200);
259 private function prepareQueryResourcePayload(&$json, $identifier) {
260 $data = json_decode($json);
261 if (json_last_error() !== JSON_ERROR_NONE) {
262 throw new \Exception(json_last_error_msg());
264 if (!empty($data->resources) || !empty($data->joins)) {
265 throw new \Exception(
"Joins are not available and "
266 .
"resources should not be explicitly passed when using the resource "
267 .
"query endpoint. Try /api/1/datastore/query.");
269 $resource = (object) [
"id" => $identifier,
"alias" =>
"t"];
270 $data->resources = [$resource];
271 $json = json_encode($data);