diff --git a/app/bootstrap.php.d/60-api.php b/app/bootstrap.php.d/60-api.php index 4892f39..ba94758 100644 --- a/app/bootstrap.php.d/60-api.php +++ b/app/bootstrap.php.d/60-api.php @@ -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; }); diff --git a/app/config/routing.yml b/app/config/routing.yml index e3421cf..2deef13 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -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} diff --git a/app/console b/app/console index eef6670..dd14b19 100755 --- a/app/console +++ b/app/console @@ -2,6 +2,7 @@ add(new CreateCommand()); +$app['console']->add(new ListCommand()); $app['console']->add(new UpdateCommand()); $app['console']->add(new StatsCommand()); $app['console']->add(new UserCreateCommand()); diff --git a/src/Gist/Api/Client.php b/src/Gist/Api/Client.php index 830416f..548839f 100644 --- a/src/Gist/Api/Client.php +++ b/src/Gist/Api/Client.php @@ -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; + } } diff --git a/src/Gist/Command/ListCommand.php b/src/Gist/Command/ListCommand.php new file mode 100644 index 0000000..a0c99aa --- /dev/null +++ b/src/Gist/Command/ListCommand.php @@ -0,0 +1,59 @@ + + */ +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(); + } +} diff --git a/src/Gist/Controller/ApiController.php b/src/Gist/Controller/ApiController.php index 1d974dd..f5fe881 100644 --- a/src/Gist/Controller/ApiController.php +++ b/src/Gist/Controller/ApiController.php @@ -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(); }