Merge pull request #5673 from wallabag/api-config-endpoint

Add new endpoint for API: config
This commit is contained in:
Kevin Decherf 2022-05-13 00:50:32 +02:00 committed by GitHub
commit 3818cfe15f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,32 @@
<?php
namespace Wallabag\ApiBundle\Controller;
use JMS\Serializer\SerializationContext;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\JsonResponse;
class ConfigRestController extends WallabagRestController
{
/**
* Retrieve configuration for current user.
*
* @ApiDoc()
*
* @return JsonResponse
*/
public function getConfigAction()
{
$this->validateAuthentication();
$json = $this->get('jms_serializer')->serialize(
$this->getUser()->getConfig(),
'json',
SerializationContext::create()->setGroups(['config_api'])
);
return (new JsonResponse())
->setJson($json)
->setStatusCode(JsonResponse::HTTP_OK);
}
}

View file

@ -32,3 +32,8 @@ user:
type: rest
resource: "WallabagApiBundle:UserRest"
name_prefix: api_
config:
type: rest
resource: "WallabagApiBundle:ConfigRest"
name_prefix: api_

View file

@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Wallabag\UserBundle\Entity\User;
@ -29,6 +30,8 @@ class Config
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Groups({"config_api"})
*/
private $id;
@ -50,6 +53,8 @@ class Config
* maxMessage = "validator.item_per_page_too_high"
* )
* @ORM\Column(name="items_per_page", type="integer", nullable=false)
*
* @Groups({"config_api"})
*/
private $itemsPerPage;
@ -58,6 +63,8 @@ class Config
*
* @Assert\NotBlank()
* @ORM\Column(name="language", type="string", nullable=false)
*
* @Groups({"config_api"})
*/
private $language;
@ -65,6 +72,8 @@ class Config
* @var string
*
* @ORM\Column(name="feed_token", type="string", nullable=true)
*
* @Groups({"config_api"})
*/
private $feedToken;
@ -77,6 +86,8 @@ class Config
* max = 100000,
* maxMessage = "validator.feed_limit_too_high"
* )
*
* @Groups({"config_api"})
*/
private $feedLimit;
@ -84,6 +95,8 @@ class Config
* @var float
*
* @ORM\Column(name="reading_speed", type="float", nullable=true)
*
* @Groups({"config_api"})
*/
private $readingSpeed;
@ -98,6 +111,8 @@ class Config
* @var int
*
* @ORM\Column(name="action_mark_as_read", type="integer", nullable=true, options={"default" = 0})
*
* @Groups({"config_api"})
*/
private $actionMarkAsRead;
@ -105,6 +120,8 @@ class Config
* @var int
*
* @ORM\Column(name="list_mode", type="integer", nullable=true)
*
* @Groups({"config_api"})
*/
private $listMode;

View file

@ -0,0 +1,46 @@
<?php
namespace Tests\Wallabag\ApiBundle\Controller;
use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
class ConfigRestControllerTest extends WallabagApiTestCase
{
public function testGetConfig()
{
$this->client->request('GET', '/api/config.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$config = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $config);
$this->assertArrayHasKey('items_per_page', $config);
$this->assertArrayHasKey('language', $config);
$this->assertArrayHasKey('reading_speed', $config);
$this->assertArrayHasKey('action_mark_as_read', $config);
$this->assertArrayHasKey('list_mode', $config);
$this->assertSame(200.0, $config['reading_speed']);
$this->assertSame('en', $config['language']);
$this->assertCount(6, $config);
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
public function testGetConfigWithoutAuthentication()
{
$client = static::createClient();
$client->request('GET', '/api/config.json');
$this->assertSame(401, $client->getResponse()->getStatusCode());
$config = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $config);
$this->assertArrayHasKey('error_description', $config);
$this->assertSame('access_denied', $config['error']);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
}
}