From 10ba077ccea0988450f076ef614c3b1ff41ff00e Mon Sep 17 00:00:00 2001 From: Thibault Dockx <thibault.dockx@fusiondirectory.org> Date: Mon, 24 Mar 2025 23:58:18 +0000 Subject: [PATCH 1/4] :sparkles: Feat(webservice) - adds exec methods Allowing generic call to exec curl --- src/FusionDirectory/Rest/WebServiceCall.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/FusionDirectory/Rest/WebServiceCall.php b/src/FusionDirectory/Rest/WebServiceCall.php index dcd6d51..232e855 100644 --- a/src/FusionDirectory/Rest/WebServiceCall.php +++ b/src/FusionDirectory/Rest/WebServiceCall.php @@ -198,5 +198,22 @@ class WebServiceCall curl_close($this->ch); return $response; } + + /** + * @return array + * @throws Exception + */ +public function execute(): array +{ + $this->setCurlSettings(); + $response = curl_exec($this->ch); + $this->handleCurlError($this->ch); + + $decoded = json_decode($response, true); + curl_close($this->ch); + + return $decoded; +} + } -- GitLab From 0cc8c68b8296a4a4c5517c774d7af2c4528534a1 Mon Sep 17 00:00:00 2001 From: Thibault Dockx <thibault.dockx@fusiondirectory.org> Date: Tue, 25 Mar 2025 00:40:42 +0000 Subject: [PATCH 2/4] :sparkles: Feat(webservice) - enhance execution method --- src/FusionDirectory/Rest/WebServiceCall.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/FusionDirectory/Rest/WebServiceCall.php b/src/FusionDirectory/Rest/WebServiceCall.php index 232e855..d035969 100644 --- a/src/FusionDirectory/Rest/WebServiceCall.php +++ b/src/FusionDirectory/Rest/WebServiceCall.php @@ -199,19 +199,22 @@ class WebServiceCall return $response; } - /** +/** * @return array - * @throws Exception + * @throws \Exception */ public function execute(): array { - $this->setCurlSettings(); $response = curl_exec($this->ch); $this->handleCurlError($this->ch); $decoded = json_decode($response, true); curl_close($this->ch); + if (!is_array($decoded)) { + throw new \Exception('Invalid JSON response: ' . $response); + } + return $decoded; } -- GitLab From b06bea04931014bdb700d37bcb39599923776704 Mon Sep 17 00:00:00 2001 From: Thibault Dockx <thibault.dockx@fusiondirectory.org> Date: Tue, 25 Mar 2025 11:58:06 +0000 Subject: [PATCH 3/4] :sparkles: Feat(webservice) - add HTTP status code handling and support for empty POST data --- src/FusionDirectory/Rest/WebServiceCall.php | 34 ++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/FusionDirectory/Rest/WebServiceCall.php b/src/FusionDirectory/Rest/WebServiceCall.php index d035969..ebbeef7 100644 --- a/src/FusionDirectory/Rest/WebServiceCall.php +++ b/src/FusionDirectory/Rest/WebServiceCall.php @@ -10,6 +10,8 @@ class WebServiceCall */ private $ch; + private $httpStatusCode; // Store the HTTP status code + /** * @param string $URL * @param string $method @@ -70,7 +72,12 @@ class WebServiceCall break; case 'post': curl_setopt($this->ch, CURLOPT_POST, TRUE); - curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->data)); + + // Allows a POST to be performed without DATA + if (!empty($this->data)) { + curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->data)); + } + curl_setopt($this->ch, CURLOPT_USERAGENT, $customUserAgent); break; } @@ -206,17 +213,36 @@ class WebServiceCall public function execute(): array { $response = curl_exec($this->ch); + + // Capture the HTTP status code + $this->httpStatusCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE); + $this->handleCurlError($this->ch); - + + // Handle 204 No Content response + if ($this->httpStatusCode === 204) { + curl_close($this->ch); + return []; // Return an empty array for 204 responses + } + $decoded = json_decode($response, true); curl_close($this->ch); - + if (!is_array($decoded)) { throw new \Exception('Invalid JSON response: ' . $response); } - + return $decoded; } +/** + * Retrieve the HTTP status code of the last request + * @return int + */ +public function getHttpStatusCode(): int +{ + return $this->httpStatusCode; +} + } -- GitLab From 052c1cde21aa5275a79b7e4b43470f643748bc0b Mon Sep 17 00:00:00 2001 From: Thibault Dockx <thibault.dockx@fusiondirectory.org> Date: Tue, 25 Mar 2025 16:20:52 +0000 Subject: [PATCH 4/4] :art: Refactor(WebServiceCall) - clean up code formatting and update .gitignore for IDE files --- .gitignore | 8 ++++++ src/FusionDirectory/Rest/WebServiceCall.php | 32 ++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 6ef5c6d..824d16b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,10 @@ .directory +filelist +phpstan-baseline.neon +phpstan.neon +.idea/.gitignore +.idea/fusiondirectory-integrator.iml +.idea/modules.xml +.idea/php.xml +.idea/vcs.xml diff --git a/src/FusionDirectory/Rest/WebServiceCall.php b/src/FusionDirectory/Rest/WebServiceCall.php index ebbeef7..0ef7029 100644 --- a/src/FusionDirectory/Rest/WebServiceCall.php +++ b/src/FusionDirectory/Rest/WebServiceCall.php @@ -77,7 +77,7 @@ class WebServiceCall if (!empty($this->data)) { curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->data)); } - + curl_setopt($this->ch, CURLOPT_USERAGENT, $customUserAgent); break; } @@ -206,12 +206,12 @@ class WebServiceCall return $response; } -/** - * @return array - * @throws \Exception - */ -public function execute(): array -{ + /** + * @return array + * @throws \Exception + */ + public function execute (): array + { $response = curl_exec($this->ch); // Capture the HTTP status code @@ -225,7 +225,7 @@ public function execute(): array return []; // Return an empty array for 204 responses } - $decoded = json_decode($response, true); + $decoded = json_decode($response, TRUE); curl_close($this->ch); if (!is_array($decoded)) { @@ -233,16 +233,16 @@ public function execute(): array } return $decoded; -} + } -/** - * Retrieve the HTTP status code of the last request - * @return int - */ -public function getHttpStatusCode(): int -{ + /** + * Retrieve the HTTP status code of the last request + * @return int + */ + public function getHttpStatusCode (): int + { return $this->httpStatusCode; -} + } } -- GitLab