diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-11-20 13:08:41 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-11-20 13:08:41 +0100 |
commit | 0a6f4568b5e4f6291cc19cbea929ed1ebeca3a54 (patch) | |
tree | 26e9852f4b38d44b63a67216921c2a25feadd095 | |
parent | 5ae8788583a63973c89a5a83f1a6880ce04b49d7 (diff) | |
download | wallabag-0a6f4568b5e4f6291cc19cbea929ed1ebeca3a54.tar.gz wallabag-0a6f4568b5e4f6291cc19cbea929ed1ebeca3a54.tar.zst wallabag-0a6f4568b5e4f6291cc19cbea929ed1ebeca3a54.zip |
Add ability to reload entry from API
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 48 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 37 |
2 files changed, 85 insertions, 0 deletions
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 | |||
@@ -286,6 +286,54 @@ class EntryRestController extends WallabagRestController | |||
286 | } | 286 | } |
287 | 287 | ||
288 | /** | 288 | /** |
289 | * Reload an entry. | ||
290 | * 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) | ||
291 | * | ||
292 | * @ApiDoc( | ||
293 | * requirements={ | ||
294 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | ||
295 | * } | ||
296 | * ) | ||
297 | * | ||
298 | * @return JsonResponse | ||
299 | */ | ||
300 | public function patchEntriesReloadAction(Entry $entry) | ||
301 | { | ||
302 | $this->validateAuthentication(); | ||
303 | $this->validateUserAccess($entry->getUser()->getId()); | ||
304 | |||
305 | // put default title in case of fetching content failed | ||
306 | $entry->setTitle('No title found'); | ||
307 | |||
308 | try { | ||
309 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
310 | } catch (\Exception $e) { | ||
311 | $this->get('logger')->error('Error while saving an entry', [ | ||
312 | 'exception' => $e, | ||
313 | 'entry' => $entry, | ||
314 | ]); | ||
315 | |||
316 | return new JsonResponse([], 304); | ||
317 | } | ||
318 | |||
319 | // if refreshing entry failed, don't save it | ||
320 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | ||
321 | return new JsonResponse([], 304); | ||
322 | } | ||
323 | |||
324 | $em = $this->getDoctrine()->getManager(); | ||
325 | $em->persist($entry); | ||
326 | $em->flush(); | ||
327 | |||
328 | // entry saved, dispatch event about it! | ||
329 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
330 | |||
331 | $json = $this->get('serializer')->serialize($entry, 'json'); | ||
332 | |||
333 | return (new JsonResponse())->setJson($json); | ||
334 | } | ||
335 | |||
336 | /** | ||
289 | * Delete **permanently** an entry. | 337 | * Delete **permanently** an entry. |
290 | * | 338 | * |
291 | * @ApiDoc( | 339 | * @ApiDoc( |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 566e9493..3c8b7980 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -678,4 +678,41 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
678 | 678 | ||
679 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | 679 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); |
680 | } | 680 | } |
681 | |||
682 | public function testReloadEntryErrorWhileFetching() | ||
683 | { | ||
684 | $entry = $this->client->getContainer() | ||
685 | ->get('doctrine.orm.entity_manager') | ||
686 | ->getRepository('WallabagCoreBundle:Entry') | ||
687 | ->findOneBy(['user' => 1, 'isArchived' => false]); | ||
688 | |||
689 | if (!$entry) { | ||
690 | $this->markTestSkipped('No content found in db.'); | ||
691 | } | ||
692 | |||
693 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | ||
694 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); | ||
695 | } | ||
696 | |||
697 | public function testReloadEntry() | ||
698 | { | ||
699 | $this->client->request('POST', '/api/entries.json', [ | ||
700 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | ||
701 | 'archive' => '1', | ||
702 | 'tags' => 'google, apple', | ||
703 | ]); | ||
704 | |||
705 | $json = json_decode($this->client->getResponse()->getContent(), true); | ||
706 | |||
707 | $this->setUp(); | ||
708 | |||
709 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | ||
710 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
711 | |||
712 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
713 | |||
714 | $this->assertNotEmpty($content['title']); | ||
715 | |||
716 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
717 | } | ||
681 | } | 718 | } |