API refactoring

This commit is contained in:
Simon Vieille 2017-06-25 18:52:27 +02:00
parent 79ba41a983
commit a9435906fe
6 changed files with 78 additions and 41 deletions

View file

@ -19,7 +19,7 @@ class Client extends BaseClient
const CREATE = '/en/api/create'; const CREATE = '/en/api/create';
/** /**
* URI of updating * URI of update
* *
* @const string * @const string
*/ */

View file

@ -7,6 +7,8 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Propel\Runtime\Parser\YamlParser;
use Symfony\Component\Yaml\Yaml;
/** /**
* class CreateCommand. * class CreateCommand.
@ -27,8 +29,9 @@ class CreateCommand extends Command
->addArgument('input', InputArgument::REQUIRED, 'Input') ->addArgument('input', InputArgument::REQUIRED, 'Input')
->addArgument('type', InputArgument::OPTIONAL, 'Type', 'text') ->addArgument('type', InputArgument::OPTIONAL, 'Type', 'text')
->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Title of the gist') ->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Title of the gist')
->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url') ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response')
->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id') ->addOption('id', 'i', InputOption::VALUE_NONE, 'Display only the id of the gist')
->addOption('json', 'j', InputOption::VALUE_NONE, 'Format the response to json')
->setHelp(<<<EOF ->setHelp(<<<EOF
Provides a client to create a gist using the API. Provides a client to create a gist using the API.
@ -43,12 +46,15 @@ Arguments:
Options: Options:
<info>--title</info>, <info>-t</info> <info>--title</info>, <info>-t</info>
Defines a title Defines a title
<info>--show-id</info>, <info>-i</info>
Display only the Id of the gist
<info>--show-url</info>, <info>-u</info> <info>--id</info>, <info>-i</info>
Display only the url of the gist Display only the id of the gist
<info>--all</info>, <info>-a</info>
Display all the response
<info>--json</info>, <info>-j</info>
Format the response to json
EOF EOF
); );
} }
@ -58,11 +64,10 @@ EOF
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
$file = $input->getArgument('input'); $file = $input->getArgument('input');
$type = $input->getArgument('type'); $type = $input->getArgument('type');
$title = $input->getOption('title'); $title = $input->getOption('title');
$json = $input->getOption('json');
if ($file === '-') { if ($file === '-') {
$content = file_get_contents('php://stdin'); $content = file_get_contents('php://stdin');
@ -94,19 +99,17 @@ EOF
$gist = $this->getSilexApplication()['api_client']->create($title, $type, $content); $gist = $this->getSilexApplication()['api_client']->create($title, $type, $content);
if ($input->getOption('show-url')) { if ($input->getOption('id')) {
$output->writeln($gist['url']); $id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id'];
$result = $json ? json_encode(array('id' => $id)) : $id;
return true; } elseif ($input->getOption('all')) {
$result = $json ? json_encode($gist) : Yaml::dump($gist);
} else {
$url = $gist['url'];
$result = $json ? json_encode(array('url' => $url)) : $url;
} }
if ($input->getOption('show-id')) { $output->writeln($result);
$output->writeln($gist['gist']['Id']);
return true;
}
$output->writeln(json_encode($gist));
} }
/** /**

View file

@ -7,6 +7,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
/** /**
* class UpdateCommand. * class UpdateCommand.
@ -26,8 +27,9 @@ class UpdateCommand extends Command
->setDescription('Update a gist using the API') ->setDescription('Update a gist using the API')
->addArgument('input', InputArgument::REQUIRED, 'Input') ->addArgument('input', InputArgument::REQUIRED, 'Input')
->addOption('gist', null, InputOption::VALUE_REQUIRED, 'Id or File of the gist') ->addOption('gist', null, InputOption::VALUE_REQUIRED, 'Id or File of the gist')
->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url') ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response')
->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id') ->addOption('id', 'i', InputOption::VALUE_NONE, 'Display only the id of the gist')
->addOption('json', 'j', InputOption::VALUE_NONE, 'Format the response to json')
->setHelp(<<<EOF ->setHelp(<<<EOF
Provides a client to create a gist using the API. Provides a client to create a gist using the API.
@ -43,11 +45,14 @@ Options:
<info>--gist</info> <info>--gist</info>
Defines the Gist to update by using its Id or its File Defines the Gist to update by using its Id or its File
<info>--show-id</info>, <info>-i</info> <info>--id</info>, <info>-i</info>
Display only the Id of the gist Display only the id of the gist
<info>--show-url</info>, <info>-u</info> <info>--all</info>, <info>-a</info>
Display only the url of the gist Display all the response
<info>--json</info>, <info>-j</info>
Format the response to json
EOF EOF
); );
} }
@ -57,10 +62,9 @@ EOF
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
$file = $input->getArgument('input'); $file = $input->getArgument('input');
$gist = $input->getOption('gist'); $gist = $input->getOption('gist');
$json = $input->getOption('json');
if ($file === '-') { if ($file === '-') {
$content = file_get_contents('php://stdin'); $content = file_get_contents('php://stdin');
@ -86,19 +90,17 @@ EOF
$gist = $this->getSilexApplication()['api_client']->update($gist, $content); $gist = $this->getSilexApplication()['api_client']->update($gist, $content);
if ($input->getOption('show-url')) { if ($input->getOption('id')) {
$output->writeln($gist['url']); $id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id'];
$result = $json ? json_encode(array('id' => $id)) : $id;
return true; } elseif ($input->getOption('all')) {
$result = $json ? json_encode($gist) : Yaml::dump($gist);
} else {
$url = $gist['url'];
$result = $json ? json_encode(array('url' => $url)) : $url;
} }
if ($input->getOption('show-id')) { $output->writeln($result);
$output->writeln($gist['gist']['Id']);
return true;
}
$output->writeln(json_encode($gist));
} }
/** /**

View file

@ -16,6 +16,13 @@ use Gist\Form\ApiUpdateGistForm;
*/ */
class ApiController extends Controller class ApiController extends Controller
{ {
/**
* Creates a gist.
*
* @param Request $request
*
* @return JsonResponse
*/
public function createAction(Request $request) public function createAction(Request $request)
{ {
$app = $this->getApp(); $app = $this->getApp();
@ -56,6 +63,14 @@ class ApiController extends Controller
return $this->invalidRequestResponse('Invalid field(s)'); return $this->invalidRequestResponse('Invalid field(s)');
} }
/**
* Updates a gist.
*
* @param Request $request
* @param string $gist
*
* @return JsonResponse
*/
public function updateAction(Request $request, $gist) public function updateAction(Request $request, $gist)
{ {
$app = $this->getApp(); $app = $this->getApp();
@ -106,6 +121,13 @@ class ApiController extends Controller
return $this->invalidRequestResponse('Invalid field(s)'); return $this->invalidRequestResponse('Invalid field(s)');
} }
/**
* Builds an invalid method response.
*
* @param mixed $message
*
* @return JsonResponse
*/
protected function invalidMethodResponse($message = null) protected function invalidMethodResponse($message = null)
{ {
$data = [ $data = [
@ -116,6 +138,13 @@ class ApiController extends Controller
return new JsonResponse($data, 405); return new JsonResponse($data, 405);
} }
/**
* Builds an invalid request response.
*
* @param mixed $message
*
* @return JsonResponse
*/
protected function invalidRequestResponse($message = null) protected function invalidRequestResponse($message = null)
{ {
$data = [ $data = [

View file

@ -16,7 +16,9 @@ class ApiCreateGistForm extends CreateGistForm
{ {
parent::build($options); parent::build($options);
$this->builder->remove('cipher'); $this->builder
->remove('cipher')
->remove('file');
return $this->builder; return $this->builder;
} }

View file

@ -18,6 +18,7 @@ class ApiUpdateGistForm extends ApiCreateGistForm
$this->builder $this->builder
->remove('title') ->remove('title')
->remove('file')
->remove('type'); ->remove('type');
return $this->builder; return $this->builder;