Open Data Catalog v2.0.0
Api.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8 
16 class Api implements ContainerInjectionInterface {
17 
23  private $requestStack;
24 
30  private $harvester;
31 
37  public static function create(ContainerInterface $container) {
38  return new Api($container);
39  }
40 
44  public function __construct(ContainerInterface $container) {
45  $this->requestStack = $container->get('request_stack');
46  $this->harvester = $container->get('dkan_harvest.service');
47  }
48 
52  public function index() {
53 
54  try {
55 
56  $rows = $this->harvester
57  ->getAllHarvestIds();
58 
59  return new JsonResponse(
60  $rows,
61  200,
62  ["Access-Control-Allow-Origin" => "*"]
63  );
64  }
65  catch (\Exception $e) {
66  return $this->exceptionJsonResponse($e);
67  }
68  }
69 
79  public function getPlan($identifier) {
80  try {
81  $plan = $this->harvester
82  ->getHarvestPlan($identifier);
83 
84  return new JsonResponse(
85  json_decode($plan),
86  200,
87  ["Access-Control-Allow-Origin" => "*"]
88  );
89  }
90  catch (\Exception $e) {
91  return $this->exceptionJsonResponse($e);
92  }
93  }
94 
98  public function register() {
99  try {
100  $harvest_plan = $this->requestStack->getCurrentRequest()->getContent();
101  $plan = json_decode($harvest_plan);
102  $identifier = $this->harvester
103  ->registerHarvest($plan);
104 
105  return new JsonResponse(
106  (object) [
107  "identifier" => $identifier,
108  ],
109  200,
110  [
111  "Access-Control-Allow-Origin" => "*",
112  ]
113  );
114  }
115  catch (\Exception $e) {
116  return $this->exceptionJsonResponse($e);
117  }
118  }
119 
123  public function deregister($identifier) {
124 
125  try {
126 
127  $this->harvester
128  ->deregisterHarvest($identifier);
129 
130  return new JsonResponse(
131  (object) [
132  "identifier" => $identifier,
133  ],
134  200,
135  ["Access-Control-Allow-Origin" => "*"]
136  );
137  }
138  catch (\Exception $e) {
139  return $this->exceptionJsonResponse($e);
140  }
141  }
142 
146  public function run() {
147  try {
148  $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
149  $payload = json_decode($payloadJson);
150  if (!isset($payload->plan_id)) {
151  $return = [
152  "message" => "Invalid payload.",
153  "documentation" => "/api/1/harvest",
154  ];
155  return $this->jsonResponse($return, 422);
156  }
157 
158  $id = $payload->plan_id;
159  $result = $this->harvester
160  ->runHarvest($id);
161 
162  return new JsonResponse(
163  (object) [
164  "identifier" => $id,
165  "result" => $result,
166  ],
167  200,
168  ["Access-Control-Allow-Origin" => "*"]
169  );
170  }
171  catch (\Exception $e) {
172  return $this->exceptionJsonResponse($e);
173  }
174  }
175 
179  public function info() {
180 
181  try {
182  $id = $this->requestStack->getCurrentRequest()->get('plan');
183  if (empty($id)) {
184  return new JsonResponse(
185  ["message" => "Missing 'plan' query parameter value"],
186  400,
187  ["Access-Control-Allow-Origin" => "*"]
188  );
189  }
190 
191  $response = $this->harvester
192  ->getAllHarvestRunInfo($id);
193 
194  return new JsonResponse(
195  $response,
196  200,
197  ["Access-Control-Allow-Origin" => "*"]
198  );
199  }
200  catch (\Exception $e) {
201  return $this->exceptionJsonResponse($e);
202  }
203  }
204 
211  public function infoRun($identifier) {
212 
213  $id = $this->requestStack->getCurrentRequest()->get('plan');
214  if (empty($id)) {
215  return new JsonResponse(
216  ["message" => "Missing 'plan' query parameter value"],
217  400,
218  ["Access-Control-Allow-Origin" => "*"]
219  );
220  }
221 
222  try {
223  $response = $this->harvester
224  ->getHarvestRunInfo($id, $identifier);
225 
226  return new JsonResponse(
227  json_decode($response),
228  200,
229  ["Access-Control-Allow-Origin" => "*"]
230  );
231  }
232  catch (\Exception $e) {
233  return $this->exceptionJsonResponse($e);
234  }
235  }
236 
240  public function revert() {
241  try {
242 
243  $id = $this->requestStack->getCurrentRequest()->get('plan');
244  if (empty($id)) {
245  return new JsonResponse(
246  ["message" => "Missing 'plan' query parameter value"],
247  400,
248  ["Access-Control-Allow-Origin" => "*"]
249  );
250  }
251 
252  $result = $this->harvester
253  ->revertHarvest($id);
254 
255  return new JsonResponse(
256  (object) [
257  "identifier" => $id,
258  'result' => $result,
259  ],
260  200,
261  [
262  "Access-Control-Allow-Origin" => "*",
263  ]
264  );
265  }
266  catch (\Exception $e) {
267  return $this->exceptionJsonResponse($e);
268  }
269  }
270 
274  private function exceptionJsonResponse(\Exception $e, int $code = 400) {
275  return $this->jsonResponse(['message' => $e->getMessage()], $code);
276  }
277 
281  private function jsonResponse(array $return, int $code = 400) {
282  return new JsonResponse(
283  (object) $return,
284  $code
285  );
286  }
287 
288 }
Drupal\dkan_harvest\Controller
Definition: Api.php:3
Drupal\dkan_harvest\Controller\Api\revert
revert()
Definition: Api.php:240
Drupal\dkan_harvest\Controller\Api\index
index()
Definition: Api.php:52
Drupal\dkan_harvest\Controller\Api\deregister
deregister($identifier)
Definition: Api.php:123
Drupal\dkan_harvest\Controller\Api\__construct
__construct(ContainerInterface $container)
Definition: Api.php:44
Drupal\dkan_harvest\Controller\Api
Definition: Api.php:16
Drupal\dkan_harvest\Controller\Api\getPlan
getPlan($identifier)
Definition: Api.php:79
Drupal\dkan_harvest\Controller\Api\info
info()
Definition: Api.php:179
Drupal\dkan_harvest\Controller\Api\create
static create(ContainerInterface $container)
Definition: Api.php:37
Drupal\dkan_harvest\Controller\Api\infoRun
infoRun($identifier)
Definition: Api.php:211
Drupal\dkan_harvest\Controller\Api\run
run()
Definition: Api.php:146