Open Data Catalog v2.0.0
WebServiceApi.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\dkan_metastore;
4 
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;
14 
20 class WebServiceApi implements ContainerInjectionInterface {
21  use JsonResponseTrait;
22 
28  private $requestStack;
29 
35  private $service;
36 
42  public static function create(ContainerInterface $container) {
43  return new WebServiceApi(
44  $container->get('request_stack'),
45  $container->get('dkan_metastore.service')
46  );
47  }
48 
52  public function __construct(RequestStack $requestStack, Service $service) {
53  $this->requestStack = $requestStack;
54  $this->service = $service;
55  }
56 
60  public function getSchemas() {
61  return $this->getResponse($this->service->getSchemas());
62  }
63 
67  public function getSchema(string $identifier) {
68  return $this->getResponse($this->service->getSchema($identifier));
69  }
70 
80  public function getAll(string $schema_id) {
81  return $this->getResponse($this->service->getAll($schema_id));
82  }
83 
95  public function get(string $schema_id, string $identifier) {
96  try {
97  $object = json_decode($this->service->get($schema_id, $identifier));
98  return $this->getResponse($object);
99  }
100  catch (\Exception $e) {
101  return $this->getResponseFromException($e, 404);
102  }
103  }
104 
116  public function getResources(string $schema_id, string $identifier) {
117  try {
118  return $this->getResponse($this->service->getResources($schema_id, $identifier));
119  }
120  catch (\Exception $e) {
121  return $this->getResponseFromException($e, 404);
122  }
123  }
124 
134  public function post(string $schema_id) {
135  try {
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,
142  ], 201);
143  }
144  catch (MetastoreException $e) {
145  return $this->getResponseFromException($e, $e->httpCode());
146  }
147  catch (\Exception $e) {
148  return $this->getResponseFromException($e, 400);
149  }
150  }
151 
163  public function put($schema_id, string $identifier) {
164  try {
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);
170  }
171  catch (MetastoreException $e) {
172  return $this->getResponseFromException($e, $e->httpCode());
173  }
174  catch (\Exception $e) {
175  return $this->getResponseFromException($e, 400);
176  }
177  }
178 
190  public function patch($schema_id, $identifier) {
191 
192  try {
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]);
197  }
198  catch (MetastoreException $e) {
199  return $this->getResponseFromException($e, $e->httpCode());
200  }
201  catch (\Exception $e) {
202  return $this->getResponseFromException($e, 400);
203  }
204  }
205 
217  public function delete($schema_id, $identifier) {
218  try {
219  $this->service->delete($schema_id, $identifier);
220  return $this->getResponse((object) ["message" => "Dataset {$identifier} has been deleted."]);
221  }
222  catch (\Exception $e) {
223  return $this->getResponseFromException($e);
224  }
225  }
226 
233  public function getCatalog() : JsonResponse {
234  try {
235  return $this->getResponse($this->service->getCatalog());
236  }
237  catch (\Exception $e) {
238  return $this->getResponseFromException($e);
239  }
240  }
241 
245  private function checkData($data, $identifier = NULL) {
246 
247  if (empty($data)) {
248  throw new MissingPayloadException("Empty body");
249  }
250 
251  $obj = json_decode($data);
252 
253  if (!$obj) {
254  throw new InvalidJsonException("Invalid JSON");
255  }
256 
257  if (isset($identifier) && isset($obj->identifier) && $obj->identifier != $identifier) {
258  throw new CannotChangeUuidException("Identifier cannot be modified");
259  }
260  }
261 
268  private function getRequestUri(): string {
269  return $this->requestStack->getCurrentRequest()->getRequestUri();
270  }
271 
278  private function getRequestContent(): string {
279  return $this->requestStack->getCurrentRequest()->getContent();
280  }
281 
282 }
Drupal\dkan_metastore\Exception\InvalidJsonException
Definition: InvalidJsonException.php:10
Drupal\dkan_metastore\WebServiceApi\getAll
getAll(string $schema_id)
Definition: WebServiceApi.php:80
Drupal\dkan_metastore
Drupal\dkan_metastore\WebServiceApi\post
post(string $schema_id)
Definition: WebServiceApi.php:134
Drupal\dkan_metastore\Exception\MissingPayloadException
Definition: MissingPayloadException.php:10
Drupal\dkan_metastore\WebServiceApi\__construct
__construct(RequestStack $requestStack, Service $service)
Definition: WebServiceApi.php:52
Drupal\dkan_metastore\Exception\CannotChangeUuidException
Definition: CannotChangeUuidException.php:10
Drupal\dkan_metastore\WebServiceApi\getResources
getResources(string $schema_id, string $identifier)
Definition: WebServiceApi.php:116
Drupal\dkan_metastore\WebServiceApi\getSchema
getSchema(string $identifier)
Definition: WebServiceApi.php:67
Drupal\dkan_metastore\WebServiceApi\put
put($schema_id, string $identifier)
Definition: WebServiceApi.php:163
Drupal\dkan_metastore\WebServiceApi\getCatalog
getCatalog()
Definition: WebServiceApi.php:233
Drupal\dkan_metastore\WebServiceApi\create
static create(ContainerInterface $container)
Definition: WebServiceApi.php:42
Drupal\dkan_metastore\WebServiceApi
Definition: WebServiceApi.php:20
Drupal\dkan_metastore\Exception\MetastoreException
Definition: MetastoreException.php:10
Drupal\dkan_metastore\Exception\MetastoreException\httpCode
httpCode()
Drupal\dkan_metastore\Service
Definition: Service.php:20
Drupal\dkan_metastore\WebServiceApi\patch
patch($schema_id, $identifier)
Definition: WebServiceApi.php:190
Drupal\dkan_metastore\WebServiceApi\getSchemas
getSchemas()
Definition: WebServiceApi.php:60