From 1eca7831a69b9470b92dcc72e1ce51b42b291338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 10:22:57 +0200 Subject: Added API endpoint to handle a list of URL By passing an array, you can add / delete URL in mass (bulk request) --- .../ApiBundle/Controller/EntryRestController.php | 71 ++++++++++++++++++++++ .../Controller/EntryRestControllerTest.php | 46 +++++++++++--- 2 files changed, 109 insertions(+), 8 deletions(-) diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7590efbb..3833ce3c 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -172,6 +172,77 @@ class EntryRestController extends WallabagRestController ->exportAs($request->attributes->get('_format')); } + /** + * Handles an entries list and create or remove URL. + * + * @ApiDoc( + * parameters={ + * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesListAction(Request $request) + { + $this->validateAuthentication(); + + $list = json_decode($request->query->get('list', [])); + $results = []; + + // handle multiple urls + if (!empty($list)) { + $results = []; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); + + $results[$key]['url'] = $element->url; + $results[$key]['action'] = $element->action; + + switch ($element->action) { + case 'delete': + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); + + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); + } + + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + + break; + case 'add': + if (false === $entry) { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + new Entry($this->getUser()), + $element->url + ); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + + break; + } + } + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } + /** * Create an entry. * diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 19fb5170..c37d08cb 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -766,20 +766,50 @@ class EntryRestControllerTest extends WallabagApiTestCase ], ]; - $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); + $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); + } + + public function testPostMassEntriesAction() + { + $list = [ + [ + 'url' => 'http://0.0.0.0/entry2', + 'action' => 'delete', + ], + [ + 'url' => 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', + 'action' => 'add', + ], + [ + 'url' => 'http://0.0.0.0/entry3', + 'action' => 'delete', + ], + [ + 'url' => 'http://0.0.0.0/entry6', + 'action' => 'add', + ], + ]; + + $this->client->request('POST', '/api/entries/lists?list='.json_encode($list)); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertInternalType('int', $content[0]['entry']); - $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']); + $this->assertTrue($content[0]['entry']); + $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); + $this->assertEquals('delete', $content[0]['action']); - $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:Entry') - ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); + $this->assertInternalType('int', $content[1]['entry']); + $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[1]['url']); + $this->assertEquals('add', $content[1]['action']); - $tags = $entry->getTags(); - $this->assertCount(2, $tags); + $this->assertFalse($content[2]['entry']); + $this->assertEquals('http://0.0.0.0/entry3', $content[2]['url']); + $this->assertEquals('delete', $content[2]['action']); + + $this->assertInternalType('int', $content[3]['entry']); + $this->assertEquals('http://0.0.0.0/entry6', $content[3]['url']); + $this->assertEquals('add', $content[3]['action']); } } -- cgit v1.2.3 From a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 11:31:00 +0200 Subject: Splitted the endpoint in two --- .../ApiBundle/Controller/EntryRestController.php | 96 ++++++++++++++-------- .../Controller/EntryRestControllerTest.php | 55 ++++++------- 2 files changed, 88 insertions(+), 63 deletions(-) diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 3833ce3c..0c98c242 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -173,68 +173,96 @@ class EntryRestController extends WallabagRestController } /** - * Handles an entries list and create or remove URL. + * Handles an entries list and delete URL. * * @ApiDoc( * parameters={ - * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} + * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} * } * ) * * @return JsonResponse */ - public function postEntriesListAction(Request $request) + public function deleteEntriesListAction(Request $request) { $this->validateAuthentication(); - $list = json_decode($request->query->get('list', [])); + $urls = json_decode($request->query->get('urls', [])); $results = []; // handle multiple urls - if (!empty($list)) { + if (!empty($urls)) { $results = []; - foreach ($list as $key => $element) { + foreach ($urls as $key => $url) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, + $url, $this->getUser()->getId() ); - $results[$key]['url'] = $element->url; - $results[$key]['action'] = $element->action; + $results[$key]['url'] = $url; - switch ($element->action) { - case 'delete': - if (false !== $entry) { - $em = $this->getDoctrine()->getManager(); - $em->remove($entry); - $em->flush(); + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); - // entry deleted, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - } + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); + } - $results[$key]['entry'] = $entry instanceof Entry ? true : false; + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + } + } - break; - case 'add': - if (false === $entry) { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( - new Entry($this->getUser()), - $element->url - ); - } + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Handles an entries list and create URL. + * + * @ApiDoc( + * parameters={ + * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesListAction(Request $request) + { + $this->validateAuthentication(); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); + $urls = json_decode($request->query->get('urls', [])); + $results = []; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + // handle multiple urls + if (!empty($urls)) { + $results = []; + foreach ($urls as $key => $url) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); - // entry saved, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + $results[$key]['url'] = $url; - break; + if (false === $entry) { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + new Entry($this->getUser()), + $url + ); } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); } } diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index c37d08cb..64c24a2d 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -769,47 +769,44 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); } - public function testPostMassEntriesAction() + + public function testPostEntriesListAction() { $list = [ - [ - 'url' => 'http://0.0.0.0/entry2', - 'action' => 'delete', - ], - [ - 'url' => 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', - 'action' => 'add', - ], - [ - 'url' => 'http://0.0.0.0/entry3', - 'action' => 'delete', - ], - [ - 'url' => 'http://0.0.0.0/entry6', - 'action' => 'add', - ], + 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', + 'http://0.0.0.0/entry6', ]; - $this->client->request('POST', '/api/entries/lists?list='.json_encode($list)); + $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertTrue($content[0]['entry']); - $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); - $this->assertEquals('delete', $content[0]['action']); + $this->assertInternalType('int', $content[0]['entry']); + $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); $this->assertInternalType('int', $content[1]['entry']); - $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[1]['url']); - $this->assertEquals('add', $content[1]['action']); + $this->assertEquals('http://0.0.0.0/entry6', $content[1]['url']); + } + + public function testDeleteEntriesListAction() + { + $list = [ + 'http://0.0.0.0/entry2', + 'http://0.0.0.0/entry3', + ]; + + $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); - $this->assertFalse($content[2]['entry']); - $this->assertEquals('http://0.0.0.0/entry3', $content[2]['url']); - $this->assertEquals('delete', $content[2]['action']); + $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertTrue($content[0]['entry']); + $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); - $this->assertInternalType('int', $content[3]['entry']); - $this->assertEquals('http://0.0.0.0/entry6', $content[3]['url']); - $this->assertEquals('add', $content[3]['action']); + $this->assertFalse($content[1]['entry']); + $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); } } -- cgit v1.2.3 From 719ba257d3d6495eaa0f4ea749b5aa4740f94dda Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 29 Apr 2017 12:24:27 +0200 Subject: Fix tests --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 1 - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 0c98c242..ae6f0e3f 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -239,7 +239,6 @@ class EntryRestController extends WallabagRestController // handle multiple urls if (!empty($urls)) { - $results = []; foreach ($urls as $key => $url) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( $url, diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 64c24a2d..b732a8be 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -774,7 +774,7 @@ class EntryRestControllerTest extends WallabagApiTestCase { $list = [ 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', - 'http://0.0.0.0/entry6', + 'http://0.0.0.0/entry1', ]; $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); @@ -787,13 +787,13 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); $this->assertInternalType('int', $content[1]['entry']); - $this->assertEquals('http://0.0.0.0/entry6', $content[1]['url']); + $this->assertEquals('http://0.0.0.0/entry1', $content[1]['url']); } public function testDeleteEntriesListAction() { $list = [ - 'http://0.0.0.0/entry2', + 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', 'http://0.0.0.0/entry3', ]; @@ -804,7 +804,7 @@ class EntryRestControllerTest extends WallabagApiTestCase $content = json_decode($this->client->getResponse()->getContent(), true); $this->assertTrue($content[0]['entry']); - $this->assertEquals('http://0.0.0.0/entry2', $content[0]['url']); + $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); $this->assertFalse($content[1]['entry']); $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); -- cgit v1.2.3 From 7fa844a34983f9929348e70ddd408cf6ba5811b6 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 29 Apr 2017 12:32:26 +0200 Subject: Fix tests (for real this time) --- tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index b732a8be..88a5be93 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -774,7 +774,7 @@ class EntryRestControllerTest extends WallabagApiTestCase { $list = [ 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', - 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry2', ]; $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); @@ -787,7 +787,7 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); $this->assertInternalType('int', $content[1]['entry']); - $this->assertEquals('http://0.0.0.0/entry1', $content[1]['url']); + $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); } public function testDeleteEntriesListAction() -- cgit v1.2.3 From efd351c98fa0caa4c8df9c7ff6965c537524f12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 1 May 2017 09:21:59 +0200 Subject: Added limit --- app/config/config.yml | 1 + .../ApiBundle/Controller/EntryRestController.php | 9 +++++++++ .../DependencyInjection/Configuration.php | 3 +++ .../DependencyInjection/WallabagCoreExtension.php | 1 + .../Controller/EntryRestControllerTest.php | 23 ++++++++++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/app/config/config.yml b/app/config/config.yml index 4f4fb900..451809d6 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -55,6 +55,7 @@ wallabag_core: list_mode: 0 fetching_error_message: | wallabag can't retrieve contents for this article. Please troubleshoot this issue. + api_limit_mass_actions: 10 wallabag_user: registration_enabled: "%fosuser_registration%" diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index ae6f0e3f..7c3e778e 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -5,6 +5,7 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use Nelmio\ApiDocBundle\Annotation\ApiDoc; +use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -229,6 +230,8 @@ class EntryRestController extends WallabagRestController * ) * * @return JsonResponse + * + * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached */ public function postEntriesListAction(Request $request) { @@ -237,6 +240,12 @@ class EntryRestController extends WallabagRestController $urls = json_decode($request->query->get('urls', [])); $results = []; + $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); + + if (count($urls) > $limit) { + throw new Exception('API limit reached'); + } + // handle multiple urls if (!empty($urls)) { foreach ($urls as $key => $url) { diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php index 006a18c3..75b37729 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php @@ -47,6 +47,9 @@ class Configuration implements ConfigurationInterface ->scalarNode('list_mode') ->defaultValue(1) ->end() + ->scalarNode('api_limit_mass_actions') + ->defaultValue(10) + ->end() ->end() ; diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php index aa9ee339..c075c19f 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php @@ -26,6 +26,7 @@ class WallabagCoreExtension extends Extension $container->setParameter('wallabag_core.action_mark_as_read', $config['action_mark_as_read']); $container->setParameter('wallabag_core.list_mode', $config['list_mode']); $container->setParameter('wallabag_core.fetching_error_message', $config['fetching_error_message']); + $container->setParameter('wallabag_core.api_limit_mass_actions', $config['api_limit_mass_actions']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 88a5be93..8594ad0b 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -809,4 +809,27 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertFalse($content[1]['entry']); $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); } + + /** + * @expectedException Symfony\Component\Config\Definition\Exception\Exception + * @expectedExceptionMessage API limit reached + */ + public function testLimitBulkAction() + { + $list = [ + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + 'http://0.0.0.0/entry1', + ]; + + $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); + } } -- cgit v1.2.3 From 72db15ca5d7950a604f359056fc6a627f25e4ee4 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 5 May 2017 12:05:50 +0200 Subject: Little refacto and send 400 on reaching urls limit --- .../ApiBundle/Controller/EntryRestController.php | 196 ++++++++++----------- .../Controller/EntryRestControllerTest.php | 7 +- 2 files changed, 97 insertions(+), 106 deletions(-) diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7c3e778e..dbff6065 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -5,7 +5,7 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use Nelmio\ApiDocBundle\Annotation\ApiDoc; -use Symfony\Component\Config\Definition\Exception\Exception; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -45,9 +45,7 @@ class EntryRestController extends WallabagRestController $results[$url] = $res instanceof Entry ? $res->getId() : false; } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } // let's see if it is a simple url? @@ -63,9 +61,7 @@ class EntryRestController extends WallabagRestController $exists = $res instanceof Entry ? $res->getId() : false; - $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse(['exists' => $exists]); } /** @@ -125,9 +121,7 @@ class EntryRestController extends WallabagRestController ) ); - $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($paginatedCollection); } /** @@ -146,9 +140,7 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -189,35 +181,35 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $urls = json_decode($request->query->get('urls', [])); + + if (empty($urls)) { + return $this->sendResponse([]); + } + $results = []; // handle multiple urls - if (!empty($urls)) { - $results = []; - foreach ($urls as $key => $url) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $url, - $this->getUser()->getId() - ); - - $results[$key]['url'] = $url; + foreach ($urls as $key => $url) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); - if (false !== $entry) { - $em = $this->getDoctrine()->getManager(); - $em->remove($entry); - $em->flush(); + $results[$key]['url'] = $url; - // entry deleted, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - } + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); - $results[$key]['entry'] = $entry instanceof Entry ? true : false; + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); } - } - $json = $this->get('serializer')->serialize($results, 'json'); + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + } - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -231,7 +223,7 @@ class EntryRestController extends WallabagRestController * * @return JsonResponse * - * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached + * @throws HttpException When limit is reached */ public function postEntriesListAction(Request $request) { @@ -243,7 +235,7 @@ class EntryRestController extends WallabagRestController $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); if (count($urls) > $limit) { - throw new Exception('API limit reached'); + throw new HttpException(400, 'API limit reached'); } // handle multiple urls @@ -274,9 +266,7 @@ class EntryRestController extends WallabagRestController } } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -336,9 +326,7 @@ class EntryRestController extends WallabagRestController // 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); + return $this->sendResponse($entry); } /** @@ -387,9 +375,7 @@ class EntryRestController extends WallabagRestController $em = $this->getDoctrine()->getManager(); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -432,9 +418,7 @@ class EntryRestController extends WallabagRestController // 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); + return $this->sendResponse($entry); } /** @@ -460,9 +444,7 @@ class EntryRestController extends WallabagRestController // entry deleted, dispatch event about it! $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -481,9 +463,7 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry->getTags()); } /** @@ -514,9 +494,7 @@ class EntryRestController extends WallabagRestController $em->persist($entry); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -541,9 +519,7 @@ class EntryRestController extends WallabagRestController $em->persist($entry); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -562,45 +538,46 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $list = json_decode($request->query->get('list', [])); - $results = []; + + if (empty($list)) { + return $this->sendResponse([]); + } // handle multiple urls - if (!empty($list)) { - foreach ($list as $key => $element) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, - $this->getUser()->getId() - ); + $results = []; - $results[$key]['url'] = $element->url; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); - $tags = $element->tags; + $results[$key]['url'] = $element->url; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; - if (false !== $entry && !(empty($tags))) { - $tags = explode(',', $tags); - foreach ($tags as $label) { - $label = trim($label); + $tags = $element->tags; - $tag = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabel($label); + if (false !== $entry && !(empty($tags))) { + $tags = explode(',', $tags); + foreach ($tags as $label) { + $label = trim($label); - if (false !== $tag) { - $entry->removeTag($tag); - } - } + $tag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); + if (false !== $tag) { + $entry->removeTag($tag); + } } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); } } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -619,32 +596,47 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $list = json_decode($request->query->get('list', [])); + + if (empty($list)) { + return $this->sendResponse([]); + } + $results = []; // handle multiple urls - if (!empty($list)) { - foreach ($list as $key => $element) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, - $this->getUser()->getId() - ); + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); - $results[$key]['url'] = $element->url; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + $results[$key]['url'] = $element->url; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; - $tags = $element->tags; + $tags = $element->tags; - if (false !== $entry && !(empty($tags))) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + if (false !== $entry && !(empty($tags))) { + $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); - } + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); } } - $json = $this->get('serializer')->serialize($results, 'json'); + return $this->sendResponse($results); + } + + /** + * Shortcut to send data serialized in json. + * + * @param mixed $data + * + * @return JsonResponse + */ + private function sendResponse($data) + { + $json = $this->get('serializer')->serialize($data, 'json'); return (new JsonResponse())->setJson($json); } diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 8594ad0b..34a2f894 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -810,10 +810,6 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); } - /** - * @expectedException Symfony\Component\Config\Definition\Exception\Exception - * @expectedExceptionMessage API limit reached - */ public function testLimitBulkAction() { $list = [ @@ -831,5 +827,8 @@ class EntryRestControllerTest extends WallabagApiTestCase ]; $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); + + $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); + $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); } } -- cgit v1.2.3