9 use Symfony\Component\HttpFoundation\JsonResponse;
10 use Symfony\Component\HttpFoundation\RequestStack;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
13 use Drupal\dkan_common\JsonResponseTrait;
21 use JsonResponseTrait;
28 private $requestStack;
42 public static function create(ContainerInterface $container) {
44 $container->get(
'request_stack'),
45 $container->get(
'dkan_metastore.service')
53 $this->requestStack = $requestStack;
54 $this->service = $service;
61 return $this->getResponse($this->service->getSchemas());
68 return $this->getResponse($this->service->getSchema($identifier));
80 public function getAll(
string $schema_id) {
81 return $this->getResponse($this->service->getAll($schema_id));
95 public function get(
string $schema_id,
string $identifier) {
97 $object = json_decode($this->service->get($schema_id, $identifier));
98 return $this->getResponse($object);
100 catch (\Exception $e) {
101 return $this->getResponseFromException($e, 404);
118 return $this->getResponse($this->service->getResources($schema_id, $identifier));
120 catch (\Exception $e) {
121 return $this->getResponseFromException($e, 404);
134 public function post(
string $schema_id) {
136 $data = $this->getRequestContent();
137 $this->checkData($data);
138 $identifier = $this->service->post($schema_id, $data);
139 return $this->getResponse([
140 "endpoint" =>
"{$this->getRequestUri()}/{$identifier}",
141 "identifier" => $identifier,
145 return $this->getResponseFromException($e, $e->
httpCode());
147 catch (\Exception $e) {
148 return $this->getResponseFromException($e, 400);
163 public function put($schema_id,
string $identifier) {
165 $data = $this->getRequestContent();
166 $this->checkData($data, $identifier);
167 $info = $this->service->put($schema_id, $identifier, $data);
168 $code = ($info[
'new'] == TRUE) ? 201 : 200;
169 return $this->getResponse([
"endpoint" => $this->getRequestUri(),
"identifier" => $info[
'identifier']], $code);
172 return $this->getResponseFromException($e, $e->
httpCode());
174 catch (\Exception $e) {
175 return $this->getResponseFromException($e, 400);
190 public function patch($schema_id, $identifier) {
193 $data = $this->getRequestContent();
194 $this->checkData($data, $identifier);
195 $this->service->patch($schema_id, $identifier, $data);
196 return $this->getResponse((
object) [
"endpoint" => $this->getRequestUri(),
"identifier" => $identifier]);
199 return $this->getResponseFromException($e, $e->
httpCode());
201 catch (\Exception $e) {
202 return $this->getResponseFromException($e, 400);
217 public function delete($schema_id, $identifier) {
219 $this->service->delete($schema_id, $identifier);
220 return $this->getResponse((
object) [
"message" =>
"Dataset {$identifier} has been deleted."]);
222 catch (\Exception $e) {
223 return $this->getResponseFromException($e);
235 return $this->getResponse($this->service->getCatalog());
237 catch (\Exception $e) {
238 return $this->getResponseFromException($e);
245 private function checkData($data, $identifier = NULL) {
251 $obj = json_decode($data);
254 throw new InvalidJsonException(
"Invalid JSON");
257 if (isset($identifier) && isset($obj->identifier) && $obj->identifier != $identifier) {
258 throw new CannotChangeUuidException(
"Identifier cannot be modified");
268 private function getRequestUri(): string {
269 return $this->requestStack->getCurrentRequest()->getRequestUri();
278 private function getRequestContent(): string {
279 return $this->requestStack->getCurrentRequest()->getContent();