class EntityShareClient in Entity Share 7
Entity Share Client class.
Hierarchy
- class \EntityShareClient
Expanded class hierarchy of EntityShareClient
File
- includes/
entity_share.client.inc, line 11 - Class for handling communication with Entity Share Server.
View source
class EntityShareClient {
const HOOK_PREFIX = 'es_client_';
const WATCHDOG_TYPE = 'entity_share';
/**
* Session datas.
*
* @var array
* Session datas.
*
* @code
* array (
* 'session_name' => data,
* 'session_id' => data
* )
* @endcode
*/
protected $sessionDatas = array();
/**
* Url of the endpoint.
*
* @var string
* URL of the server.
*/
protected $endpointUrl;
/**
* Debug mode of the client.
*
* @var bool
* Debug mode.
*/
protected static $debug;
/**
* Last response from the last request.
*
* @var object
* Last response.
*/
protected $lastResponse;
/**
* Initialize properties.
*
* @param string $endpoint_url
* Url of the server endpoint.
* @param bool $debug
* Debug mode.
*/
public function __construct($endpoint_url, $debug = FALSE) {
$this->endpointUrl = $endpoint_url;
self::$debug = $debug;
}
/**
* Get the last response.
*
* @return object
* Last response object.
*/
public function getLastResponse() {
return $this->lastResponse;
}
/**
* Login to the remote server.
*
* @param string $login
* Login of the remote user.
* @param string $password
* Password of the remote user.
*
* @return bool
* TRUE if success, FALSE otherwise.
*/
public function login($login, $password) {
// Call the login rest api.
$url = $this->endpointUrl . '/login';
$datas = drupal_json_encode(array(
'login' => $login,
'password' => $password,
));
$response = self::call($url, 'POST', $datas);
if (!empty($response->data) && $response->code == '200') {
$result = drupal_json_decode($response->data);
if (!empty($result['status']) && $result['status'] == 'OK') {
// Store session_name and session_id.
$this->sessionDatas['session_name'] = $result['result']['session_name'];
$this->sessionDatas['session_id'] = $result['result']['session_id'];
return TRUE;
}
}
return FALSE;
}
/**
* Make an API call.
*
* @param string $url
* URL of the request.
* @param string $method
* HTTP method of the request.
* @param mixed $datas
* Data of the request.
*
* @return object
* Object of the response.
*/
public function call($url, $method, $datas = NULL) {
$options = array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
'method' => $method,
'data' => is_null($datas) ? '' : $datas,
);
// Add authentication information.
if (!empty($this->sessionDatas)) {
$options['headers']['Cookie'] = $this->sessionDatas['session_name'] . '=' . $this->sessionDatas['session_id'];
}
drupal_alter(self::HOOK_PREFIX . 'call_options', $options);
$this->lastResponse = drupal_http_request($url, $options);
if (self::$debug) {
watchdog(self::WATCHDOG_TYPE, '@url @method response => @response', array(
'@url' => $url,
'@method' => $method,
'@response' => drupal_json_encode($this->lastResponse),
));
}
return $this->lastResponse;
}
/**
* Get the endpoint URL.
*
* @return string
* The endpoint URL.
*/
public function getEndPointUrl() {
return $this->endpointUrl;
}
/**
* Get the debug mode status.
*
* @return bool
* The debug mode status.
*/
public function isDebug() {
return self::$debug;
}
/**
* Get an entity.
*
* @param string $entity_type
* Node, etc.
* @param int $entity_id
* (optional) Entity uuid to get.
*
* @return object|array
* Object of the response.
*/
public function get($entity_type, $entity_id = NULL) {
// Append path to endpoint URL.
$url = $this->endpointUrl . '/' . $entity_type;
if (!empty($entity_id)) {
$url .= '/' . $entity_id;
}
return $this
->call($url, 'GET');
}
/**
* Create an entity.
*
* @param string $entity_type
* Node, etc.
* @param mixed $datas
* Datas of the request.
*
* @return object
* Object of the response.
*/
public function create($entity_type, $datas) {
// Append path to endpoint URL.
$url = $this->endpointUrl . '/' . $entity_type;
return $this
->call($url, 'POST', $datas);
}
/**
* Update an entity.
*
* @param string $entity_type
* Node, etc.
* @param int $entity_id
* Entity uuid to update.
* @param mixed $datas
* Datas of the request.
*
* @return object
* Object of the response.
*/
public function update($entity_type, $entity_id, $datas) {
// Append path to endpoint URL.
$url = $this->endpointUrl . '/' . $entity_type . '/' . $entity_id;
return $this
->call($url, 'PUT', $datas);
}
/**
* Delete an entity.
*
* @param string $entity_type
* Node, etc.
* @param int $entity_id
* Entity uuid to delete.
*
* @return object
* Object of the response.
*/
public function delete($entity_type, $entity_id) {
// Append path to endpoint URL.
$url = $this->endpointUrl . '/' . $entity_type . '/' . $entity_id;
return $this
->call($url, 'DELETE');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityShareClient:: |
protected static | property | Debug mode of the client. | |
EntityShareClient:: |
protected | property | Url of the endpoint. | |
EntityShareClient:: |
protected | property | Last response from the last request. | |
EntityShareClient:: |
protected | property | Session datas. | |
EntityShareClient:: |
public | function | Make an API call. | |
EntityShareClient:: |
public | function | Create an entity. | |
EntityShareClient:: |
public | function | Delete an entity. | |
EntityShareClient:: |
public | function | Get an entity. | |
EntityShareClient:: |
public | function | Get the endpoint URL. | |
EntityShareClient:: |
public | function | Get the last response. | |
EntityShareClient:: |
constant | |||
EntityShareClient:: |
public | function | Get the debug mode status. | |
EntityShareClient:: |
public | function | Login to the remote server. | |
EntityShareClient:: |
public | function | Update an entity. | |
EntityShareClient:: |
constant | |||
EntityShareClient:: |
public | function | Initialize properties. |