API: update method

This commit is contained in:
Simon Vieille 2015-11-07 22:13:08 +01:00
parent d305c47e21
commit e722bd399f
10 changed files with 298 additions and 2 deletions

View File

@ -129,6 +129,34 @@ Params:
* Code ```405```: Method Not Allowed
* Code ```400```: Bad Request
### Update an existing Gist
**POST** /{locale}/api/update/{id}
Params:
* ```{id}```: Gist Id (required)
* ```form[content]```: String (required)
#### Responses:
* Code ```200```: A json which contains gist's information. Example:
```javascript
{
"url": "https:\/\/gist.deblan.org\/en\/view\/55abcfa7771e0\/abcgi72967dd95e3461490dcaa310d728d6adef",
"gist": {
"Id": 66,
"Title": "test prod",
"Cipher": false,
"Type": "javascript",
"File": "55abcfa7771e0",
"CreatedAt": "2015-07-19T16:26:15Z",
"UpdatedAt": "2015-07-19T16:30:15Z"
}
}
```
* Code ```405```: Method Not Allowed
* Code ```400```: Bad Request
Console
-------
@ -146,6 +174,7 @@ Arguments:
Options:
-t, --title=TITLE Title of the gist
-u, --show-url Display only the gist url
-i, --show-id Display only the gist Id
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
@ -168,6 +197,48 @@ Help:
Options:
--title, -t
Defines a title
--show-id, -i
Display only the Id of the gist
--show-url, -u
Display only the url of the gist
$ ./app/console --help create
Usage:
update [options] [--] <input>
Arguments:
input Input
Options:
--id=ID Gist Id
-u, --show-url Display only the gist url
-i, --show-id Display only the gist Id
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Provides a client to create a gist using the API.
Arguments:
input
Identify the source of the content: path of the file (eg: /path/to/file) or standard input (-)
type
Defines the type of code: html, css, javascript, php, sql, xml, yaml, perl, c, asp, python, bash, actionscript3, text
Default value: text
Options:
--id
Defines the Gist to update by using its ID
--show-id, -i
Display only the Id of the gist
--show-url, -u
Display only the url of the gist

View File

@ -3,5 +3,6 @@
use Gist\Api\Client;
$app['api_client'] = function ($app) {
return new Client(['base_uri' => 'https://gist.deblan.org/']);
// return new Client(['base_uri' => 'https://gist.deblan.org/']);
return new Client(['base_uri' => 'http://localhost:8080/']);
};

View File

@ -33,3 +33,7 @@ revisions:
api_create:
path: /api/create
defaults: {_controller: Gist\Controller\ApiController::createAction, _locale: en}
api_update:
path: /api/update/{id}
defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en}

View File

@ -2,9 +2,11 @@
<?php
use Gist\Command\CreateCommand;
use Gist\Command\UpdateCommand;
$app = require __DIR__.'/bootstrap.php';
$app['console']->add(new CreateCommand());
$app['console']->add(new UpdateCommand());
$app['console']->run();

View File

@ -11,6 +11,7 @@ use GuzzleHttp\Client as BaseClient;
class Client extends BaseClient
{
const CREATE = '/en/api/create';
const UPDATE = '/en/api/update/{id}';
public function create($title, $type, $content)
{
@ -33,4 +34,24 @@ class Client extends BaseClient
return [];
}
public function update($id, $content)
{
$response = $this->post(
str_replace('{id}', $id, self::UPDATE),
array(
'form_params' => array(
'form' => array(
'content' => $content,
),
),
)
);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
}

View File

@ -20,6 +20,7 @@ class CreateCommand extends Command
->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')
->setHelp(<<<EOF
Provides a client to create a gist using the API.
@ -34,6 +35,9 @@ 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
@ -84,6 +88,12 @@ EOF
return true;
}
if ($input->getOption('show-id')) {
$output->writeln($gist['gist']['Id']);
return true;
}
$output->writeln(json_encode($gist));
}

View File

@ -0,0 +1,114 @@
<?php
namespace Gist\Command;
use Knp\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class UpdateCommand extends Command
{
protected function configure()
{
$types = implode(', ', $this->getTypes());
$this
->setName('update')
->setDescription('Update a gist using the API')
->addArgument('input', InputArgument::REQUIRED, 'Input')
->addOption('id', null, InputOption::VALUE_REQUIRED, 'Gist Id')
->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url')
->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id')
->setHelp(<<<EOF
Provides a client to create a gist using the API.
Arguments:
<info>input</info>
Identify the source of the content: path of the file (eg: <comment>/path/to/file</comment>) or standard input (<comment>-</comment>)
<info>type</info>
Defines the type of code: {$types}
Default value: <comment>text</comment>
Options:
<info>--id</info>
Defines the Gist to update by using its ID
<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
EOF
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
$file = $input->getArgument('input');
$id = $input->getOption('id');
if ($file === '-') {
$content = file_get_contents('php://stdin');
} else {
if (!is_readable($file)) {
$output->writeln(sprintf('<error>%s: No such file.</error>', $file));
return false;
}
if (!is_file($file)) {
$output->writeln(sprintf('<error>"%s" must be a file.</error>', $file));
return false;
}
$content = file_get_contents($file);
}
if (trim($content) === '') {
$output->writeln(sprintf('<error>You can not create an empty gist.</error>', $type));
}
$gist = $this->getSilexApplication()['api_client']->update($id, $content);
if ($input->getOption('show-url')) {
$output->writeln($gist['url']);
return true;
}
if ($input->getOption('show-id')) {
$output->writeln($gist['gist']['Id']);
return true;
}
$output->writeln(json_encode($gist));
}
protected function getTypes()
{
$types = array(
'html',
'css',
'javascript',
'php',
'sql',
'xml',
'yaml',
'perl',
'c',
'asp',
'python',
'bash',
'actionscript3',
'text',
);
return $types;
}
}

View File

@ -7,6 +7,8 @@ use Symfony\Component\HttpFoundation\Request;
use Gist\Model\Gist;
use Symfony\Component\HttpFoundation\JsonResponse;
use Gist\Form\ApiCreateGistForm;
use Gist\Model\GistQuery;
use Gist\Form\ApiUpdateGistForm;
/**
* Class ApiController
@ -51,6 +53,56 @@ class ApiController extends Controller
return $this->invalidRequestResponse('Invalid field(s)');
}
public function updateAction(Request $request, Application $app, $id)
{
if (false === $request->isMethod('post')) {
return $this->invalidMethodResponse('POST method is required.');
}
if (!ctype_digit($id)) {
return $this->invalidRequestResponse('Invalid Gist');
}
$gist = GistQuery::create()
->filterByCipher(false)
->filterById((int) $id)
->findOne();
if (!$gist) {
return $this->invalidRequestResponse('Invalid Gist');
}
$form = new ApiUpdateGistForm(
$app['form.factory'],
$app['translator'],
[],
['csrf_protection' => false]
);
$form = $form->build()->getForm();
$form->submit($request);
if ($form->isValid()) {
$gist = $app['gist']->commit($gist, $form->getData());
$history = $app['gist']->getHistory($gist);
return new JsonResponse(array(
'url' => $request->getSchemeAndHttpHost().$app['url_generator']->generate(
'view',
array(
'gist' => $gist->getFile(),
'commit' => array_pop($history)['commit'],
)
),
'gist' => $gist->toArray(),
));
}
return $this->invalidRequestResponse('Invalid field(s)');
}
protected function invalidMethodResponse($message = null)
{

View File

@ -3,7 +3,7 @@
namespace Gist\Form;
/**
* Class CreateGistForm
* Class ApiCreateGistForm
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiCreateGistForm extends CreateGistForm

View File

@ -0,0 +1,21 @@
<?php
namespace Gist\Form;
/**
* Class ApiUpdateGistForm
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiUpdateGistForm extends ApiCreateGistForm
{
public function build(array $options = array())
{
parent::build($options);
$this->builder
->remove('title')
->remove('type');
return $this->builder;
}
}