123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?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);
- */
|