2
0
forked from Wavyzz/dolibarr

Merge branch 'refactor-rest-api' of

https://github.com/EuskalMoneta/dolibarr into
EuskalMoneta-refactor-rest-api

Conflicts:
	htdocs/api/admin/explorer.php
This commit is contained in:
Laurent Destailleur
2016-07-27 14:06:14 +02:00
21 changed files with 2514 additions and 122 deletions

View File

@@ -104,17 +104,43 @@ foreach ($modulesdir as $dir)
{
while (($file_searched = readdir($handle_part))!==false)
{
// Support of the deprecated API.
if (is_readable($dir_part.$file_searched) && preg_match("/^api_deprecated_(.*)\.class\.php$/i",$file_searched,$reg))
{
$classname = ucwords($reg[1]).'Api';
require_once $dir_part.$file_searched;
if (class_exists($classname))
{
dol_syslog("Found deprecated API classname=".$classname);
$api->r->addAPIClass($classname, '');
}
}
elseif (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i",$file_searched,$reg))
{
$classname = ucwords($reg[1]);
require_once $dir_part.$file_searched;
if (class_exists($classname))
{
dol_syslog("Found API classname=".$classname);
$listofapis[] = $classname;
}
}
/*
if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
{
$classname=$reg[1];
$classname = str_replace('Api_','',ucwords($reg[1])).'Api';
//$classname = str_replace('Api_','',ucwords($reg[1]));
$classname = ucfirst($classname);
require_once $dir_part.$file_searched;
if (class_exists($classname))
{
dol_syslog("Found API classname=".$classname);
$api->r->addAPIClass($classname,'');
/*
require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Routes.php';
$tmpclass = new ReflectionClass($classname);
@@ -125,8 +151,9 @@ foreach ($modulesdir as $dir)
}*/
//$listofapis[]=array('classname'=>$classname, 'fullpath'=>$file_searched);
}
}
/* }
}*/
}
}
}
@@ -135,8 +162,8 @@ foreach ($modulesdir as $dir)
}
}
$listofapis=Routes::toArray();
//var_dump($listofapis);
$listofapis=Routes::toArray(); // TODO api for "status" is lost here
//var_dump($listofapis);
@@ -166,6 +193,7 @@ print $langs->trans("ListOfAvailableAPIs").':<br>';
foreach($listofapis['v1'] as $key => $val)
{
if ($key == 'login') continue;
if ($key == 'index') continue;
if ($key)
{

View File

@@ -95,9 +95,23 @@ print '</tr>';
print '</table>';
print '<br><br>';
// Define $urlwithroot
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
// Show message
$message='';
$url='<a href="'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword" target="_blank">'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword[&reset=1]</a>';
$message.=$langs->trans("UrlToGetKeyToUseAPIs").':<br>';
$message.=img_picto('','object_globe.png').' '.$url;
print $message;
print '<br>';
print '<br>';
// Explorer
print '<u>'.$langs->trans("ApiExporerIs").':</u><br>';
$url=DOL_MAIN_URL_ROOT.'/api/admin/explorer.php';
$url=DOL_MAIN_URL_ROOT.'/api/index.php/explorer';
print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";

View File

@@ -88,6 +88,17 @@ class DolibarrApi
// Remove $db object property for object
unset($object->db);
// Remove the $oldcopy property because it is not supported by the JSON
// encoder. The following error is generated when trying to serialize
// it: "Error encoding/decoding JSON: Type is not supported"
// Note: Event if this property was correctly handled by the JSON
// encoder, it should be ignored because keeping it would let the API
// have a very strange behavior: calling PUT and then GET on the same
// resource would give different results:
// PUT /objects/{id} -> returns object with oldcopy = previous version of the object
// GET /objects/{id} -> returns object with oldcopy empty
unset($object->oldcopy);
// If object has lines, remove $db property
if(isset($object->lines) && count($object->lines) > 0) {
$nboflines = count($object->lines);

View File

@@ -18,14 +18,11 @@
use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
/**
* API generic (login, status, ...)
*
* API that allows to log in with an user account.
*/
class GenericApi extends DolibarrApi
class Login
{
function __construct() {
@@ -46,7 +43,7 @@ class GenericApi extends DolibarrApi
*
* @throws RestException
*/
public function login($login, $password, $entity=0, $reset=0) {
public function index($login, $password, $entity=0, $reset=0) {
global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
@@ -103,20 +100,4 @@ class GenericApi extends DolibarrApi
)
);
}
/**
* Get status (Dolibarr version)
*
* @access protected
* @class DolibarrApiAccess {@requires admin}
*/
function status() {
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
return array(
'success' => array(
'code' => 200,
'dolibarr_version' => DOL_VERSION
)
);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
/**
* API that gives the status of the Dolibarr instance.
*
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class Status
{
/**
* Get status (Dolibarr version)
*/
function index() {
return array(
'success' => array(
'code' => 200,
'dolibarr_version' => DOL_VERSION
)
);
}
}

View File

@@ -58,7 +58,10 @@ if (empty($conf->global->MAIN_MODULE_API))
$api = new DolibarrApi($db);
$api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
// Enable the Restler API Explorer.
// See https://github.com/Luracast/Restler-API-Explorer for more info.
$api->r->addAPIClass('Luracast\\Restler\\Explorer');
$api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
$api->r->addAuthenticationClass('DolibarrApiAccess','');
@@ -77,25 +80,19 @@ foreach ($modulesdir as $dir)
{
while (($file = readdir($handle))!==false)
{
if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
if (is_readable($dir.$file) && preg_match("/^mod(.*)\.class\.php$/i",$file,$reg))
{
$modulename=$reg[1];
$module = $part = strtolower($reg[1]);
// Defined if module is enabled
$enabled=true;
$module=$part=$obj=strtolower(preg_replace('/^mod/i','',$modulename));
//if ($part == 'propale') $part='propal';
if ($module == 'societe') {
$obj = 'thirdparty';
}
if ($module == 'categorie') {
$part = 'categories';
$obj = 'category';
}
if ($module == 'facture') {
$part = 'compta/facture';
$obj = 'facture';
}
// Defined if module is enabled
$enabled=true;
if (empty($conf->$module->enabled)) $enabled=false;
if ($enabled)
@@ -115,17 +112,25 @@ foreach ($modulesdir as $dir)
{
while (($file_searched = readdir($handle_part))!==false)
{
if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
// Support of the deprecated API.
if (is_readable($dir_part.$file_searched) && preg_match("/^api_deprecated_(.*)\.class\.php$/i",$file_searched,$reg))
{
$classname=$reg[1];
$classname = str_replace('Api_','',ucwords($reg[1])).'Api';
$classname = ucfirst($classname);
$classname = ucwords($reg[1]).'Api';
require_once $dir_part.$file_searched;
if (class_exists($classname))
if (class_exists($classname))
{
dol_syslog("Found API classname=".$classname);
$api->r->addAPIClass($classname,'');
$listofapis[]=array('classname'=>$classname, 'fullpath'=>$file_searched);
dol_syslog("Found deprecated API classname=".$classname);
$api->r->addAPIClass($classname, '');
}
}
elseif (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i",$file_searched,$reg))
{
$classname = ucwords($reg[1]);
require_once $dir_part.$file_searched;
if (class_exists($classname))
{
dol_syslog("Found API classname=".$classname);
$listofapis[] = $classname;
}
}
}
@@ -136,6 +141,14 @@ foreach ($modulesdir as $dir)
}
}
// Sort the classes before adding them to Restler. The Restler API Explorer
// shows the classes in the order they are added and it's a mess if they are
// not sorted.
sort($listofapis);
foreach ($listofapis as $classname)
{
$api->r->addAPIClass($classname);
}
// TODO If not found, redirect to explorer