221 lines
7.9 KiB
PHP
221 lines
7.9 KiB
PHP
<?php
|
|
|
|
class App_Model_RestResource_Abstract
|
|
{
|
|
|
|
const METHOD_BY_ID = 1;
|
|
const METHOD_ALL_LIST = 2;
|
|
const METHOD_FILTER = 3;
|
|
const METHOD_GENERIC = 4;
|
|
const METHOD_BY_COLUMNNAME = 5;
|
|
const METHOD_CUSTOM_URL = 6;
|
|
|
|
protected $_apiUrl;
|
|
protected $_apiVersion = "v1";
|
|
protected $_secretId = false;
|
|
protected $_testHeader = false;
|
|
|
|
protected $_httpClient = null;
|
|
protected $_risorsa;
|
|
|
|
public function __construct()
|
|
{
|
|
|
|
if (!$this->_risorsa || !$this->_apiUrl || !$this->_apiVersion)
|
|
throw new Exception("Non sono stati specificati tutti i parametri necessari alla risorsa (_risorsa,_apiUrl,_apiVersion)");
|
|
|
|
if ($this->_httpClient instanceof Zend_Http_Client) {
|
|
//$this->_httpClient = $this->_httpClient;
|
|
} else {
|
|
$this->_httpClient = new Zend_Http_Client();
|
|
$this->_httpClient->setConfig(array(
|
|
'adapter' => 'Zend_Http_Client_Adapter_Curl',
|
|
'maxredirects' => 0,
|
|
'timeout' => 60,
|
|
'ssltransport' => 'ssl'
|
|
));
|
|
}
|
|
}
|
|
|
|
/***** METODI COMUNI DELLE CHIMATE *****/
|
|
|
|
public function postCall($dati)
|
|
{
|
|
$this->_httpClient->setMethod('POST');
|
|
$this->_httpClient->setUri($this->getBaseUrl());
|
|
$this->_httpClient->setRawData(json_encode($dati), 'application/json;charset=utf-8');
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function updateById($id, $item)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('PUT');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_BY_ID, array('id' => $id)));
|
|
$this->_httpClient->setRawData(json_encode($item), 'application/json;charset=utf-8');
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function deleteById($id)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('DELETE');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_BY_ID, array('id' => $id)));
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getById($id)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_BY_ID, array('id' => $id)));
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getByColumnName($column, $value)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_BY_COLUMNNAME, array('colname' => $column, 'value' => $value)));
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getList($paged = null, $count = null, $order = null)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_ALL_LIST));
|
|
$this->_httpClient->setParameterGet(array('page' => $paged, 'count' => $count, 'order' => $order));
|
|
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getFiltered($paged = null, $count = null, $order = null, $where = null)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
|
|
if (is_array($order)) {
|
|
$orderStr = "";
|
|
$last_key = key(array_slice($order, -1, 1, TRUE));
|
|
foreach ($order as $k => $val) {
|
|
$orderStr .= $val;
|
|
if ($k !== $last_key) $orderStr .= ",";
|
|
}
|
|
}
|
|
if ($orderStr) $order = $orderStr;
|
|
|
|
$this->_httpClient->setParameterGet(array('page' => $paged, 'count' => $count, 'order' => $order));
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_FILTER, $where));
|
|
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getByMethod($method, $params = null)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_GENERIC, $method));
|
|
$this->_httpClient->setParameterGet($params);
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function getByCustomUrl($url, $params = null)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('GET');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_CUSTOM_URL, $url));
|
|
$this->_httpClient->setParameterGet($params);
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function postByCustomUrl($url, $dati = null)
|
|
{
|
|
$this->_httpClient->resetParameters();
|
|
$this->_httpClient->setMethod('POST');
|
|
$this->_httpClient->setUri($this->getMethodUrl(self::METHOD_CUSTOM_URL, $url));
|
|
$this->_httpClient->setRawData(json_encode($dati), 'application/json;charset=utf-8');
|
|
return $this->executeCall();
|
|
}
|
|
|
|
/***** METODI PRIVATI PER GESTIRE LE CHIAMATE *****/
|
|
|
|
private function getMethodUrl($method, $params = null)
|
|
{
|
|
$url = $this->getBaseUrl(!(self::METHOD_CUSTOM_URL === $method));
|
|
switch ($method) {
|
|
case self::METHOD_BY_ID:
|
|
if (!isset($params['id'])) throw new Exception("Id Richiesto");
|
|
$url .= "/" . $params['id'];
|
|
break;
|
|
case self::METHOD_ALL_LIST:
|
|
break;
|
|
case self::METHOD_FILTER:
|
|
$url .= "/method/filtra";
|
|
$this->_httpClient->setParameterGet($params);
|
|
break;
|
|
case self::METHOD_GENERIC:
|
|
if (!$params) throw new Exception("Nome del metodo non valido");
|
|
$url .= "/method/$params";
|
|
break;
|
|
case self::METHOD_BY_COLUMNNAME:
|
|
if (!isset($params['colname'])) throw new Exception("Nome Colonna Richiesto");
|
|
if (!isset($params['value'])) throw new Exception("Valore Richiesto");
|
|
$url .= "/method/bycolumn/colname/" . $params['colname'] . "/val/" . $params['value'];
|
|
break;
|
|
case self::METHOD_CUSTOM_URL:
|
|
if (!$params) throw new Exception("Nome dell'url non valido");
|
|
$url .= "$params";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return $url;
|
|
}
|
|
|
|
private function getBaseUrl($withResouces = true)
|
|
{
|
|
if ($withResouces) {
|
|
return $this->_apiUrl . "/" . $this->_apiVersion . "/" . strtolower($this->_risorsa);
|
|
} else {
|
|
return $this->_apiUrl . "/" . $this->_apiVersion;
|
|
}
|
|
}
|
|
|
|
private function executeCall()
|
|
{
|
|
|
|
if ($this->_secretId) {
|
|
$this->_httpClient->setHeaders('Authorization', $this->_secretId);
|
|
}
|
|
|
|
if ($this->_testHeader) {
|
|
$this->_httpClient->setHeaders('X-As-Dev-On', '1');
|
|
}
|
|
|
|
$httpResponse = $this->_httpClient->request();
|
|
|
|
//Zend_Registry::get('log')->info(print_r($this->_httpClient->getLastRequest(),true));
|
|
//Zend_Registry::get('log')->info(print_r($httpResponse,true));
|
|
|
|
$httpResponseEncoded = json_decode($httpResponse->getBody(), 1);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
Zend_Registry::get('log')->info($this->_httpClient->getUri());
|
|
Zend_Registry::get('log')->info("Errore codifica JSON:");
|
|
Zend_Registry::get('log')->info(print_r($httpResponse, true));
|
|
return null;
|
|
}
|
|
|
|
if ($this->_httpClient->getLastResponse()->getStatus() != '200') {
|
|
Zend_Registry::get('log')->info($this->_httpClient->getUri());
|
|
Zend_Registry::get('log')->info("Errore status response " . $this->_httpClient->getLastResponse()->getStatus());
|
|
Zend_Registry::get('log')->info(print_r($httpResponseEncoded, true));
|
|
throw new App_Exception_ResponseCloudException($httpResponseEncoded['message'], $httpResponseEncoded['errorCode']);
|
|
}
|
|
|
|
return $httpResponseEncoded;
|
|
}
|
|
|
|
}
|