Implementation of Api action "list"
parent
ffe3540565
commit
f6e4837776
|
@ -3,5 +3,11 @@
|
|||
use Gist\Api\Client;
|
||||
|
||||
$app['api_client'] = $app->share(function ($app) {
|
||||
return new Client(['base_uri' => rtrim($app['settings']['api']['base_url'], '/')]);
|
||||
$client = new Client(['base_uri' => rtrim($app['settings']['api']['base_url'], '/')]);
|
||||
|
||||
if (!empty($app['settings']['api']['client']['api_key'])) {
|
||||
$client->setApiToken($app['settings']['api']['client']['api_key']);
|
||||
}
|
||||
|
||||
return $client;
|
||||
});
|
||||
|
|
|
@ -59,12 +59,12 @@ revisions:
|
|||
|
||||
api_list:
|
||||
path: /api/list/{apiKey}
|
||||
defaults: {_controller: Gist\Controller\ApiController::listAction, _locale: en}
|
||||
defaults: {_controller: Gist\Controller\ApiController::listAction, _locale: en, apiKey: null}
|
||||
|
||||
api_create:
|
||||
path: /api/create/{apiKey}
|
||||
defaults: {_controller: Gist\Controller\ApiController::createAction, _locale: en}
|
||||
defaults: {_controller: Gist\Controller\ApiController::createAction, _locale: en, apiKey: null}
|
||||
|
||||
api_update:
|
||||
path: /api/update/{gist}/{apiKey}
|
||||
defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en}
|
||||
defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en, apiKey: null}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<?php
|
||||
|
||||
use Gist\Command\CreateCommand;
|
||||
use Gist\Command\ListCommand;
|
||||
use Gist\Command\UpdateCommand;
|
||||
use Gist\Command\StatsCommand;
|
||||
use Gist\Command\UserCreateCommand;
|
||||
|
@ -10,6 +11,7 @@ use Gist\Command\Migration\UpgradeTo1p4p1Command;
|
|||
$app = require __DIR__.'/bootstrap.php';
|
||||
|
||||
$app['console']->add(new CreateCommand());
|
||||
$app['console']->add(new ListCommand());
|
||||
$app['console']->add(new UpdateCommand());
|
||||
$app['console']->add(new StatsCommand());
|
||||
$app['console']->add(new UserCreateCommand());
|
||||
|
|
|
@ -25,6 +25,20 @@ class Client extends BaseClient
|
|||
*/
|
||||
const UPDATE = '/en/api/update/{gist}';
|
||||
|
||||
/**
|
||||
* URI of list.
|
||||
*
|
||||
* @const string
|
||||
*/
|
||||
const LIST = '/en/api/list';
|
||||
|
||||
/**
|
||||
* The API token.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $apiToken;
|
||||
|
||||
/**
|
||||
* Creates a gist.
|
||||
*
|
||||
|
@ -37,7 +51,7 @@ class Client extends BaseClient
|
|||
public function create($title, $type, $content)
|
||||
{
|
||||
$response = $this->post(
|
||||
self::CREATE,
|
||||
$this->mergeToken(self::CREATE),
|
||||
array(
|
||||
'form_params' => array(
|
||||
'form' => array(
|
||||
|
@ -67,7 +81,7 @@ class Client extends BaseClient
|
|||
public function update($gist, $content)
|
||||
{
|
||||
$response = $this->post(
|
||||
str_replace('{gist}', $gist, self::UPDATE),
|
||||
str_replace('{gist}', $gist, $this->mergeToken(self::LIST)),
|
||||
array(
|
||||
'form_params' => array(
|
||||
'form' => array(
|
||||
|
@ -83,4 +97,63 @@ class Client extends BaseClient
|
|||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists the user's gists.
|
||||
*
|
||||
* @param string $gist Gist's ID
|
||||
* @param string $content The content
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
$response = $this->get($this->mergeToken(self::LIST));
|
||||
|
||||
if ($response->getStatusCode() === 200) {
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/*
|
||||
* Merges the API token with the given url..
|
||||
*
|
||||
* @param string $url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function mergeToken($url)
|
||||
{
|
||||
if (empty($this->apiToken)) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return rtrim($url, '/').'/'.$this->apiToken;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the value of "apiToken".
|
||||
*
|
||||
* @param string|null $apiToken
|
||||
*
|
||||
* @return Client
|
||||
*/
|
||||
public function setApiToken($apiToken)
|
||||
{
|
||||
$this->apiToken = $apiToken;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the value of "apiToken".
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getApiToken()
|
||||
{
|
||||
return $this->apiToken;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
<?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;
|
||||
use Propel\Runtime\Parser\YamlParser;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* class ListCommand.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class ListCommand extends Command
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('list')
|
||||
->setDescription('Listing gists using the API');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$gists = $this->getSilexApplication()['api_client']->list();
|
||||
$rows = [];
|
||||
|
||||
foreach ($gists as $gist) {
|
||||
$rows[] = array(
|
||||
$gist['id'],
|
||||
$gist['title'],
|
||||
$gist['cipher'] ? 'y' : 'n',
|
||||
$gist['type'],
|
||||
(new DateTime($gist['createdAt']))->format('Y-m-d H:i:s'),
|
||||
(new DateTime($gist['updatedAt']))->format('Y-m-d H:i:s'),
|
||||
$gist['url'],
|
||||
);
|
||||
}
|
||||
|
||||
$table = new Table($output);
|
||||
$table
|
||||
->setHeaders(array('ID', 'Title', 'Cipher', 'Type', 'Created At', 'Updated At', 'url'))
|
||||
->setRows($rows);
|
||||
|
||||
$table->render();
|
||||
}
|
||||
}
|
|
@ -82,7 +82,7 @@ class ApiController extends Controller
|
|||
return new Response('', 403);
|
||||
}
|
||||
|
||||
if (false === $this->isValidApiKey($apiKey)) {
|
||||
if (false === $this->isValidApiKey($apiKey, (bool) $app['settings']['api']['api_key_required'])) {
|
||||
return $this->invalidApiKeyResponse();
|
||||
}
|
||||
|
||||
|
@ -139,7 +139,7 @@ class ApiController extends Controller
|
|||
return new Response('', 403);
|
||||
}
|
||||
|
||||
if (false === $this->isValidApiKey($apiKey)) {
|
||||
if (false === $this->isValidApiKey($apiKey, (bool) $app['settings']['api']['api_key_required'])) {
|
||||
return $this->invalidApiKeyResponse();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue