diff --git a/src/Service/WorkgroupApi.php b/src/Service/WorkgroupApi.php index c992374..c12df48 100644 --- a/src/Service/WorkgroupApi.php +++ b/src/Service/WorkgroupApi.php @@ -2,6 +2,7 @@ namespace Drupal\stanford_samlauth\Service; +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Site\Settings; @@ -17,20 +18,6 @@ class WorkgroupApi implements WorkgroupApiInterface { const WORKGROUP_API = 'https://workgroupsvc.stanford.edu/workgroups/2.0'; - /** - * Config factory service. - * - * @var \Drupal\Core\Config\ConfigFactoryInterface - */ - protected $configFactory; - - /** - * Guzzle client service. - * - * @var \GuzzleHttp\ClientInterface - */ - protected $guzzle; - /** * Logger channel service. * @@ -55,16 +42,16 @@ class WorkgroupApi implements WorkgroupApiInterface { /** * StanfordSSPWorkgroupApi constructor. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * Config factory service. * @param \GuzzleHttp\ClientInterface $guzzle * Http client guzzle service. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger * Logger channel factory service. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache + * Caching service. */ - public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $guzzle, LoggerChannelFactoryInterface $logger) { - $this->configFactory = $config_factory; - $this->guzzle = $guzzle; + public function __construct(protected ConfigFactoryInterface $configFactory, protected ClientInterface $guzzle, LoggerChannelFactoryInterface $logger, protected CacheBackendInterface $cache) { $this->logger = $logger->get('stanford_samlauth'); $config = $this->configFactory->get('stanford_samlauth.settings'); @@ -159,6 +146,10 @@ public function isSunetValid(string $sunet): bool { * API response or false if fails. */ protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?array { + $cached_data = $this->cache->get("samlauth:$workgroup:$sunet"); + if ($cached_data) { + return $cached_data->data; + } $config = $this->configFactory->get('stanford_samlauth.settings'); $options = [ 'cert' => $this->getCert(), @@ -173,7 +164,9 @@ protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?arr $api_url = Settings::get('stanford_samlauth.workgroup_api', self::WORKGROUP_API); try { $result = $this->guzzle->request('GET', $api_url, $options); - return json_decode($result->getBody(), TRUE); + $result = json_decode($result->getBody(), TRUE, 512, JSON_THROW_ON_ERROR); + $this->cache->set("samlauth:$workgroup:$sunet", $result, time() + ini_get('max_execution_time') ?: 60); + return $result; } catch (GuzzleException $e) { $this->logger->error('Unable to connect to workgroup api. @message', ['@message' => $e->getMessage()]); diff --git a/stanford_samlauth.services.yml b/stanford_samlauth.services.yml index 6aa89a3..24d4bd9 100644 --- a/stanford_samlauth.services.yml +++ b/stanford_samlauth.services.yml @@ -1,7 +1,7 @@ services: stanford_samlauth.workgroup_api: class: Drupal\stanford_samlauth\Service\WorkgroupApi - arguments: [ '@config.factory', '@http_client', '@logger.factory' ] + arguments: [ '@config.factory', '@http_client', '@logger.factory', '@cache.default' ] stanford_samlauth.event_subscriber: class: Drupal\stanford_samlauth\EventSubscriber\StanfordSamlAuthSubscriber