]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
api/entries: add parameter detail to exclude or include content in response 3960/head
authorKevin Decherf <kevin@kdecherf.com>
Sat, 11 May 2019 22:00:00 +0000 (00:00 +0200)
committerKevin Decherf <kevin@kdecherf.com>
Sat, 18 May 2019 16:11:08 +0000 (18:11 +0200)
detail=metadata will nullify the content field of entries in order to
make smaller responses.

detail=full keeps the former behavior, it sends the content of entries.
It's the default, for backward compatibility.

Fixes #2817

Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 06520af917bc0745f58efe753fb34e3639b359dd..aff0534a056fcf0f08a124d3d093956e129ee6de 100644 (file)
@@ -103,6 +103,7 @@ class EntryRestController extends WallabagRestController
      *          {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
      *          {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
      *          {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"},
+     *          {"name"="detail", "dataType"="string", "required"=false, "format"="metadata or full, metadata by default", "description"="include content field if 'full'. 'full' by default for backward compatibility."},
      *       }
      * )
      *
@@ -121,6 +122,7 @@ class EntryRestController extends WallabagRestController
         $perPage = (int) $request->query->get('perPage', 30);
         $tags = \is_array($request->query->get('tags')) ? '' : (string) $request->query->get('tags', '');
         $since = $request->query->get('since', 0);
+        $detail = strtolower($request->query->get('detail', 'full'));
 
         try {
             /** @var \Pagerfanta\Pagerfanta $pager */
@@ -132,7 +134,8 @@ class EntryRestController extends WallabagRestController
                 $sort,
                 $order,
                 $since,
-                $tags
+                $tags,
+                $detail
             );
         } catch (\Exception $e) {
             throw new BadRequestHttpException($e->getMessage());
@@ -156,6 +159,7 @@ class EntryRestController extends WallabagRestController
                     'perPage' => $perPage,
                     'tags' => $tags,
                     'since' => $since,
+                    'detail' => $detail,
                 ],
                 true
             )
index f5089729643f94c67584445a4a0814c831949c0e..3990932e14a1261dcf49b3b9283fc5ede0182caf 100644 (file)
@@ -139,15 +139,30 @@ class EntryRepository extends EntityRepository
      * @param string $order
      * @param int    $since
      * @param string $tags
+     * @param string $detail     'metadata' or 'full'. Include content field if 'full'
+     *
+     * @todo Breaking change: replace default detail=full by detail=metadata in a future version
      *
      * @return Pagerfanta
      */
-    public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '')
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full')
     {
+        if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) {
+            throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata');
+        }
+
         $qb = $this->createQueryBuilder('e')
             ->leftJoin('e.tags', 't')
             ->where('e.user = :userId')->setParameter('userId', $userId);
 
+        if ('metadata' === $detail) {
+            $fieldNames = $this->getClassMetadata()->getFieldNames();
+            $fields = array_filter($fieldNames, function ($k) {
+                return 'content' !== $k;
+            });
+            $qb->select(sprintf('partial e.{%s}', implode(',', $fields)));
+        }
+
         if (null !== $isArchived) {
             $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived);
         }
index 8cc12ed379487ab3546416026cbb601112e61fd1..8b7898eea744795543f8b0f47e3d24d5da3921d2 100644 (file)
@@ -133,6 +133,27 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertSame(1, $content['page']);
         $this->assertGreaterThanOrEqual(1, $content['pages']);
 
+        $this->assertNotNull($content['_embedded']['items'][0]['content']);
+
+        $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+    }
+
+    public function testGetEntriesDetailMetadata()
+    {
+        $this->client->request('GET', '/api/entries?detail=metadata');
+
+        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertGreaterThanOrEqual(1, \count($content));
+        $this->assertNotEmpty($content['_embedded']['items']);
+        $this->assertGreaterThanOrEqual(1, $content['total']);
+        $this->assertSame(1, $content['page']);
+        $this->assertGreaterThanOrEqual(1, $content['pages']);
+
+        $this->assertNull($content['_embedded']['items'][0]['content']);
+
         $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
     }