]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #2614 from wallabag/api-reload
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Tue, 22 Nov 2016 08:39:43 +0000 (09:39 +0100)
committerGitHub <noreply@github.com>
Tue, 22 Nov 2016 08:39:43 +0000 (09:39 +0100)
Add ability to reload entry from API

src/Wallabag/ApiBundle/Controller/EntryRestController.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index c5bf1df819e4183a53eb2de8fc8acb86b8eaf8b7..1a726b6edbcc776aa0ba2cb0f57ea862574732f1 100644 (file)
@@ -285,6 +285,51 @@ class EntryRestController extends WallabagRestController
         return (new JsonResponse())->setJson($json);
     }
 
+    /**
+     * Reload an entry.
+     * 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={
+     *          {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
+     *      }
+     * )
+     *
+     * @return JsonResponse
+     */
+    public function patchEntriesReloadAction(Entry $entry)
+    {
+        $this->validateAuthentication();
+        $this->validateUserAccess($entry->getUser()->getId());
+
+        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(['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(['error' => 'Error while trying to extract content'], 400);
+        }
+
+        $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.
      *
index 566e9493ae811e057daaf28836769f5e4c561e07..432ce7d8907222611a7d3de6e9a9f9ae3189ea6a 100644 (file)
@@ -678,4 +678,44 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
         $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
     }
+
+    public function testReloadEntryErrorWhileFetching()
+    {
+        $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
+
+        if (!$entry) {
+            $this->markTestSkipped('No content found in db.');
+        }
+
+        $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json');
+        $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
+
+        $this->assertContains('Error while trying to extract content', $this->client->getResponse()->getContent());
+
+        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+    }
+
+    public function testReloadEntry()
+    {
+        $this->client->request('POST', '/api/entries.json', [
+            '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',
+            'archive' => '1',
+            'tags' => 'google, apple',
+        ]);
+
+        $json = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->setUp();
+
+        $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json');
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertNotEmpty($content['title']);
+
+        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+    }
 }