From 0a6f4568b5e4f6291cc19cbea929ed1ebeca3a54 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 20 Nov 2016 13:08:41 +0100 Subject: Add ability to reload entry from API --- .../ApiBundle/Controller/EntryRestController.php | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/Wallabag') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index c5bf1df8..3303d47b 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -285,6 +285,54 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + /** + * Reload an entry. + * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error) + * + * @ApiDoc( + * requirements={ + * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} + * } + * ) + * + * @return JsonResponse + */ + public function patchEntriesReloadAction(Entry $entry) + { + $this->validateAuthentication(); + $this->validateUserAccess($entry->getUser()->getId()); + + // put default title in case of fetching content failed + $entry->setTitle('No title found'); + + try { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + + return new JsonResponse([], 304); + } + + // if refreshing entry failed, don't save it + if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { + return new JsonResponse([], 304); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + + $json = $this->get('serializer')->serialize($entry, 'json'); + + return (new JsonResponse())->setJson($json); + } + /** * Delete **permanently** an entry. * -- cgit v1.2.3 From 70584b42aaa75c2fe9adda5cf6c37ab63adcccb3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 20 Nov 2016 15:15:13 +0100 Subject: Fixing tests --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 3303d47b..7b7d94bf 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -287,7 +287,7 @@ class EntryRestController extends WallabagRestController /** * Reload an entry. - * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error) + * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). * * @ApiDoc( * requirements={ -- cgit v1.2.3 From 56da73969ac49e400ade89248f87c643047221d6 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 20 Nov 2016 16:25:13 +0100 Subject: Return an explicit error if reload fail --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7b7d94bf..1a726b6e 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -287,7 +287,7 @@ class EntryRestController extends WallabagRestController /** * Reload an entry. - * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). + * A response with HTTP Status 400 will be return if we weren't able to update the content (because it hasn't changed or we got an error). * * @ApiDoc( * requirements={ @@ -302,9 +302,6 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - // put default title in case of fetching content failed - $entry->setTitle('No title found'); - try { $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); } catch (\Exception $e) { @@ -313,12 +310,12 @@ class EntryRestController extends WallabagRestController 'entry' => $entry, ]); - return new JsonResponse([], 304); + return new JsonResponse(['error' => 'Error while trying to fetch content'], 400); } // if refreshing entry failed, don't save it if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { - return new JsonResponse([], 304); + return new JsonResponse(['error' => 'Error while trying to extract content'], 400); } $em = $this->getDoctrine()->getManager(); -- cgit v1.2.3