View source
<?php
if (class_exists('WsConnector')) {
class restclient_wsconnector extends WsConnector {
public function supportsCaching() {
return TRUE;
}
public function __construct($endpoint) {
$this->languagePlugins = array(
'header',
'replace',
'default',
);
parent::__construct($endpoint);
}
public function getMethods() {
return array(
'single' => array(
'create' => t('RESTful create method (POST)'),
'read' => t('RESTful read method (GET)'),
'update' => t('RESTful update method (PUT)'),
'delete' => t('RESTful delete method (DELETE)'),
'index' => t('RESTful index method (GET)'),
),
'multiple' => array(
'action' => t('RESTful action (POST)'),
),
);
}
protected function restclientPrepareWsCall(&$type, &$endpoint, &$method, &$arguments, &$options) {
if ($type == 'create' and isset($options['create_method'])) {
$type = $options['create_method'];
}
if (is_callable(array(
$this,
'setError',
)) and !isset($options['error_handling'])) {
$options['error_handling'] = __CLASS__;
}
if (!empty($arguments) and !is_string($arguments)) {
$options['data'] = $arguments;
$arguments = NULL;
}
if ($type == 'update' and isset($options['update_method'])) {
$type = $options['update_method'];
}
if (empty($method) and isset($options['accept-type'])) {
$method = "." . $options['accept-type'];
}
if (!empty($options['language']) and isset($options['language plugin'])) {
foreach ($options['language plugin'] as $name => $settings) {
switch ($name) {
case 'header':
$options['headers'][$settings['header']] = $settings[$options['language']];
break;
case 'replace':
$replace = $settings['default'];
if (!empty($settings[$options['language']])) {
$replace = $settings[$options['language']];
}
$method = str_replace('{language}', $replace, $method);
$endpoint = str_replace('{language}', $replace, $endpoint);
break;
default:
}
break;
}
}
if (isset($this->cacheDefaultTime) and isset($this->cacheDefaultOverride)) {
$options['cache_default_time'] = $this->cacheDefaultTime;
$options['cache_default_override'] = $this->cacheDefaultOverride;
}
if (isset($this->staleCache) and $this->staleCache) {
$options['stale_cache'] = TRUE;
}
}
protected function restclientCreate(&$type, &$endpoint, &$method, &$arguments, &$options) {
$variables = array(
'endpoint' => $endpoint,
'body' => $arguments,
) + $options;
if (empty($arguments)) {
unset($variables['body']);
}
$response = restclient_post($method, $variables);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling'])) {
if ($options['error_handling'] == __CLASS__) {
$this
->setError($response->code, $response->error);
return FALSE;
}
else {
$this->expires = 0;
return $response;
}
}
else {
return FALSE;
}
}
else {
return $response->data;
}
}
protected function restclientRead(&$type, &$endpoint, &$method, &$arguments, &$options) {
$response = restclient_get($method, array(
'endpoint' => $endpoint,
) + $options);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling'])) {
if ($options['error_handling'] == __CLASS__) {
$this
->setError($response->code, $response->error);
return FALSE;
}
else {
$this->expires = 0;
return $response;
}
}
else {
return FALSE;
}
}
else {
if (isset($response->expires)) {
$this->expires = $response->expires;
}
return $response->data;
}
}
protected function restclientUpdate(&$type, &$endpoint, &$method, &$arguments, &$options) {
$variables = array(
'endpoint' => $endpoint,
'body' => $arguments,
) + $options;
if (empty($arguments)) {
unset($variables['body']);
}
$response = restclient_put($method, $variables);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling'])) {
if ($options['error_handling'] == __CLASS__) {
$this
->setError($response->code, $response->error);
return FALSE;
}
else {
$this->expires = 0;
return $response;
}
}
else {
return FALSE;
}
}
else {
return $response->data;
}
}
protected function restclientDelete(&$type, &$endpoint, &$method, &$arguments, &$options) {
$response = restclient_delete($method, array(
'endpoint' => $endpoint,
) + $options);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling'])) {
if ($options['error_handling'] == __CLASS__) {
$this
->setError($response->code, $response->error);
return FALSE;
}
else {
$this->expires = 0;
return $response;
}
}
else {
return FALSE;
}
}
else {
return $response->data;
}
}
protected function restclientIndex(&$type, &$endpoint, &$method, &$arguments, &$options) {
$response = restclient_get($method, array(
'endpoint' => $endpoint,
) + $options);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling'])) {
if ($options['error_handling'] == __CLASS__) {
$this
->setError($response->code, $response->error);
return FALSE;
}
else {
$this->expires = 0;
return $response;
}
}
else {
return FALSE;
}
}
else {
if (isset($response->expires)) {
$this->expires = $response->expires;
}
return $response->data;
}
}
public function wscall($type, $method, $arguments, $options) {
$endpoint = $this->endpoint;
$this
->restclientPrepareWsCall($type, $endpoint, $method, $arguments, $options);
switch ($type) {
case 'create':
return $this
->restclientCreate($type, $endpoint, $method, $arguments, $options);
case 'read':
return $this
->restclientRead($type, $endpoint, $method, $arguments, $options);
case 'update':
return $this
->restclientUpdate($type, $endpoint, $method, $arguments, $options);
case 'delete':
return $this
->restclientDelete($type, $endpoint, $method, $arguments, $options);
case 'index':
return $this
->restclientIndex($type, $endpoint, $method, $arguments, $options);
default:
if (drupal_substr($type, 0, 7) == 'action_') {
$variables = array(
'endpoint' => $endpoint,
'body' => $arguments,
) + $options;
if (empty($arguments)) {
unset($variables['body']);
}
$response = restclient_post($method, $variables);
if (restclient_response_code($response) != RESTCLIENT_RESPONSE_SUCCESS) {
if (isset($options['error_handling']) and $options['error_handling']) {
$this->expires = 0;
return $response;
}
else {
return FALSE;
}
}
else {
return $response->data;
}
}
}
}
public function update($id, $method, $object, $options = array()) {
$this->expires = 0;
return $this
->wscall('update', $method, $object, $options);
}
public function isDegraded() {
$error = $this
->getError();
if (!isset($error) or !isset($error['code'])) {
return FALSE;
}
if ($error['code'] == -1) {
return TRUE;
}
return FALSE;
}
}
}