API refactoring
This commit is contained in:
parent
79ba41a983
commit
a9435906fe
|
@ -19,7 +19,7 @@ class Client extends BaseClient
|
|||
const CREATE = '/en/api/create';
|
||||
|
||||
/**
|
||||
* URI of updating
|
||||
* URI of update
|
||||
*
|
||||
* @const string
|
||||
*/
|
||||
|
|
|
@ -7,6 +7,8 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Propel\Runtime\Parser\YamlParser;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* class CreateCommand.
|
||||
|
@ -27,8 +29,9 @@ class CreateCommand extends Command
|
|||
->addArgument('input', InputArgument::REQUIRED, 'Input')
|
||||
->addArgument('type', InputArgument::OPTIONAL, 'Type', 'text')
|
||||
->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Title of the gist')
|
||||
->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url')
|
||||
->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id')
|
||||
->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response')
|
||||
->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
|
||||
Provides a client to create a gist using the API.
|
||||
|
||||
|
@ -43,12 +46,15 @@ Arguments:
|
|||
Options:
|
||||
<info>--title</info>, <info>-t</info>
|
||||
Defines a title
|
||||
|
||||
<info>--show-id</info>, <info>-i</info>
|
||||
Display only the Id of the gist
|
||||
|
||||
<info>--show-url</info>, <info>-u</info>
|
||||
Display only the url of the gist
|
||||
<info>--id</info>, <info>-i</info>
|
||||
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
|
||||
);
|
||||
}
|
||||
|
@ -58,11 +64,10 @@ EOF
|
|||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
|
||||
|
||||
$file = $input->getArgument('input');
|
||||
$type = $input->getArgument('type');
|
||||
$title = $input->getOption('title');
|
||||
$json = $input->getOption('json');
|
||||
|
||||
if ($file === '-') {
|
||||
$content = file_get_contents('php://stdin');
|
||||
|
@ -94,19 +99,17 @@ EOF
|
|||
|
||||
$gist = $this->getSilexApplication()['api_client']->create($title, $type, $content);
|
||||
|
||||
if ($input->getOption('show-url')) {
|
||||
$output->writeln($gist['url']);
|
||||
|
||||
return true;
|
||||
if ($input->getOption('id')) {
|
||||
$id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id'];
|
||||
$result = $json ? json_encode(array('id' => $id)) : $id;
|
||||
} 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($gist['gist']['Id']);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$output->writeln(json_encode($gist));
|
||||
$output->writeln($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,6 +7,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* class UpdateCommand.
|
||||
|
@ -26,8 +27,9 @@ class UpdateCommand extends Command
|
|||
->setDescription('Update a gist using the API')
|
||||
->addArgument('input', InputArgument::REQUIRED, 'Input')
|
||||
->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('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id')
|
||||
->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response')
|
||||
->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
|
||||
Provides a client to create a gist using the API.
|
||||
|
||||
|
@ -43,11 +45,14 @@ Options:
|
|||
<info>--gist</info>
|
||||
Defines the Gist to update by using its Id or its File
|
||||
|
||||
<info>--show-id</info>, <info>-i</info>
|
||||
Display only the Id of the gist
|
||||
<info>--id</info>, <info>-i</info>
|
||||
Display only the id of the gist
|
||||
|
||||
<info>--show-url</info>, <info>-u</info>
|
||||
Display only the url 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
|
||||
);
|
||||
}
|
||||
|
@ -57,10 +62,9 @@ EOF
|
|||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
|
||||
|
||||
$file = $input->getArgument('input');
|
||||
$gist = $input->getOption('gist');
|
||||
$json = $input->getOption('json');
|
||||
|
||||
if ($file === '-') {
|
||||
$content = file_get_contents('php://stdin');
|
||||
|
@ -86,19 +90,17 @@ EOF
|
|||
|
||||
$gist = $this->getSilexApplication()['api_client']->update($gist, $content);
|
||||
|
||||
if ($input->getOption('show-url')) {
|
||||
$output->writeln($gist['url']);
|
||||
|
||||
return true;
|
||||
if ($input->getOption('id')) {
|
||||
$id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id'];
|
||||
$result = $json ? json_encode(array('id' => $id)) : $id;
|
||||
} 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($gist['gist']['Id']);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$output->writeln(json_encode($gist));
|
||||
$output->writeln($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,13 @@ use Gist\Form\ApiUpdateGistForm;
|
|||
*/
|
||||
class ApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* Creates a gist.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
{
|
||||
$app = $this->getApp();
|
||||
|
@ -56,6 +63,14 @@ class ApiController extends Controller
|
|||
return $this->invalidRequestResponse('Invalid field(s)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a gist.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $gist
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function updateAction(Request $request, $gist)
|
||||
{
|
||||
$app = $this->getApp();
|
||||
|
@ -106,6 +121,13 @@ class ApiController extends Controller
|
|||
return $this->invalidRequestResponse('Invalid field(s)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an invalid method response.
|
||||
*
|
||||
* @param mixed $message
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
protected function invalidMethodResponse($message = null)
|
||||
{
|
||||
$data = [
|
||||
|
@ -116,6 +138,13 @@ class ApiController extends Controller
|
|||
return new JsonResponse($data, 405);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an invalid request response.
|
||||
*
|
||||
* @param mixed $message
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
protected function invalidRequestResponse($message = null)
|
||||
{
|
||||
$data = [
|
||||
|
|
|
@ -16,7 +16,9 @@ class ApiCreateGistForm extends CreateGistForm
|
|||
{
|
||||
parent::build($options);
|
||||
|
||||
$this->builder->remove('cipher');
|
||||
$this->builder
|
||||
->remove('cipher')
|
||||
->remove('file');
|
||||
|
||||
return $this->builder;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ class ApiUpdateGistForm extends ApiCreateGistForm
|
|||
|
||||
$this->builder
|
||||
->remove('title')
|
||||
->remove('file')
|
||||
->remove('type');
|
||||
|
||||
return $this->builder;
|
||||
|
|
Loading…
Reference in a new issue