diff --git a/apps/webhook_listeners/lib/Controller/WebhooksController.php b/apps/webhook_listeners/lib/Controller/WebhooksController.php index 42870825877b7..e7a05f0983ea7 100644 --- a/apps/webhook_listeners/lib/Controller/WebhooksController.php +++ b/apps/webhook_listeners/lib/Controller/WebhooksController.php @@ -10,8 +10,10 @@ namespace OCA\WebhookListeners\Controller; use OCA\WebhookListeners\Db\AuthMethod; +use OCA\WebhookListeners\Db\WebhookListener; use OCA\WebhookListeners\Db\WebhookListenerMapper; use OCA\WebhookListeners\ResponseDefinitions; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\ApiRoute; use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; @@ -20,6 +22,7 @@ use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSForbiddenException; +use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCSController; use OCP\IRequest; use OCP\ISession; @@ -45,15 +48,26 @@ public function __construct( * List registered webhooks * * @return DataResponse + * @throws OCSException Other internal error * * 200: Webhook registrations returned */ #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')] #[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')] public function index(): DataResponse { - $webhookListeners = $this->mapper->getAll(); + try { + $webhookListeners = $this->mapper->getAll(); - return new DataResponse($webhookListeners); + return new DataResponse( + array_map( + fn (WebhookListener $listener): array => $listener->jsonSerialize(), + $webhookListeners + ) + ); + } catch (\Exception $e) { + $this->logger->error('Error when listing webhooks', ['exception' => $e]); + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); + } } /** @@ -62,13 +76,22 @@ public function index(): DataResponse { * @param int $id id of the webhook * * @return DataResponse + * @throws OCSNotFoundException Webhook not found + * @throws OCSException Other internal error * * 200: Webhook registration returned */ #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')] #[AuthorizedAdminSetting(settings:'OCA\WebhookListeners\Settings\Admin')] public function show(int $id): DataResponse { - return new DataResponse($this->mapper->getById($id)); + try { + return new DataResponse($this->mapper->getById($id)->jsonSerialize()); + } catch (DoesNotExistException $e) { + throw new OCSNotFoundException($e->getMessage(), $e); + } catch (\Exception $e) { + $this->logger->error('Error when getting webhook', ['exception' => $e]); + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); + } } /** @@ -106,6 +129,11 @@ public function create( if ($this->session->get('app_api') === true) { $appId = $this->request->getHeader('EX-APP-ID'); } + try { + $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); + } catch (\ValueError $e) { + throw new OCSBadRequestException('This auth method does not exist'); + } try { $webhookListener = $this->mapper->addWebhookListener( $appId, @@ -115,17 +143,17 @@ public function create( $event, $eventFilter, $headers, - AuthMethod::from($authMethod ?? AuthMethod::None->value), + $authMethod, $authData, ); - return new DataResponse($webhookListener); + return new DataResponse($webhookListener->jsonSerialize()); } catch (\UnexpectedValueException $e) { throw new OCSBadRequestException($e->getMessage(), $e); } catch (\DomainException $e) { throw new OCSForbiddenException($e->getMessage(), $e); } catch (\Exception $e) { $this->logger->error('Error when inserting webhook', ['exception' => $e]); - throw new OCSException('An internal error occurred', $e->getCode(), $e); + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); } } @@ -166,6 +194,11 @@ public function update( if ($this->session->get('app_api') === true) { $appId = $this->request->getHeader('EX-APP-ID'); } + try { + $authMethod = AuthMethod::from($authMethod ?? AuthMethod::None->value); + } catch (\ValueError $e) { + throw new OCSBadRequestException('This auth method does not exist'); + } try { $webhookListener = $this->mapper->updateWebhookListener( $id, @@ -176,17 +209,17 @@ public function update( $event, $eventFilter, $headers, - AuthMethod::from($authMethod ?? AuthMethod::None->value), + $authMethod, $authData, ); - return new DataResponse($webhookListener); + return new DataResponse($webhookListener->jsonSerialize()); } catch (\UnexpectedValueException $e) { throw new OCSBadRequestException($e->getMessage(), $e); } catch (\DomainException $e) { throw new OCSForbiddenException($e->getMessage(), $e); } catch (\Exception $e) { $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]); - throw new OCSException('An internal error occurred', $e->getCode(), $e); + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); } } @@ -215,7 +248,7 @@ public function destroy(int $id): DataResponse { throw new OCSForbiddenException($e->getMessage(), $e); } catch (\Exception $e) { $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]); - throw new OCSException('An internal error occurred', $e->getCode(), $e); + throw new OCSException('An internal error occurred', Http::STATUS_INTERNAL_SERVER_ERROR, $e); } } } diff --git a/apps/webhook_listeners/lib/Settings/Admin.php b/apps/webhook_listeners/lib/Settings/Admin.php index e5e0d00221c20..5ef7656ca3e5e 100644 --- a/apps/webhook_listeners/lib/Settings/Admin.php +++ b/apps/webhook_listeners/lib/Settings/Admin.php @@ -9,6 +9,7 @@ namespace OCA\WebhookListeners\Settings; +use OCP\AppFramework\Http; use OCP\AppFramework\Http\TemplateResponse; use OCP\IL10N; use OCP\Settings\IDelegatedSettings; @@ -28,7 +29,8 @@ public function __construct( * Empty template response */ public function getForm(): TemplateResponse { - return new class($this->appName, '') extends TemplateResponse { + + return new /** @template-extends TemplateResponse */ class($this->appName, '') extends TemplateResponse { public function render(): string { return ''; } diff --git a/apps/webhook_listeners/openapi.json b/apps/webhook_listeners/openapi.json index 308f0e8b11df4..8488ce8e11758 100644 --- a/apps/webhook_listeners/openapi.json +++ b/apps/webhook_listeners/openapi.json @@ -411,6 +411,34 @@ } } } + }, + "404": { + "description": "Webhook not found", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } } } },