Add a factory function
This commit is contained in:
parent
a8d9667d54
commit
954195b57f
52
_public.php
52
_public.php
|
@ -4,6 +4,56 @@ if (!defined('DC_RC_PATH')) { return; }
|
|||
$core->url->register('rest','rest','^rest(?:/(.*))?$',array('rest','getResponse'));
|
||||
class rest extends dcUrlHandlers
|
||||
{
|
||||
|
||||
/**
|
||||
* Create the "good" RestQuery instance
|
||||
* Input: $httpMethod: String (POST/GET/PATCH etc...)
|
||||
* $args Url arguments
|
||||
* $user dcAuth object
|
||||
* $body Body of the input query. String
|
||||
* Output: object RestQuery
|
||||
*/
|
||||
private function restFactoryQuery($httpMethod,$args,$user,$body){
|
||||
|
||||
//définir la methode API (pas HTML) appelée
|
||||
switch($httpMethod){
|
||||
case "GET":
|
||||
if($args == 'blogs'){
|
||||
$queryObj = new RestQueryGetBlogs($user);
|
||||
break;
|
||||
}elseif($args == 'specs'){
|
||||
$queryObj = new RestQueryGetSpecs($user);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "POST":
|
||||
|
||||
break;
|
||||
case "PUT":
|
||||
|
||||
break;
|
||||
|
||||
case "PATCH":
|
||||
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
|
||||
break;
|
||||
default:
|
||||
$this->response_code = RestQuery::get_full_code_header(400);
|
||||
$this->response_message = array(
|
||||
"error" => "Unrecoknized method",
|
||||
"code" => 400
|
||||
);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
return $queryObj;
|
||||
|
||||
}
|
||||
|
||||
public static function getResponse($args)
|
||||
{
|
||||
global $core;
|
||||
|
@ -44,7 +94,7 @@ class rest extends dcUrlHandlers
|
|||
}
|
||||
}
|
||||
|
||||
$r = new RestQuery($_SERVER['REQUEST_METHOD'],$args,$user);
|
||||
$r = rest::restFactoryQuery($_SERVER['REQUEST_METHOD'],$args,$user,file_get_contents('php://input'));
|
||||
header($r->response_code);
|
||||
echo json_encode($r->response_message);
|
||||
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
<?php
|
||||
|
||||
class RestQueryGetBlogs
|
||||
class RestQueryGetBlogs extends RestQuery
|
||||
{
|
||||
public $response_code;
|
||||
public $response_message; //array
|
||||
|
||||
public function __construct($user){
|
||||
if($user === false){
|
||||
//need To be authentified
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
<?php
|
||||
class RestQueryGetSpecs
|
||||
class RestQueryGetSpecs extends RestQuery
|
||||
{
|
||||
|
||||
public $response_code;
|
||||
public $response_message; //array
|
||||
|
||||
public function __construct($user){
|
||||
global $core;
|
||||
$this->response_code = 200;
|
||||
|
|
|
@ -3,49 +3,6 @@
|
|||
class RestQuery{
|
||||
public $response_code;
|
||||
public $response_message; //array
|
||||
private $queryObj;
|
||||
|
||||
public function __construct($httpMethod,$args,$user){
|
||||
error_log($httpMethod." ".$args);
|
||||
//définir la methode API (pas HTML) appelée
|
||||
switch($httpMethod){
|
||||
case "GET":
|
||||
if($args == 'blogs'){
|
||||
$queryObj = new RestQueryGetBlogs($user);
|
||||
break;
|
||||
}elseif($args == 'specs'){
|
||||
$queryObj = new RestQueryGetSpecs($user);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "POST":
|
||||
|
||||
break;
|
||||
case "PUT":
|
||||
|
||||
break;
|
||||
|
||||
case "PATCH":
|
||||
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
|
||||
break;
|
||||
default:
|
||||
$this->response_code = RestQuery::get_full_code_header(400);
|
||||
$this->response_message = array(
|
||||
"error" => "Unrecoknized method",
|
||||
"code" => 400
|
||||
);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
$this->response_code = $queryObj->response_code;
|
||||
$this->response_message = $queryObj->response_message;
|
||||
|
||||
}
|
||||
|
||||
public function get_full_code_header($code){
|
||||
static $codes = array(
|
||||
|
@ -111,12 +68,5 @@ class RestQuery{
|
|||
}else{
|
||||
return "HTTP/1.0 ".$code." Something wrong happened";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//etc...
|
Loading…
Reference in New Issue
Block a user