Implementation of Api action "delete"

This commit is contained in:
Simon Vieille 2017-08-23 19:32:56 +02:00
parent f6e4837776
commit 87348c8335
5 changed files with 124 additions and 24 deletions

View file

@ -6,7 +6,7 @@ $app['api_client'] = $app->share(function ($app) {
$client = 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'])) { if (!empty($app['settings']['api']['client']['api_key'])) {
$client->setApiToken($app['settings']['api']['client']['api_key']); $client->setApiKey($app['settings']['api']['client']['api_key']);
} }
return $client; return $client;

View file

@ -68,3 +68,7 @@ api_create:
api_update: api_update:
path: /api/update/{gist}/{apiKey} path: /api/update/{gist}/{apiKey}
defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en, apiKey: null} defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en, apiKey: null}
api_delete:
path: /api/delete/{gist}/{apiKey}
defaults: {_controller: Gist\Controller\ApiController::deleteAction, _locale: en, apiKey: null}

View file

@ -25,6 +25,13 @@ class Client extends BaseClient
*/ */
const UPDATE = '/en/api/update/{gist}'; const UPDATE = '/en/api/update/{gist}';
/**
* URI of delete.
*
* @const string
*/
const DELETE = '/en/api/delete/{gist}';
/** /**
* URI of list. * URI of list.
* *
@ -33,17 +40,17 @@ class Client extends BaseClient
const LIST = '/en/api/list'; const LIST = '/en/api/list';
/** /**
* The API token. * The API key.
* *
* @var string|null * @var string|null
*/ */
protected $apiToken; protected $apiKey;
/** /**
* Creates a gist. * Creates a gist.
* *
* @param string $title The title * @param string $title The title
* @param string $type The type * @param string $type The type
* @param string $content The content * @param string $content The content
* *
* @return array * @return array
@ -51,7 +58,7 @@ class Client extends BaseClient
public function create($title, $type, $content) public function create($title, $type, $content)
{ {
$response = $this->post( $response = $this->post(
$this->mergeToken(self::CREATE), $this->mergeApiKey(self::CREATE),
array( array(
'form_params' => array( 'form_params' => array(
'form' => array( 'form' => array(
@ -71,9 +78,9 @@ class Client extends BaseClient
} }
/** /**
* Clones and update a gist * Clones and update a gist.
* *
* @param string $gist Gist's ID * @param string $gist Gist's ID
* @param string $content The content * @param string $content The content
* *
* @return array * @return array
@ -81,7 +88,7 @@ class Client extends BaseClient
public function update($gist, $content) public function update($gist, $content)
{ {
$response = $this->post( $response = $this->post(
str_replace('{gist}', $gist, $this->mergeToken(self::LIST)), str_replace('{gist}', $gist, $this->mergeApiKey(self::LIST)),
array( array(
'form_params' => array( 'form_params' => array(
'form' => array( 'form' => array(
@ -99,16 +106,34 @@ class Client extends BaseClient
} }
/** /**
* Lists the user's gists. * Deletes a gist.
* *
* @param string $gist Gist's ID * @param string $gist Gist's ID
*
* @return array
*/
public function delete($gist)
{
$response = $this->post(str_replace('{gist}', $gist, $this->mergeApiKey(self::DELETE)));
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
}
return [];
}
/**
* Lists the user's gists.
*
* @param string $gist Gist's ID
* @param string $content The content * @param string $content The content
* *
* @return array * @return array
*/ */
public function list() public function list()
{ {
$response = $this->get($this->mergeToken(self::LIST)); $response = $this->get($this->mergeApiKey(self::LIST));
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true); return json_decode($response->getBody()->getContents(), true);
@ -118,42 +143,42 @@ class Client extends BaseClient
} }
/* /*
* Merges the API token with the given url.. * Merges the API key with the given url.
* *
* @param string $url * @param string $url
* *
* @return string * @return string
*/ */
public function mergeToken($url) public function mergeApiKey($url)
{ {
if (empty($this->apiToken)) { if (empty($this->apiKey)) {
return $url; return $url;
} }
return rtrim($url, '/').'/'.$this->apiToken; return rtrim($url, '/').'/'.$this->apiKey;
} }
/* /*
* Set the value of "apiToken". * Set the value of "apiKey".
* *
* @param string|null $apiToken * @param string|null $apiKey
* *
* @return Client * @return Client
*/ */
public function setApiToken($apiToken) public function setApiKey($apiKey)
{ {
$this->apiToken = $apiToken; $this->apiKey = $apiKey;
return $this; return $this;
} }
/* /*
* Get the value of "apiToken". * Get the value of "apiKey".
* *
* @return string|null * @return string|null
*/ */
public function getApiToken() public function getApiKey()
{ {
return $this->apiToken; return $this->apiKey;
} }
} }

View file

@ -42,7 +42,8 @@ class ApiController extends Controller
return $this->invalidMethodResponse('GET method is required.'); return $this->invalidMethodResponse('GET method is required.');
} }
$gists = GistQuery::create()->find(); $user = $app['user.provider']->loadUserByApiKey($apiKey);
$gists = $user->getGists();
$data = array(); $data = array();
foreach ($gists as $gist) { foreach ($gists as $gist) {
@ -102,8 +103,12 @@ class ApiController extends Controller
$form->submit($request); $form->submit($request);
if ($form->isValid()) { if ($form->isValid()) {
$user = !empty($apiKey) ? $app['user.provider']->loadUserByApiKey($apiKey) : null;
$gist = $app['gist']->create(new Gist(), $form->getData()); $gist = $app['gist']->create(new Gist(), $form->getData());
$gist->setCipher(false)->save(); $gist
->setCipher(false)
->setUser($user)
->save();
$history = $app['gist']->getHistory($gist); $history = $app['gist']->getHistory($gist);
@ -189,6 +194,49 @@ class ApiController extends Controller
return $this->invalidRequestResponse('Invalid field(s)'); return $this->invalidRequestResponse('Invalid field(s)');
} }
/**
* Deletes a gist.
*
* @param Request $request
* @param string $gist
* @param string $apiKey
*
* @return JsonResponse
*/
public function deleteAction(Request $request, $gist, $apiKey)
{
$app = $this->getApp();
if (false === $app['settings']['api']['enabled']) {
return new Response('', 403);
}
if (false === $this->isValidApiKey($apiKey, true)) {
return $this->invalidApiKeyResponse();
}
if (false === $request->isMethod('post')) {
// return $this->invalidMethodResponse('POST method is required.');
}
$user = $app['user.provider']->loadUserByApiKey($apiKey);
$gist = GistQuery::create()
->filterById((int) $gist)
->_or()
->filterByFile($gist)
->filterByUser($user)
->findOne();
if (!$gist) {
return $this->invalidRequestResponse('Invalid Gist');
}
$gist->delete();
return new JsonResponse(['error' => false]);
}
/** /**
* Builds an invalid api key response. * Builds an invalid api key response.
* *
@ -240,6 +288,15 @@ class ApiController extends Controller
return new JsonResponse($data, 400); return new JsonResponse($data, 400);
} }
/**
* Checks if the given api key is valid
* depending of the requirement.
*
* @param mixed $apiKey
* @param mixed $required
*
* @return bool
*/
protected function isValidApiKey($apiKey, $required = false) protected function isValidApiKey($apiKey, $required = false)
{ {
if (empty($apiKey)) { if (empty($apiKey)) {

View file

@ -167,6 +167,20 @@ class UserProvider implements UserProviderInterface
return $user; return $user;
} }
/**
* Loads a user by his api key.
*
* @param string $apiKey
*
* @return User
*/
public function loadUserByApiKey($apiKey)
{
$user = UserQuery::create()->findOneByApiKey($apiKey);
return $user;
}
/* /*
* Checks if the given password is the current user password. * Checks if the given password is the current user password.
* *