Api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Drupal\janrain_views;
  3. class Api {
  4. public $ver;
  5. public $clientId;
  6. public $clientSecret;
  7. public $captureAddress;
  8. public $captureUrl;
  9. /**
  10. * Retrieves API access credentials from settings.
  11. */
  12. function __construct() {
  13. $this->ver = variable_get('janrain_capture_ver', array());
  14. if ($this->ver == '1.0') {
  15. $main = variable_get('janrain_capture_main', array());
  16. }
  17. else {
  18. $main = variable_get('janrain_capture_main2', array());
  19. }
  20. dsm($main, __METHOD__);
  21. $this->clientId = isset($main['capture_client_id'])
  22. ? $main['capture_client_id']
  23. : '';
  24. $this->clientSecret = isset($main['capture_client_secret'])
  25. ? $main['capture_client_secret']
  26. : '';
  27. $this->captureAddress = !empty($main['capture_address'])
  28. ? str_replace('https://', '', $main['capture_address'])
  29. : '';
  30. }
  31. public function defaultArgs() {
  32. $ret = array(
  33. 'client_id' => $this->clientId,
  34. 'client_secret' => $this->clientSecret,
  35. );
  36. return $ret;
  37. }
  38. /**
  39. * Performs the HTTP request.
  40. *
  41. * @param string $command
  42. * The Capture command to perform
  43. * @param array $args
  44. * The data set to pass via POST
  45. * @param string $access_token
  46. * The client access token to use when performing user-specific calls
  47. *
  48. * @return mixed
  49. * The HTTP request result data
  50. */
  51. public function __invoke($command, array $args = array(), $access_token = NULL) {
  52. $url = "https://{$this->captureAddress}/$command";
  53. $headers = array(
  54. 'Content-Type' => 'application/x-www-form-urlencoded',
  55. 'User-Agent' => 'Drupal',
  56. );
  57. if (isset($access_token)) {
  58. $headers['Authorization'] = "OAuth $access_token";
  59. }
  60. $options = array('headers' => $headers);
  61. $args = array_merge($args, $this->defaultArgs());
  62. $options['method'] = 'POST';
  63. $options['data'] = http_build_query($args, '', '&');
  64. dsm($options, $url);
  65. $result = drupal_http_request($url, $options);
  66. if (!isset($result->data) || $result->code != '200') {
  67. $this->reportError($result);
  68. return FALSE;
  69. }
  70. $data = json_decode($result->data, TRUE);
  71. // NULL decoded value indicates a parse error.
  72. if (!isset($data)) {
  73. $data['stat'] = 'error';
  74. $data['code'] = '0';
  75. $data['error'] = t('JSON parse error for: %data.', array('%data' => $result->data));
  76. }
  77. if ($data['stat'] == 'error') {
  78. $error = (object) array(
  79. 'code' => $data['code'],
  80. 'error' => t('@error: @description', array(
  81. '@error' => $data['error'],
  82. '@description' => $data['error_description'],
  83. )),
  84. );
  85. $this->reportError($error);
  86. return FALSE;
  87. }
  88. return $data;
  89. }
  90. /**
  91. * Helper function for the Engage web API wrappers.
  92. *
  93. * @param stdClass $result
  94. * Result containing error code and message
  95. */
  96. public function reportError($result) {
  97. $args = array(
  98. '%code' => $result->code,
  99. '%error' => $result->error,
  100. );
  101. watchdog('janrain_capture', 'Capture web API seems to be inaccessible due to "%code - %error".', $args, WATCHDOG_WARNING);
  102. drupal_set_message(t('Capture web API seems to be inaccessible because of error "%code - %error".', $args), 'error');
  103. }
  104. }
  105. /*
  106. use Drupal\janrain_views\Api.php;
  107. $path = drupal_get_path('module', 'janrain_views');
  108. require_once("$path/lib/Drupal/janrain_views/Api.inc");
  109. $j = new Api();
  110. dsm($j);
  111. $s = $j('entityType', array('type_name' => 'user'));
  112. dsm($s);
  113. */