From 42a64c852cbe476d3854548922320cac28926a7f Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Mon, 19 Feb 2024 16:45:31 +0000 Subject: [PATCH] Move debug message in HttpWrapper --- Lib/GrouperApiAccess.php | 68 +++++++++++++++++--------------------- Lib/GrouperHTTPWrapper.php | 12 ++++++- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/Lib/GrouperApiAccess.php b/Lib/GrouperApiAccess.php index 201a233..e7ad416 100644 --- a/Lib/GrouperApiAccess.php +++ b/Lib/GrouperApiAccess.php @@ -99,9 +99,9 @@ public function addGrouperGroupMember(array $queryData) $this->http->setHeader(['Content-Type' => 'application/json', 'Accept' => 'application/json']); try { - $results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($usersToAdd, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('PUT', + $connectionUrl, + json_encode($usersToAdd, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -157,9 +157,9 @@ public function createGroupWithTemplate(array $queryData) $status = true; $message = ''; try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToSave)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($groupToSave)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -235,9 +235,9 @@ public function createUpdateGroup(array $queryData) $connectionUrl = "{$this->config['fullUrl']}/groups"; try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToSave, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($groupToSave, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -286,9 +286,9 @@ public function deleteGroupWithTemplate(array $queryData) $connectionUrl = "{$this->config['fullUrl']}/gshTemplateExec"; try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToDelete, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($groupToDelete, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -336,10 +336,9 @@ public function getGrouperGroupInfo(array $queryData) $connectionUrl = "{$this->config['fullUrl']}/attributeAssignments"; try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($stemToFind, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); - + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($stemToFind, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -379,8 +378,6 @@ public function getGrouperMemberOfGroups(string $userId) . "&actAsSubjectId={$userId}"; try { $results = $this->http->sendRequest('GET', $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -415,9 +412,9 @@ public function getMembersInGroup(array $queryData) { $this->http->setHeader(['Content-Type' => 'application/json', 'Accept' => 'application/json']); try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($usersToShow, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($usersToShow, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -453,7 +450,6 @@ public function getOptionalGroups(array $queryData) try { $results = $this->useMembershipUrl($queryData); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -543,16 +539,16 @@ public function joinGrouperGroup(string $userId, string $groupName) $connectionUrl = "{$this->config['fullUrl']}/groups/{$groupName}/members"; try { - $results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($groupCommand, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('PUT', + $connectionUrl, + json_encode($groupCommand, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; } return isset($results['WsAddMemberResults']['wsGroupAssigned']) - && $results['WsAddMemberResults']['wsGroupAssigned']['name'] == urldecode($groupName); + && $results['WsAddMemberResults']['wsGroupAssigned']['name'] == urldecode($groupName); } /** @@ -586,9 +582,9 @@ public function leaveGrouperGroup(string $userId, string $groupName) $connectionUrl = "{$this->config['fullUrl']}/groups/{$groupName}/members"; try { - $results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($groupCommand, JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('PUT', + $connectionUrl, + json_encode($groupCommand, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -620,8 +616,6 @@ public function isMemberOf(string $groupName, string $userId): bool . "/{$groupNameEncoded}/members/{$userId}"; try { $results = $this->http->sendRequest('GET', $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -693,10 +687,8 @@ public function removeGrouperGroupMember(array $queryData) try { $results = $this->http->sendRequest('POST', - $connectionUrl, json_encode($userToDelete, - JSON_THROW_ON_ERROR)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $connectionUrl, + json_encode($userToDelete, JSON_THROW_ON_ERROR)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; @@ -759,9 +751,9 @@ private function useMembershipUrl(array $queryData) $connectionUrl = "{$this->config['fullUrl']}/memberships"; try { - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupsToShow)); - CakeLog::write('debug', __METHOD__ . '::connection url: ' . $connectionUrl); - CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($results, true)); + $results = $this->http->sendRequest('POST', + $connectionUrl, + json_encode($groupsToShow)); } catch (Exception $e) { CakeLog::write('error', __METHOD__ . ': An error occurred'); throw $e; diff --git a/Lib/GrouperHTTPWrapper.php b/Lib/GrouperHTTPWrapper.php index eb2d4ff..b3509e4 100644 --- a/Lib/GrouperHTTPWrapper.php +++ b/Lib/GrouperHTTPWrapper.php @@ -137,6 +137,7 @@ public function setHeader(array $headerSetting): void * @throws GrouperLiteWidgetException If issue with Grouper WS connection */ public function sendRequest(string $method, string $uri, string $body = ''): array { + CakeLog::write('debug', __METHOD__ . '::connection url: ' . $uri); $this->_request['method'] = $method; $this->_request['uri'] = $this->_parseUri($uri); @@ -156,6 +157,15 @@ public function sendRequest(string $method, string $uri, string $body = ''): arr CakeLog::write('error', __METHOD__ . ': Grouper WS returned non-200 of ' . var_export($apiResults->body(), true)); } - return json_decode($apiResults->body(), true); + $payload = json_decode($apiResults->body(), true); + + CakeLog::write('debug', __METHOD__ . '::payload: ' . var_export($payload, true)); + + $successHeader = $apiResults->getHeader('X-Grouper-success'); + if (empty($successHeader)) { + throw new RuntimeException('Web service did not even respond!'); + } + + return $payload; } }