forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseApiV2Controller.php
More file actions
57 lines (48 loc) · 1.46 KB
/
BaseApiV2Controller.php
File metadata and controls
57 lines (48 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Controller;
use Closure;
use OCA\Polls\Exceptions\Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
/**
* @psalm-api
*/
class BaseApiV2Controller extends OCSController {
public function __construct(
string $appName,
IRequest $request,
string $corsMethods = 'PUT, POST, GET, DELETE',
string $corsAllowedHeaders = 'Authorization, Content-Type, Accept',
int $corsMaxAge = 1728000,
) {
parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge);
}
/**
* response
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function response(Closure $callback, int $successStatus = Http::STATUS_OK): DataResponse {
try {
return new DataResponse($callback(), $successStatus);
} catch (DoesNotExistException $e) {
throw new OCSNotFoundException($e->getMessage());
} catch (Exception $e) {
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
return new DataResponse(statusCode: $e->getStatus());
}
throw new OCSBadRequestException($e->getMessage());
}
}
}