136 lines
3.5 KiB
PHP
136 lines
3.5 KiB
PHP
<?php
|
|
namespace Drupal\janrain_views;
|
|
|
|
class Api {
|
|
public $ver;
|
|
public $clientId;
|
|
public $clientSecret;
|
|
public $captureAddress;
|
|
public $captureUrl;
|
|
|
|
/**
|
|
* Retrieves API access credentials from settings.
|
|
*/
|
|
function __construct() {
|
|
$this->ver = variable_get('janrain_capture_ver', array());
|
|
|
|
if ($this->ver == '1.0') {
|
|
$main = variable_get('janrain_capture_main', array());
|
|
}
|
|
else {
|
|
$main = variable_get('janrain_capture_main2', array());
|
|
}
|
|
dsm($main, __METHOD__);
|
|
$this->clientId = isset($main['capture_client_id'])
|
|
? $main['capture_client_id']
|
|
: '';
|
|
|
|
$this->clientSecret = isset($main['capture_client_secret'])
|
|
? $main['capture_client_secret']
|
|
: '';
|
|
|
|
$this->captureAddress = !empty($main['capture_address'])
|
|
? str_replace('https://', '', $main['capture_address'])
|
|
: '';
|
|
}
|
|
|
|
public function defaultArgs() {
|
|
$ret = array(
|
|
'client_id' => $this->clientId,
|
|
'client_secret' => $this->clientSecret,
|
|
);
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* Performs the HTTP request.
|
|
*
|
|
* @param string $command
|
|
* The Capture command to perform
|
|
* @param array $args
|
|
* The data set to pass via POST
|
|
* @param string $access_token
|
|
* The client access token to use when performing user-specific calls
|
|
*
|
|
* @return mixed
|
|
* The HTTP request result data
|
|
*/
|
|
public function __invoke($command, array $args = array(), $access_token = NULL) {
|
|
$url = "https://{$this->captureAddress}/$command";
|
|
|
|
$headers = array(
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
'User-Agent' => 'Drupal',
|
|
);
|
|
|
|
if (isset($access_token)) {
|
|
$headers['Authorization'] = "OAuth $access_token";
|
|
}
|
|
|
|
$options = array('headers' => $headers);
|
|
|
|
$args = array_merge($args, $this->defaultArgs());
|
|
$options['method'] = 'POST';
|
|
$options['data'] = http_build_query($args, '', '&');
|
|
|
|
dsm($options, $url);
|
|
$result = drupal_http_request($url, $options);
|
|
|
|
if (!isset($result->data) || $result->code != '200') {
|
|
$this->reportError($result);
|
|
return FALSE;
|
|
}
|
|
|
|
$data = json_decode($result->data, TRUE);
|
|
|
|
// NULL decoded value indicates a parse error.
|
|
if (!isset($data)) {
|
|
$data['stat'] = 'error';
|
|
$data['code'] = '0';
|
|
$data['error'] = t('JSON parse error for: %data.', array('%data' => $result->data));
|
|
}
|
|
if ($data['stat'] == 'error') {
|
|
$error = (object) array(
|
|
'code' => $data['code'],
|
|
'error' => t('@error: @description', array(
|
|
'@error' => $data['error'],
|
|
'@description' => $data['error_description'],
|
|
)),
|
|
);
|
|
$this->reportError($error);
|
|
return FALSE;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Helper function for the Engage web API wrappers.
|
|
*
|
|
* @param stdClass $result
|
|
* Result containing error code and message
|
|
*/
|
|
public function reportError($result) {
|
|
$args = array(
|
|
'%code' => $result->code,
|
|
'%error' => $result->error,
|
|
);
|
|
watchdog('janrain_capture', 'Capture web API seems to be inaccessible due to "%code - %error".', $args, WATCHDOG_WARNING);
|
|
drupal_set_message(t('Capture web API seems to be inaccessible because of error "%code - %error".', $args), 'error');
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
use Drupal\janrain_views\Api.php;
|
|
|
|
$path = drupal_get_path('module', 'janrain_views');
|
|
require_once("$path/lib/Drupal/janrain_views/Api.inc");
|
|
|
|
$j = new Api();
|
|
dsm($j);
|
|
$s = $j('entityType', array('type_name' => 'user'));
|
|
dsm($s);
|
|
|
|
*/
|