From dc8d6b6fdbf600e30767af5f953d9be60205f794 Mon Sep 17 00:00:00 2001 From: pookmish Date: Wed, 29 May 2024 08:27:01 -0700 Subject: [PATCH] Store workgroup api response to avoid unncessary repeated requests (#16) --- src/Service/WorkgroupApi.php | 59 ++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/Service/WorkgroupApi.php b/src/Service/WorkgroupApi.php index c992374..30f166e 100644 --- a/src/Service/WorkgroupApi.php +++ b/src/Service/WorkgroupApi.php @@ -12,25 +12,24 @@ * Workgroup api service class to connect to the API. * * @package Drupal\stanford_samlauth\Service + * + * @phpstan-type WorkgroupInfo array{ + * lastUpdated: string, + * description: string, + * lastUpdatedBy: string, + * name: string, + * memberCount: string, + * } + * @phpstan-type WorkgroupAPIResponse array{ + * id: string, + * type: string, + * result: array + * } */ 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. * @@ -52,19 +51,27 @@ class WorkgroupApi implements WorkgroupApiInterface { */ protected $key; + /** + * Keyed array of api responses, keyed by the type and the id of the request. + * + * @var array{ + * workgroup?: array, + * user?: array + * } + */ + protected $responses = []; + /** * 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. */ - 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) { $this->logger = $logger->get('stanford_samlauth'); $config = $this->configFactory->get('stanford_samlauth.settings'); @@ -155,10 +162,16 @@ public function isSunetValid(string $sunet): bool { * @param string|null $sunet * User sunetid. * - * @return null|array + * @return null|array * API response or false if fails. */ protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?array { + $type = $workgroup ? 'workgroup' : 'user'; + $id = $workgroup ?: $sunet; + if (isset($this->responses[$type][$id])) { + return $this->responses[$type][$id]; + } + $config = $this->configFactory->get('stanford_samlauth.settings'); $options = [ 'cert' => $this->getCert(), @@ -166,14 +179,16 @@ protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?arr 'verify' => TRUE, 'timeout' => $config->get('role_mapping.workgroup_api.timeout') ?: 30, 'query' => [ - 'type' => $workgroup ? 'workgroup' : 'user', - 'id' => $workgroup ?: $sunet, + 'type' => $type, + 'id' => $id, ], ]; $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->responses[$type][$id] = $result; + return $result; } catch (GuzzleException $e) { $this->logger->error('Unable to connect to workgroup api. @message', ['@message' => $e->getMessage()]);