diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-11-22 09:39:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-22 09:39:43 +0100 |
commit | ba838dae5ac5bf0a65165ee075138aefbccbc3a2 (patch) | |
tree | 02130c39367f10e1a721926f4f94f2fb71eb5ca6 | |
parent | c72c6f43f2f851448a34f87bc11e24021326346c (diff) | |
parent | 56da73969ac49e400ade89248f87c643047221d6 (diff) | |
download | wallabag-ba838dae5ac5bf0a65165ee075138aefbccbc3a2.tar.gz wallabag-ba838dae5ac5bf0a65165ee075138aefbccbc3a2.tar.zst wallabag-ba838dae5ac5bf0a65165ee075138aefbccbc3a2.zip |
Merge pull request #2614 from wallabag/api-reload
Add ability to reload entry from API
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 45 | ||||
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 40 |
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..1a726b6e 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -286,6 +286,51 @@ class EntryRestController extends WallabagRestController | |||
286 | } | 286 | } |
287 | 287 | ||
288 | /** | 288 | /** |
289 | * Reload an entry. | ||
290 | * 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). | ||
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 | try { | ||
306 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
307 | } catch (\Exception $e) { | ||
308 | $this->get('logger')->error('Error while saving an entry', [ | ||
309 | 'exception' => $e, | ||
310 | 'entry' => $entry, | ||
311 | ]); | ||
312 | |||
313 | return new JsonResponse(['error' => 'Error while trying to fetch content'], 400); | ||
314 | } | ||
315 | |||
316 | // if refreshing entry failed, don't save it | ||
317 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | ||
318 | return new JsonResponse(['error' => 'Error while trying to extract content'], 400); | ||
319 | } | ||
320 | |||
321 | $em = $this->getDoctrine()->getManager(); | ||
322 | $em->persist($entry); | ||
323 | $em->flush(); | ||
324 | |||
325 | // entry saved, dispatch event about it! | ||
326 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
327 | |||
328 | $json = $this->get('serializer')->serialize($entry, 'json'); | ||
329 | |||
330 | return (new JsonResponse())->setJson($json); | ||
331 | } | ||
332 | |||
333 | /** | ||
289 | * Delete **permanently** an entry. | 334 | * Delete **permanently** an entry. |
290 | * | 335 | * |
291 | * @ApiDoc( | 336 | * @ApiDoc( |
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 566e9493..432ce7d8 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -678,4 +678,44 @@ 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()->get('doctrine.orm.entity_manager') | ||
685 | ->getRepository('WallabagCoreBundle:Entry') | ||
686 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | ||
687 | |||
688 | if (!$entry) { | ||
689 | $this->markTestSkipped('No content found in db.'); | ||
690 | } | ||
691 | |||
692 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | ||
693 | $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); | ||
694 | |||
695 | $this->assertContains('Error while trying to extract content', $this->client->getResponse()->getContent()); | ||
696 | |||
697 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
698 | } | ||
699 | |||
700 | public function testReloadEntry() | ||
701 | { | ||
702 | $this->client->request('POST', '/api/entries.json', [ | ||
703 | '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', | ||
704 | 'archive' => '1', | ||
705 | 'tags' => 'google, apple', | ||
706 | ]); | ||
707 | |||
708 | $json = json_decode($this->client->getResponse()->getContent(), true); | ||
709 | |||
710 | $this->setUp(); | ||
711 | |||
712 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | ||
713 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
714 | |||
715 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
716 | |||
717 | $this->assertNotEmpty($content['title']); | ||
718 | |||
719 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | ||
720 | } | ||
681 | } | 721 | } |