API improves

This commit is contained in:
Diego 2017-07-19 22:50:08 +02:00
parent 48a4077ebb
commit b7bb0449b9
1 changed files with 47 additions and 62 deletions

View File

@ -55,26 +55,20 @@ class pluginAPI extends Plugin {
// ------------------------------------------------------------
$method = $this->getMethod();
// INPUTS
// METHOD INPUTS
// ------------------------------------------------------------
$inputs = $this->getInputs();
$inputs = $this->getMethodInputs();
if ( empty($inputs) ) {
$this->response(array(
'status'=>'1',
'message'=>'Missing inputs.'
));
$this->response(404,'Not Found', array('message'=>'Missing method inputs.'));
}
// PARAMETERS
// ENDPOINT PARAMETERS
// ------------------------------------------------------------
$parameters = $this->getParameters($URI);
$parameters = $this->getEndpointParameters($URI);
if ( empty($parameters) ) {
$this->response(array(
'status'=>'1',
'message'=>'Missing parameters.'
));
$this->response(404,'Not Found', array('message'=>'Missing endpoint parameters.'));
}
// API TOKEN
@ -83,18 +77,12 @@ class pluginAPI extends Plugin {
// Check empty token
if ( empty($inputs['token']) ) {
$this->response(array(
'status'=>'1',
'message'=>'Missing API token.'
));
$this->response(404,'Not Found', array('message'=>'Missing API token.'));
}
// Check the token is valid
if( $inputs['token']!=$tokenAPI ) {
$this->response(array(
'status'=>'1',
'message'=>'Invalid API token.'
));
if ($inputs['token']!==$tokenAPI) {
$this->response(401, 'Unauthorized', array('message'=>'Invalid API token.'));
}
// AUTHENTICATION TOKEN
@ -109,7 +97,7 @@ class pluginAPI extends Plugin {
}
}
// REQUESTS
// ENDPOINTS
// ------------------------------------------------------------
// (GET) /api/pages
@ -125,13 +113,10 @@ class pluginAPI extends Plugin {
$data = $this->newPage($inputs);
}
else {
$data = array(
'status'=>'1',
'message'=>'Error: URI not found or Access denied.'
);
$this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.'));
}
$this->response($data);
$this->response(200, 'OK', $data);
}
// PRIVATE METHODS
@ -150,25 +135,7 @@ class pluginAPI extends Plugin {
return $this->method;
}
private function getParameters($URI)
{
// PARAMETERS
// ------------------------------------------------------------
// /api/pages | GET | returns all pages
// /api/pages/{key} | GET | returns the page with the {key}
// /api/cli/regenerate | POST | check for new posts and pages
$parameters = explode('/', $URI);
// Sanitize parameters
foreach($parameters as $key=>$value) {
$parameters[$key] = Sanitize::html($value);
}
return $parameters;
}
private function getInputs()
private function getMethodInputs()
{
switch($this->method) {
case "POST":
@ -189,6 +156,24 @@ class pluginAPI extends Plugin {
return $this->cleanInputs($inputs);
}
private function getEndpointParameters($URI)
{
// ENDPOINT Parameters
// ------------------------------------------------------------
// /api/pages | GET | returns all pages
// /api/pages/{key} | GET | returns the page with the {key}
// /api/pages | POST | create a new page
$parameters = explode('/', $URI);
// Sanitize parameters
foreach ($parameters as $key=>$value) {
$parameters[$key] = Sanitize::html($value);
}
return $parameters;
}
private function cleanInputs($inputs)
{
$tmp = array();
@ -196,8 +181,7 @@ class pluginAPI extends Plugin {
foreach($inputs as $key=>$value) {
$tmp[$key] = Sanitize::html($value);
}
}
elseif( is_string($inputs) ) {
} elseif ( is_string($inputs) ) {
$tmp = json_decode($inputs, true);
if(json_last_error()===0) {
$tmp = array();
@ -206,10 +190,11 @@ class pluginAPI extends Plugin {
return $tmp;
}
private function response($data=array())
private function response($code=200, $message='OK', $data=array())
{
$json = json_encode($data);
header('HTTP/1.1 '.$code.' '.$message);
header('Content-Type: application/json');
$json = json_encode($data);
exit($json);
}