API client

This commit is contained in:
Simon Vieille 2015-07-19 18:19:43 +02:00
parent 0d0403e242
commit 5c4252c6ab
5 changed files with 161 additions and 1 deletions

View File

@ -0,0 +1,7 @@
<?php
use Gist\Api\Client;
$app['api_client'] = function ($app) {
return new Client(['base_uri' => 'http://127.0.0.1:8080/']);
};

View File

@ -1,6 +1,10 @@
#!/usr/bin/env php
<?php
use Gist\Command\CreateCommand;
$app = require __DIR__.'/bootstrap.php';
$app['console']->add(new CreateCommand());
$app['console']->run();

View File

@ -9,7 +9,8 @@
"symfony/form": "~2.6",
"symfony/security-csrf": "~2.6",
"knplabs/console-service-provider": "~1.0",
"propel/propel": "~2.0@dev"
"propel/propel": "~2.0@dev",
"guzzlehttp/guzzle": "~6.0"
},
"autoload": {
"psr-0": {

36
src/Gist/Api/Client.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace Gist\Api;
use GuzzleHttp\Client as BaseClient;
/**
* Class Client
* @author Simon Vieille <simon@deblan.fr>
*/
class Client extends BaseClient
{
const CREATE = '/en/api/create';
public function create($title, $type, $content)
{
$response = $this->post(
self::CREATE,
array(
'form_params' => array(
'form' => array(
'title' => $title,
'type' => $type,
'content' => $content,
),
),
)
);
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return false;
}
}

View File

@ -0,0 +1,112 @@
<?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 CreateCommand extends Command
{
protected function configure()
{
$types = implode(', ', $this->getTypes());
$this
->setName('create')
->setDescription('Create a gist using the API')
->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')
->setHelp(<<<EOF
The <info>%command.name%</info> provides a client to create a gist using an API.
Arguments:
<info>input</info>
Identify the source of the content: a file path (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>--title</info>, <info>-t</info>
Defines a title
<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');
$type = $input->getArgument('type');
$title = $input->getOption('title');
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 (!in_array($type, $this->getTypes())) {
$output->writeln(sprintf('<error>%s: invalid type.</error>', $type));
return false;
}
if (trim($content) === '') {
$output->writeln(sprintf('<error>You can not create an empty gist.</error>', $type));
}
$gist = $this->getSilexApplication()['api_client']->create($title, $type, $content);
if ($input->getOption('show-url')) {
$output->writeln($gist['url']);
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;
}
}