From a066deea4951cc3b1158c2112af964a3b5094c3b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 27 Mar 2015 00:30:25 -0700 Subject: [PATCH] CRM-16173 - CxnApp.get API --- api/v3/CxnApp.php | 116 +++++++++++++++++++++++++++++ api/v3/utils.php | 9 ++- tests/phpunit/api/v3/UtilsTest.php | 6 ++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 api/v3/CxnApp.php diff --git a/api/v3/CxnApp.php b/api/v3/CxnApp.php new file mode 100644 index 0000000000..89d3a4d068 --- /dev/null +++ b/api/v3/CxnApp.php @@ -0,0 +1,116 @@ + 'appCert', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => ts('Certificate'), + 'description' => 'PEM-encoded certificate', + ); + $spec['appId'] = array( + 'name' => 'appId', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Application GUID'), + 'description' => 'Application GUID', + 'maxlength' => 128, + 'size' => CRM_Utils_Type::HUGE, + ); + $spec['appUrl'] = array( + 'name' => 'appUrl', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Registration URL'), + 'description' => 'An endpoint to notify when performing registration', + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + ); + $spec['desc'] = array( + 'name' => 'desc', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => ts('Description'), + 'description' => 'Description', + ); + //$spec['perm'] = array( + // 'name' => 'perm', + // 'type' => CRM_Utils_Type::T_TEXT, + // 'title' => ts('Permissions'), + // 'description' => 'Permissions expected for the service (struct)', + //); +} + +/** + * Get a list of applications available for connections. + * + * @param array $params + * @return array + * @throws API_Exception + * @throws CRM_Core_Exception + * @throws \Civi\Cxn\Rpc\Exception\InvalidMessageException + */ +function civicrm_api3_cxn_app_get($params) { + // FIXME: We should cache, but CRM_Utils_Cache and CRM_Core_BAO_Cache don't seem to support TTL... + list ($status, $blob) = CRM_Utils_HttpClient::singleton()->get(\Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL); + if (CRM_Utils_HttpClient::STATUS_OK != $status) { + throw new API_Exception("Failed to download application list."); + } + $agent = new \Civi\Cxn\Rpc\Agent(CRM_Cxn_BAO_Cxn::getCACert(), NULL, NULL); + $message = $agent->decode(array(AppMetasMessage::NAME, GarbledMessage::NAME), $blob); + if ($message instanceof AppMetasMessage) { + return _civicrm_api3_basic_array_get('CxnApp', $params, $message->getData(), 'appId', + array('appId', 'appUrl', 'desc', 'appCert', 'perm')); + } + elseif ($message instanceof GarbledMessage) { + return civicrm_api3_create_error('Received garbled response', array( + 'garbled_message' => $message->getData(), + )); + } + else { + return civicrm_api3_create_error("Unrecognized message"); + } +} diff --git a/api/v3/utils.php b/api/v3/utils.php index af33724230..c1d78b2e99 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2268,9 +2268,9 @@ function _civicrm_api3_field_value_check(&$params, $fieldName) { } /** - * Like _civicrm_api3_basic_get, but data is backed by a simple array - * instead of a DAO/BAO. This is useful for small/mid-size data loaded - * from JSON or XML documents. + * A generic "get" API based on simple array data. This is comparable to + * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for + * small/mid-size data loaded from external JSON or XML documents. * * @param array $params * API parameters. @@ -2297,6 +2297,9 @@ function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $fiel } $match = TRUE; foreach ($params as $k => $v) { + if ($k == 'id') { + $k = $idCol; + } if (in_array($k, $fields) && $record[$k] !== $v) { $match = FALSE; break; diff --git a/tests/phpunit/api/v3/UtilsTest.php b/tests/phpunit/api/v3/UtilsTest.php index c5a90b75da..62a642ad28 100644 --- a/tests/phpunit/api/v3/UtilsTest.php +++ b/tests/phpunit/api/v3/UtilsTest.php @@ -341,6 +341,12 @@ class api_v3_UtilsTest extends CiviUnitTestCase { array('b', 'c'), ); + $cases[] = array( + $records, + array('version' => 3, 'id' => 'd'), + array('d'), + ); + return $cases; } -- 2.25.1