Commit 07c3fad3 authored by Karamel's avatar Karamel
Browse files

Add CommonAPI with login, version, archive, clean APICaller a bit.

parent 4766d98f
......@@ -5,7 +5,7 @@
namespace Pasteque\bin;
use \Pasteque\Server\AppContext;
use \Pasteque\Server\API\ArchiveAPI;
use \Pasteque\Server\CommonAPI\ArchiveAPI;
if ($argc < 2) {
die("createarchive.php <user_id> [max_exec_time]\n"
......
......@@ -20,8 +20,9 @@
// You should have received a copy of the GNU General Public License
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server\API;
namespace Pasteque\Server\CommonAPI;
use \Pasteque\Server\API\FiscalAPI;
use \Pasteque\Server\Exception\ConfigurationException;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
......@@ -32,7 +33,7 @@ use \Pasteque\Server\Model\GenericModel;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOCondition;
class ArchiveAPI implements API
class ArchiveAPI implements \Pasteque\Server\API\API
{
/** The maximum number of fiscal tickets in a single file of an archive. */
const BATCH_SIZE = 100;
......
......@@ -18,11 +18,11 @@
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server\API;
namespace Pasteque\Server\CommonAPI;
use \Pasteque\Server\System\Login;
class LoginAPI implements API {
class LoginAPI implements \Pasteque\Server\API\API {
public function __construct($identModule, $secret) {
$this->identModule = $identModule;
......@@ -47,4 +47,4 @@ class LoginAPI implements API {
public function login($login, $password) {
return $this->getToken($login, $password);
}
}
\ No newline at end of file
}
......@@ -18,12 +18,12 @@
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server\API;
namespace Pasteque\Server\CommonAPI;
use \Pasteque\Server\Model\GenericModel;
use \Pasteque\Server\Model\Option;
class VersionAPI implements API {
class VersionAPI implements \Pasteque\Server\API\API {
const VERSION = '8.1';
const REVISION = 2;
......
......@@ -22,7 +22,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\API\VersionAPI;
use \Pasteque\Server\CommonAPI\VersionAPI;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
......
......@@ -22,7 +22,7 @@
namespace Pasteque\Server\Model;
use \Pasteque\Server\API\VersionAPI;
use \Pasteque\Server\CommonAPI\VersionAPI;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\DAO\DoctrineMainModel;
......
......@@ -61,53 +61,58 @@ class APICaller {
if (!is_array($args)) { $args = array($args); }
// Get API class
$apiName = APICaller::formatAPIName($apiName);
$apiPkg = $app->isFiscalMirror() && strtolower($apiName) !== 'login' ?
'FiscalMirrorAPI' : 'API'; // TODO: ugly exception
$className = sprintf('\Pasteque\Server\%s\%s', $apiPkg, $apiName);
if (class_exists($className, true)) {
// Class found, get requested method
$apiClass = new \ReflectionClass($className);
if (!$apiClass->implementsInterface('\Pasteque\Server\API\API')) {
return APIResult::reject(new APINotFoundException($apiName));
$className = sprintf('\Pasteque\Server\CommonAPI\%s', $apiName);
if (!class_exists($className, true)) {
$apiPkg = $app->isFiscalMirror() ? 'FiscalMirrorAPI' : 'API';
$className = sprintf('\Pasteque\Server\%s\%s', $apiPkg, $apiName);
if (!class_exists($className, true)) {
$className = null;
}
$constructor = $apiClass->getMethod('fromApp');
$api = $constructor->invoke(null, $app);
if (method_exists($api, $methodName)) {
// Method found, check arguments
$method = new \ReflectionMethod($api, $methodName);
$methodParser = new APIMethodParser($api, $method);
if (!$methodParser->checkArgc($args)) {
return APIResult::reject(new APINotFoundException($apiName,
$methodName,
$methodParser->getMinArgc(),
$methodParser->getMaxArgc(),
count($args)));
}
try {
$realArgs = $methodParser->buildArgsArray($args);
return APIResult::success($method->invokeArgs($api,
$realArgs));
} catch (PastequeException $e) {
return APIResult::reject($e);
} catch (\BadMethodCallException $e) {
return APIResult::reject($e->getMessage());
} catch (\UnexpectedValueException $e) {
return APIResult::reject($e->getMessage());
} catch (\ReflectionException $e) {
$app->getLogger()->error('Internal error while calling API',
array('exception' => $e));
return APIResult::error($e->__toString());
} catch (\Exception $e) {
$app->getLogger()->error('Internal error',
array('exception' => $e));
return APIResult::error($e->__toString());
}
} else {
return APIResult::reject(new APINotFoundException($apiName,
$methodName));
}
} // end class exists
return APIResult::reject(new APINotFoundException($apiName));
}
if ($className == null) {
return APIResult::reject(new APINotFoundException($apiName));
}
// Get method
$apiClass = new \ReflectionClass($className);
if (!$apiClass->implementsInterface('\Pasteque\Server\API\API')) {
return APIResult::reject(new APINotFoundException($apiName));
}
$constructor = $apiClass->getMethod('fromApp');
$api = $constructor->invoke(null, $app);
if (!method_exists($api, $methodName)) {
return APIResult::reject(new APINotFoundException($apiName,
$methodName));
}
$method = new \ReflectionMethod($api, $methodName);
$methodParser = new APIMethodParser($api, $method);
// Check arguments
if (!$methodParser->checkArgc($args)) {
return APIResult::reject(new APINotFoundException($apiName,
$methodName,
$methodParser->getMinArgc(),
$methodParser->getMaxArgc(),
count($args)));
}
// Invoke
try {
$realArgs = $methodParser->buildArgsArray($args);
return APIResult::success($method->invokeArgs($api,
$realArgs));
} catch (PastequeException $e) {
return APIResult::reject($e);
} catch (\BadMethodCallException $e) {
return APIResult::reject($e->getMessage());
} catch (\UnexpectedValueException $e) {
return APIResult::reject($e->getMessage());
} catch (\ReflectionException $e) {
$app->getLogger()->error('Internal error while calling API',
array('exception' => $e));
return APIResult::error($e->__toString());
} catch (\Exception $e) {
$app->getLogger()->error('Internal error',
array('exception' => $e));
return APIResult::error($e->__toString());
}
}
/** Check if the user is authenticated and has permission.
......
......@@ -21,7 +21,7 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\API\ArchiveAPI;
use \Pasteque\Server\CommonAPI\ArchiveAPI;
use \Pasteque\Server\Exception\ConfigurationException;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
......
......@@ -21,7 +21,7 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\API\ArchiveAPI;
use \Pasteque\Server\CommonAPI\ArchiveAPI;
use \Pasteque\Server\Exception\ConfigurationException;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
......
......@@ -21,7 +21,7 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\API\VersionAPI;
use \Pasteque\Server\CommonAPI\VersionAPI;
use \Pasteque\Server\Model\Option;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
......
......@@ -21,7 +21,7 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\API\VersionAPI;
use \Pasteque\Server\CommonAPI\VersionAPI;
use \Pasteque\Server\Model\Option;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\DAO\DAOFactory;
......
......@@ -5,6 +5,9 @@
<testsuite name="Model">
</testsuite>
<testsuite name="API">
<file>API/VersionAPITest.php</file>
<file>API/ArchiveAPIRequestTest.php</file>
<file>API/ArchiveAPICreateTest.php</file>
</testsuite>
<testsuite name="http">
<directory>fiscalmirror/http</directory>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment