View source
<?php
module_load_include('inc', 'deploy', 'includes/DeployServiceRest');
class DeployRemoteCcServiceRestJSON extends \DeployServiceRest {
public function __construct(array $config = array()) {
parent::__construct($config);
$this->config['timeout'] = 120.0;
}
public function deploy(\Traversable $entities) {
foreach ($entities->entities as $entity) {
$this->config['headers']['Content-Type'] = 'application/json';
$url = parse_url($this->config['url']);
$url = $url['scheme'] . "://" . $url['host'] . "/";
$url .= $entities
->buildPath(array(
$entity,
));
$this
->httpRequest($url, 'PUT');
}
}
public function getContext() {
return parent::fetchContext();
}
public function httpRequest($url, $method, $data = NULL) {
$this->config['headers']['Content-Type'] = 'application/json';
$options = array(
'method' => $method,
'headers' => $this->config['headers'],
'data' => $data,
'context' => $this->config['context'],
'timeout' => (double) $this->config['timeout'],
);
if ($this->config['debug']) {
watchdog('deploy', 'Service request: %url <pre>@options</pre>', array(
'%url' => $url,
'@options' => print_r($options, TRUE),
), WATCHDOG_DEBUG);
}
$response = drupal_http_request($url, $options);
if ($this->config['debug']) {
watchdog('deploy', 'Service response: <pre>@response</pre>', array(
'@response' => print_r($response, TRUE),
), WATCHDOG_DEBUG);
}
if (isset($response->error) || !in_array($response->code, array(
200,
304,
))) {
throw new \DeployServiceException(t('Service error: @code @error', array(
'@code' => $response->code,
'@error' => $response->error,
)));
}
return drupal_json_decode($response->data);
}
}