Open Data Catalog v2.0.0
WebServiceApi.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\datastore;
4 
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\HttpFoundation\RequestStack;
9 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
10 use Drupal\common\JsonResponseTrait;
12 
20 class WebServiceApi implements ContainerInjectionInterface {
21  use JsonResponseTrait;
22 
28  protected $datastoreService;
29 
35  private $requestStack;
36 
40  public function __construct(Service $datastoreService, RequestStack $requestStack) {
41  $this->datastoreService = $datastoreService;
42  $this->requestStack = $requestStack;
43  }
44 
48  public static function create(ContainerInterface $container) {
49  $datastoreService = $container->get('dkan.datastore.service');
50  $requestStack = $container->get('request_stack');
51  return new WebServiceApi($datastoreService, $requestStack);
52  }
53 
63  public function summary($identifier) {
64  try {
65  $data = $this->datastoreService->summary($identifier);
66  return $this->getResponse($data);
67  }
68  catch (\Exception $e) {
69  $exception = new \Exception("A datastore for resource {$identifier} does not exist.");
70  return $this->getResponseFromException($exception, 404);
71  }
72  }
73 
77  public function import() {
78 
79  $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
80  $payload = json_decode($payloadJson);
81 
82  if (isset($payload->resource_ids)) {
83  return $this->importMultiple($payload->resource_ids);
84  }
85 
86  if (!isset($payload->resource_id)) {
87  return $this->getResponseFromException(new \Exception("Invalid payload."));
88  }
89 
90  try {
91  $resourceId = $payload->resource_id;
92  $identifier = NULL; $version = NULL;
93  list($identifier, $version) = Resource::getIdentifierAndVersion($resourceId);
94  $results = $this->datastoreService->import($identifier, FALSE, $version);
95  return $this->getResponse($results);
96  }
97  catch (\Exception $e) {
98  return $this->getResponseFromException($e);
99  }
100  }
101 
105  private function importMultiple(array $resourceIds) {
106 
107  $responses = [];
108  foreach ($resourceIds as $identifier) {
109  try {
110  $results = $this->datastoreService->import($identifier, TRUE);
111  $responses[$identifier] = $results;
112  }
113  catch (\Exception $e) {
114  $responses[$identifier] = $e->getMessage();
115  }
116  }
117 
118  return $this->getResponse($responses);
119  }
120 
127  public function delete($identifier) {
128  try {
129  $this->datastoreService->drop($identifier);
130  return $this->getResponse(
131  [
132  "identifier" => $identifier,
133  "message" => "The datastore for resource {$identifier} was successfully dropped.",
134  ]
135  );
136  }
137  catch (\Exception $e) {
138  return $this->getResponseFromException($e);
139  }
140  }
141 
148  public function deleteMultiple() {
149  $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
150  $payload = json_decode($payloadJson);
151 
152  if (!isset($payload->resource_ids)) {
153  return $this->getResponseFromException(new \Exception("Invalid payload."));
154  }
155 
156  $identifiers = $payload->resource_ids;
157 
158  $responses = [];
159  foreach ($identifiers as $identifier) {
160  $responses[$identifier] = json_decode($this->delete($identifier)->getContent());
161  }
162 
163  return $this->getResponse($responses);
164  }
165 
172  public function list() {
173  try {
174  $data = $this->datastoreService->list();
175  return $this->getResponse($data);
176  }
177  catch (\Exception $e) {
178  return $this->getResponseFromException(
179  new \Exception("No importer data was returned. {$e->getMessage()}"),
180  404
181  );
182  }
183  }
184 
191  public function query() {
193  $this->requestStack->getCurrentRequest(),
194  file_get_contents(__DIR__ . "/../docs/query.json")
195  );
196 
197  try {
198  $datastoreQuery = new DatastoreQuery($payloadJson);
199  $result = $this->datastoreService->runQuery($datastoreQuery);
200  }
201  catch (\Exception $e) {
202  return $this->getResponseFromException($e, 400);
203  }
204 
205  return $this->getResponse($result->{"$"}, 200);
206  }
207 
217  public function queryResource($identifier) {
218  $payloadJson = RequestParamNormalizer::getJson($this->requestStack->getCurrentRequest());
219  try {
220  $this->prepareQueryResourcePayload($payloadJson, $identifier);
221  }
222  catch (\Exception $e) {
223  return $this->getResponseFromException(
224  new \Exception("Invalid query JSON: {$e->getMessage()}"),
225  400
226  );
227  }
228  try {
229  $payloadJson = RequestParamNormalizer::fixTypes($payloadJson, file_get_contents(__DIR__ . "/../docs/query.json"));
230  $datastoreQuery = new DatastoreQuery($payloadJson);
231  $result = $this->datastoreService->runQuery($datastoreQuery);
232  }
233  catch (\Exception $e) {
234  return $this->getResponseFromException($e, 400);
235  }
236 
237  return $this->getResponse($result->{"$"}, 200);
238  }
239 
246  public function querySchema() {
247  $schema = json_decode(file_get_contents(__DIR__ . "/../docs/query.json"), TRUE);
248  return $this->getResponse($schema, 200);
249  }
250 
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());
263  }
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.");
268  }
269  $resource = (object) ["id" => $identifier, "alias" => "t"];
270  $data->resources = [$resource];
271  $json = json_encode($data);
272  }
273 
274 }
Drupal\datastore\WebServiceApi\summary
summary($identifier)
Definition: WebServiceApi.php:63
Drupal\datastore\Service
Definition: Service.php:20
Drupal\datastore\WebServiceApi
Definition: WebServiceApi.php:20
Drupal\common\Resource
Definition: Resource.php:28
Drupal\datastore\Service\DatastoreQuery
Definition: DatastoreQuery.php:10
Drupal\datastore\WebServiceApi\list
list()
Definition: WebServiceApi.php:172
Drupal\common\Util\RequestParamNormalizer\getFixedJson
static getFixedJson(Request $request, string $schema=NULL)
Definition: RequestParamNormalizer.php:30
Drupal\datastore
Drupal\datastore\WebServiceApi\query
query()
Definition: WebServiceApi.php:191
Drupal\datastore\WebServiceApi\$datastoreService
$datastoreService
Definition: WebServiceApi.php:28
Drupal\datastore\WebServiceApi\create
static create(ContainerInterface $container)
Definition: WebServiceApi.php:48
Drupal\common\Util\RequestParamNormalizer\getJson
static getJson(Request $request)
Definition: RequestParamNormalizer.php:50
Drupal\datastore\WebServiceApi\__construct
__construct(Service $datastoreService, RequestStack $requestStack)
Definition: WebServiceApi.php:40
Drupal\common\Util\RequestParamNormalizer
Definition: RequestParamNormalizer.php:17
Drupal\common\Resource\getIdentifierAndVersion
static getIdentifierAndVersion($string)
Definition: Resource.php:197
Drupal\datastore\WebServiceApi\querySchema
querySchema()
Definition: WebServiceApi.php:246
Drupal\common\Util\RequestParamNormalizer\fixTypes
static fixTypes($json, $schema)
Definition: RequestParamNormalizer.php:77
Drupal\datastore\WebServiceApi\deleteMultiple
deleteMultiple()
Definition: WebServiceApi.php:148
Drupal\datastore\WebServiceApi\queryResource
queryResource($identifier)
Definition: WebServiceApi.php:217